0
 public void findHighestNumber(int timesToLoop)
    {   
         int highNumber = 0;
         int counter    = 0;
         int userInput  = 0;

         while(counter < timesToLoop) {
             System.out.print("Enter a whole number: ");
             userInput = reader.getInput();
             counter++;
             System.out.println("");  
         }     
         if(userInput > highNumber) {
             highNumber = userInput;
         }      
         if(counter == timesToLoop) { 
             System.out.println("Finished looping! Highest is " +highNumber);
             System.out.println("");
         }     
         if(counter > timesToLoop) {        
             System.out.println("You have entered a negative number: " +timesToLoop);
             System.out.println("");
         }

    }

How do I track the highest number? I can't figure this out.

2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by SuperManofBC
0

you'll need to put your check whether or not your number is higher than the currently stored one within your while loop, or you will only check whether the last number entered is greater than 0.

this is not really a java issue, as much as a logical one. write down what has to be done when, and what has to be done how many times. check that list next to your code, like checking where your while loop ends, and how many times your other instructions will be executed, and you will see the problem.

0

Hey thanks.. I put the if statement of the highest number inside the while loop and it worked...

  while(counter < timesToLoop) {
             System.out.print("Enter a whole number: ");
             userInput = reader.getInput();
             counter++;
             System.out.println("");       
             if(userInput > highNumber) {
             highNumber = userInput;
             }   
  }          

...

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.