0

I am prompting the user for a series of numbers and want to keep track of the largest one entered and print that out at the end. Example:

Please enter a number (enter -1 to quit): 10

Enter your next number (-1 to quit): 7

Enter your next number (-1 to quit): 15

Enter your next number (-1 to quit): 11

Enter your next number (-1 to quit): -1

The largest number entered was 15

How do I print this largest number? Here is what I have tried:

int a;

while(true){
    System.out.print("Please enter a number (enter -1 to quit):");
    a = user.nextInt();
    if (a==-1) 
    {
        System.out.println("The largest number is the "+ );
        break;
    }
}
12
  • Why not store everything in an array, the do a sort? Commented Nov 10, 2014 at 16:19
  • Keep track of the max number you have encountered on every loop, then just print it Commented Nov 10, 2014 at 16:20
  • 1
    Please explain about your idea on how to implement it first. Commented Nov 10, 2014 at 16:23
  • 1
    @McAdam331 he was talking to OP. Commented Nov 10, 2014 at 16:28
  • 1
    @McAdam331 It was meant for Hyunseok Song Commented Nov 10, 2014 at 17:59

1 Answer 1

3

You should store a variable outside the loop that you change when needed.

Typically, you would set this number equal to the first value, but for this case, you can set it to -1 since that is not something that will change. Try this:

int maxNumber = -1;
while(true){
   System.out.print("Please enter a number (enter -1 to quit):");
   a = user.nextInt();
   if(a != -1 && a > maxNumber){
      maxNumber = a;
   else if(a == -1){
      // You can do a check here to be sure they even entered a number.
      if(maxNumber == -1){
         System.out.println("No number was input.");
      } else{
         System.out.println("The max number was " + maxNumber);
      }
   }
}

EDIT for further explanation

What this will do is check that if the user has entered a number that is larger than anything entered yet. If it is, it will change the value to that number. Example: If I type 2, and then 3, maxNumber will become 3. If I type in 0, max number will remain 3. If at any point I type -1, it will break as you have set up already.

EDIT 2

As far as what I meant when I said 'you would set this number equal to the first value' let me explain.

A user can input any value, except -1. So, how do I set the initial max value? If I say maxNumber = 0 and you enter nothing but negatives, I will print 0 as the max number when I am done. That being said, what I have done above is still not fool proof. If the user enters values only less than -1, I still will print an incorrect max. So, it would be best practice to get the first input value, and set that to your max, and change it if a new one is encountered. It would change the code to look like this:

int maxNumber = -1; // Just a junk value to start.
boolean firstNumber = true;
while(true){
   System.out.print("Please enter a number (enter -1 to quit): ");
   a = user.nextInt();
   if(a == -1 && firstNumber){ // If the user enters -1 right away.
      System.out.println("No number was input.");
   } else if(a == -1 && !firstNumber){ // If they've entered -1 after inputting a valid number
      System.out.println("The max number was " + maxNumber);
   } else{ // They've entered a valid number.
      if(firstNumber){ // If this is the first number, we set the max initially, and change this flag to false now.
         maxNumber = a;
         firstNumber = false;
      } else{ // Not first number, so only check if the input is larger.
         if(a > maxNumber){
            maxNumber = a;
         }
      }
   }
}
Sign up to request clarification or add additional context in comments.

4 Comments

int maxNumber = -1; is clumsy. +1 if you do this in a cuter way.
@bathsheba agreed, was in the middle of editing while you commented. Was thinking on the fly with -1.
I don't like it since you can get bugs when the mandate of the function creeps to include negative inputs.
@Bathsheba what do you mean?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.