lundi 26 février 2018

Java: How to put values into a object from a list of randomly generated numbers

Ok so I am new to Java and this is what I need to do. Write a Java program that will allow a user to specify how many random numbers he/she would like to be generated between 1 and 100. Then produce the random and list them in your output. You should also compute the highest, lowest, sum and average. The part I need help on is getting the lowest and highest. I can't use arrays since we haven't really touched upon them yet. what I need help doing throwing something in int lowest from my randomInt.

public static void main(String[] args) 
{
     Scanner sc = new Scanner(System.in);
     Random rnd = new Random();
     int r;
     int sum = 0;
     int lowest=0;
     int highest=0;

     System.out.println("Please enter enter how many numbers "
             + "you would like to generate.");

     int userInput = sc.nextInt();

     for(r=1;r<=userInput;r++) 
    {  // was unsure how to remove the comma from the last number
        int randomInt = (int)(rnd.nextFloat()*100)+1;            
        System.out.print(randomInt + ",");

        sum += randomInt;

        if(randomInt < lowest)
        {
            lowest = randomInt;
        }            
    }    
     System.out.println(" The total sum of these random numbers are " 
             +sum);
     int average = sum / userInput;
     System.out.println("The average of all those numbers is "
             + average);
     System.out.println(lowest);
}




Aucun commentaire:

Enregistrer un commentaire