We help IT Professionals succeed at work.
Troubleshooting Question

Java variable

on
37 Views
Can anyone help me understand this -
why is if(largest == -99)  and not if(num == -99)?
It only works when I use largest, but I have no idea why!?
Anyone with java experience?
I am completed stumped..
```public class LargestAndSmallest
{
public static void main(String[] args)
{
String input;        // To hold keyboard input
int num;             // A number
int largest;         // The largest number
int smallest;        // The smallest number

// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);

// Get the first number.
System.out.print("Enter an integer, or -99 to quit: ");
num = keyboard.nextInt();

// Since this is the only number we have, it is the both the
// largest and the smallest... so far.
largest = num;
smallest = num;

// Get the rest of the numbers.
while (num != -99)
{
System.out.print("Enter an integer, or -99 to quit: ");
num = keyboard.nextInt();

// Determine whether this is now the largest.
if (num != -99 && num > largest)
largest = num;

// Determine whether this is now the smallest.
if (num != -99 && num < smallest)
smallest = num;
}

// Display the largest and smallest numbers.
if (largest == -99)
System.out.println("You did not enter any numbers.");
else
{
System.out.println("Largest: " + largest);
System.out.println("Smallest: " + smallest);
}```
Comment
Watch Question
CERTIFIED EXPERT
Commented:
This problem has been solved!
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

Deciding to stick with EE.

Mohamed Asif

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant