mercredi 31 octobre 2018

find average between two random numbers in java

Hello again StackOverflow,

New question needing help with some code. So far I'm making a random number generator that will choose a random number between 1-100 for rolls and then roll a "die" for a number between 1-6. It then prints out every roll and number rolled up to said random number between 1-100.

The problem I'm having is this. Let's be simple and say that the random number generated between 1-100 is 9 and the die rolls in order for each roll is

1, 6, 3 , 5, 4, 2, 1, 6, 6

That output is fine. The problem is that I need to take all these random numbers, add them together, then divide that sum by the random number between 1-100 (in the example case above, 9). I don't know how to do that. Help please?

My Current Code:

    class Main {
      public static void main(String[] args) 
  {

    int rolls = (int)(Math.random()*100);
    System.out.println("Number of Rolls: "+ rolls);

    System.out.println(" ");
    System.out.println("Rolls\t\tNumber");
    for (int i = 1; i <= rolls ; i++)
      {
        int dienumber = (int)(Math.random()*6+1);    
        System.out.println(i + "\t\t" + dienumber);
      }

    //(this space is left blank for the code I need)

  }
}




Aucun commentaire:

Enregistrer un commentaire