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.

Recommended Answers

All 2 Replies

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.

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;
             }   
  }          

...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.