-1

I have the following error

cannot find symbol: variable num

in all the if statements.

This is my current code.

Scanner console = new Scanner(System.in);
 int min = Integer.MAX_VALUE;
    int max = Integer.MIN_VALUE;

do{System.out.println("Type a number (or -1 to stop): ");
   int num = console.nextInt();
  }while(!num == -1);{
   System.out.print(+ num );
   }if (min < num) {
       num = min;
   }if (num > max) {
         max = num;
      } 
     
    System.out.println("maximum was : " + max);
   System.out.println("minimum was : " + min);
}

Help.

Abra
  • 11,631
  • 5
  • 25
  • 33

3 Answers3

3

The issue lies with the scope of the variable num. You have declared it inside the do block so num will not be accessible outside the do block. Also, your if condition is not correct.

Implement this and the code should run just fine:

Scanner console = new Scanner(System.in);
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
int num = 0;

do{
System.out.println("Type a number (or -1 to stop): ");
num = console.nextInt();
} while(num != -1);

if (min < num) {
num = min;
} if (num > max) {
max = num;
}

System.out.println("maximum was : " + max);
System.out.println("minimum was : " + min);
Daddys Code
  • 461
  • 8
  • thanks! the errors are gone but -1 is always the max and the min is always 2147483647, so we are not fully there yet. – Mental Overload Feb 24 '21 at 05:19
  • @MentalOverload Since there was no info available my answer was limited to solving errors so can you please provide more info as to what you want to achieve with this. It would help me better understand how to approach this problem. – Daddys Code Feb 24 '21 at 10:59
0

You want to test for max and min inside the loop and not after it terminates, since that means that you are only testing the last number that was entered – which will always be -1 (minus one). You also write that you want to ignore the -1. I believe the following code does what you require – which is to find the minimum and maximum of a series of numbers that are entered by the user.

Scanner console = new Scanner(System.in);
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
int num = 0;
do {
    System.out.print("Type a number (or -1 to stop): ");
    num = console.nextInt();
    console.nextLine();
    if (num != -1) {
        if (num > max) {
            max = num;
        }
        if (num < min) {
            min = num;
        }
    }
} while (num != -1);
if (max == Integer.MIN_VALUE) {
    System.out.println("No numbers were entered.");
}
else {
    System.out.printf("max = %d , min = %d%n", max, min);
}

Note that you need to consume the <ENTER> key that the user presses after entering each number. Hence the call to method nextLine() of class Scanner. Refer to Scanner is skipping nextLine() after using next() or nextFoo()?

If the first number entered is -1 then the user did not enter any numbers and therefore there will be no maximum or minimum. If the first number entered is -1, then the value of max will be Integer.MIN_VALUE.

Abra
  • 11,631
  • 5
  • 25
  • 33
-1

Syntax problem

while (!num == -1);{

Won't work, because Java does not allow an negate operator (!) to be at the start of a non-type boolean. You cannot do a local-body at the while loop within a do-while loop.

The fix here
You can fix your code by simply changing this line to:

while (num != -1); {...} 

Therefore you have to remove the whole while body and replace it with

while (num != -1);

So removing the bracket

I highly recommend you to learn the Java syntax.
Sincerly, Vinzent

mindcubr
  • 3
  • 1
  • While addressing one of the issues wrong with the code, you have failed to address the main issue mentioned in the post thereby not yielding the desired output – Modo Feb 24 '21 at 05:12
  • @MohitDodhia of course I did. The issue, why "num" isn't going to be found is, because the syntax is wrong. If he'd describe the main issue he has I could respond to that. "how to use input in different loops" I do not know what that means. But I know, that his syntax is the main issue, as he described! – mindcubr Feb 24 '21 at 09:11
  • The reason why `num` will not be found is because its scope is limited to the `do` block. The compiler would have thrown a syntax error if `num` was available inside the `while` condition. By this I do not mean that your assessment of a syntax error is incorrect; I am simply saying that the primary problem in this code was the scope of variable `num` followed by the syntax error which you have already corrected in your answer. – Modo Feb 24 '21 at 11:10