We help IT Professionals succeed at work.
Get Started
Troubleshooting Question

Java variable

Euro5
Euro5 asked
on
37 Views
Last Modified: 2021-03-11
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)
      {
         // Ask for another number.
         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!
Unlock 3 Answers and 9 Comments.
See Answers
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

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

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

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE