Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Need some helping finding the lowest, highest, sum, and average.

  1. #1
    Junior Member
    Join Date
    Feb 2018
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Need some helping finding the lowest, highest, sum, and average.

    Hello if anyone could help I would appreciate it. The issue I am having with this is that can't seem to figure out what to use for the highest, lowest, sum and average despite getting the random values to spit out.

    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. Output should be neat and user friendly. You may find that the output would be much nicer if you use System.out.print(); instead of ptintln(). Using print() will keep the next thing printed on the same line and look like this: 123456. Then you could use commas to separate the numbers like 1,2,3,4.
    (needs to be between 10 and 20 numbers)

    import java.util.Scanner;
    import java.util.Random;
     
    public class Looper
    {   
        public static void main(String[] args) 
        {
             Scanner sc = new Scanner(System.in);
             Random rnd = new Random();  
             int r;
     
             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
                System.out.print((int)(rnd.nextFloat()*100)+1 + ",");            
            }      
        }
    }
    Last edited by rmeade; February 26th, 2018 at 05:33 PM. Reason: Forgot some stuff and reorder

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,404
    Thanks
    53
    Thanked 2,399 Times in 2,352 Posts

    Default Re: Need some helping finding the lowest, highest, sum, and average.

    what to use for the highest, lowest, sum and average
    Ok work on one of those at a time.
    I think the easiest would be the sum. See if you can write a loop to sum the values.
    Then the average would be the sum divided by the count.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    rmeade (February 26th, 2018)

  4. #3
    Junior Member
    Join Date
    Feb 2018
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Need some helping finding the lowest, highest, sum, and average.

    Ok so i am able to write a loop to sum a value, but I can't seem to figure how to take those random numbers I got and put them into a sum. I assume a I need a something to hold them but I don't know what to use.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,404
    Thanks
    53
    Thanked 2,399 Times in 2,352 Posts

    Default Re: Need some helping finding the lowest, highest, sum, and average.

    I need a something to hold them
    Define a variable outside of the loop (same as r) and inside the loop add each random number to it.
    You would not need a new loop. I was assuming the random numbers were to be saved in an array but I see that is not the case.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    rmeade (February 26th, 2018)

  7. #5
    Junior Member
    Join Date
    Feb 2018
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Need some helping finding the lowest, highest, sum, and average.

    Yeah we haven't learned about arrays yet. I got the sum working thank you. This is what I have now. What do you suggest I try next. I was thinking lowest and highest.
    mport java.util.Scanner;
    import java.util.Random;
     
    public class Looper
    {   
        public static void main(String[] args) 
        {
             Scanner sc = new Scanner(System.in);
             Random rnd = new Random();  
             int r;         
             int sum = 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;            
            }    
             System.out.println(" The total sum of these random numbers are " 
                     +sum);
        }
    }

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,404
    Thanks
    53
    Thanked 2,399 Times in 2,352 Posts

    Default Re: Need some helping finding the lowest, highest, sum, and average.

    Ok, try lowest. Use a variable to hold the lowest value found so far. Compare each new number against it and save if lower.

    You also could have done the average once you have the sum.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    rmeade (February 26th, 2018)

  10. #7
    Junior Member
    Join Date
    Feb 2018
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Need some helping finding the lowest, highest, sum, and average.

    I see ok I did the average real quick since it is just sum / userInput in my case. Thank you reminding me about that.

    --- Update ---

    Ok so i have something to hold the lowest but I am having trouble getting something put to it and than comparing.
    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;
     
            }    
             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);
        }
    }

  11. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,404
    Thanks
    53
    Thanked 2,399 Times in 2,352 Posts

    Default Re: Need some helping finding the lowest, highest, sum, and average.

    Use an if statement to compare and when true save the value.

    See the tutorial on if statements: https://docs.oracle.com/javase/tutor...dbolts/if.html
    If you don't understand my answer, don't ignore it, ask a question.

  12. #9
    Junior Member
    Join Date
    Feb 2018
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Need some helping finding the lowest, highest, sum, and average.

    Thank for the help Norm I have Finished this thanks to you. For anyone else who might need this (doubt they will xD) here is my final code.
     import java.util.Scanner;
    import java.util.Random;
     
    public class Looper
    {   
        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(r == 1 || randomInt < lowest)
                {
                    lowest = randomInt;
                }      
     
                if(r == 1 || randomInt > highest)
                {
                    highest = 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("The lowest number is " + lowest + ".");
             System.out.println("The higest number is " + highest + ".");
        }
    Last edited by rmeade; February 26th, 2018 at 08:29 PM. Reason: Finished the program

  13. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,404
    Thanks
    53
    Thanked 2,399 Times in 2,352 Posts

    Default Re: Need some helping finding the lowest, highest, sum, and average.

    What is the starting value in lowest? Are there any random numbers that will be less than its initial value?
    Try a different starting value.
    If you don't understand my answer, don't ignore it, ask a question.

  14. The Following User Says Thank You to Norm For This Useful Post:

    rmeade (February 26th, 2018)

Similar Threads

  1. Sorting from lowest to highest issue
    By cb0688 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 24th, 2014, 06:34 AM
  2. Finding the lowest number in an array.
    By EatMyBible in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 4th, 2013, 01:49 AM
  3. Replies: 7
    Last Post: November 18th, 2012, 04:17 AM
  4. [SOLVED] flightpaths, finding lowest cost -- lowest amount of crossovers
    By CjStaal in forum Algorithms & Recursion
    Replies: 4
    Last Post: May 8th, 2012, 12:47 AM
  5. Replies: 4
    Last Post: June 10th, 2009, 01:04 AM