0

Can somebody please help me with my logic error. I am very new and could really use some help. Its a simple program for an intro class (painfully obvious I would imagine). I would like the user to stay in the loop unless they enter -99 to exit. Then it will display the highest and lowest of the entries.

Thank You!

import java.util.Scanner;

public class LeastGreatest {

    public static void main(String[] args) {

      Scanner keyboard = new Scanner(System.in);
        int input = 0, high = 0, low = 0;

        System.out.println("Welcome to fun with Loops and Numbers!\n");
      System.out.println("Please Enter the AN INTEGER: \n");
        input = keyboard.nextInt();

        high = input;
        low = input;
        //System.out.println(input);
        do
        {
            System.out.println("Please Enter the AN INTEGER (Press -99 to Exit): \n");
            input = keyboard.nextInt();

            if (input > high)
            {
                input = high;
            }
            if (input < low)
            {
                input = low;
            }
        } while(input != -99);

        System.out.println("The highest INT entered was: " + high);
        System.out.println("The lowest INT entered was: " + low);
        System.out.println("Thank You! Goodbye!");      
    }
}

4 Answers 4

3

You are repeatedly assigning input to its original value:

high = input;
low = input;
...
if (input > high)
{
  input = high; // input = input
}
if (input < low)
{
  input = low; // input = input
}

This is correct:

if (input > high)
{
  high = input;
}
if (input < low)
{
  low = input;
}

Also, assuming that input < -99 isn't valid, the lowest value will always be -99. The following will correct this issue:

while (input != -99) { // Break BEFORE setting low to -99.
  if (input > high)
  {
    high = input;
  }
  if (input < low)
  {
    low = input;
  }

  System.out.println("Please Enter the AN INTEGER (Press -99 to Exit): \n");
  input = keyboard.nextInt();
}
Sign up to request clarification or add additional context in comments.

Comments

0

The problem with your code is you are not checkin to see if high and low is -99. Better would be if you bring the check condition outside. Also initialising high and low to 0 is a wrong method to do this. You should initialise them to the first input value.

input = keyboard.nextInt();
if(input!=-99)
{
   high=input;
   low=input;
}

Now the loop.

while(input!=99)
{
  if(input>high)
     high=input;
  else if(input<low)
     low=input;
}

And print the answers like above.

Comments

0
import java.util.Scanner;

public class LeastGreatest {

    public static void main(String[] args) {

        Scanner keyboard = new Scanner(System.in);
        int input = 0, high = 0, low = 0;

        System.out.println("Welcome to fun with Loops and Numbers!\n");
        // System.out.println("Please Enter the AN INTEGER: \n"); -redundant codes 
        // input = keyboard.nextInt();

       //high = input;
      //low = input;


        do
        {
            System.out.println("Please Enter the AN INTEGER (Press -99 to Exit): \n");
            input = keyboard.nextInt();     // you can surround this with try catch to validate integer

            if (input > high){
                high = input;
            } else if (input < high && input > low){     
// this is to set that 
//lowest possible will be a 0 (as you declared it above), unless you want to accept 
//negative integers (then you will have to change the conditions)
                low = input;
            }
        } while(input != -99);

        System.out.println("The highest INT entered was: " + high);
        System.out.println("The lowest INT entered was: " + low);
        System.out.println("Thank You! Goodbye!");      
    }
}

Comments

0
public static void main(String[] args) {

    Scanner keyboard = new Scanner(System.in);
    int input = 0, high = 0, low = 0;

    System.out.println("Welcome to fun with Loops and Numbers!\n");
    System.out.println("Please Enter the AN INTEGER: \n");
    input = keyboard.nextInt();

    high = input;
    low = input;
    //System.out.println(input);
    do
    {
        if (input > high)
        {
            high = input;
        }
        if (input < low)
        {
            low = input;
        }

        System.out.println("Please Enter the AN INTEGER (Press -99 to Exit): \n");
        input = keyboard.nextInt();

    } while(input != -99);

    System.out.println("The highest INT entered was: " + high);
    System.out.println("The lowest INT entered was: " + low);
    System.out.println("Thank You! Goodbye!");      
}

Comments

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.