简体   繁体   中英

logic error with do while loops

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!");      
    }
}

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();
}

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.

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!");      
    }
}
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!");      
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM