do while循环的逻辑错误

时间:2013-06-17 04:57:27

标签: java loops

有人可以帮助我解决我的逻辑错误。我很新,可以真正使用一些帮助。它是一个介绍类的简单程序(我想象的非常明显)。我希望用户保持循环,除非他们输入-99退出。然后它将显示条目的最高和最低。

谢谢!

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 个答案:

答案 0 :(得分:3)

您反复将input分配给其原始值:

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

这是正确的:

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

此外,假设input < -99无效,则最低值始终为-99。以下将解决此问题:

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

答案 1 :(得分:0)

您的代码存在的问题是您没有检查高低是-99。如果你把检查条件带到外面会更好。将high和low初始化为0也是一种错误的方法。您应该将它们初始化为第一个输入值。

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

现在循环。

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

打印上面的答案。

答案 2 :(得分: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!");      
    }
}

答案 3 :(得分: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!");      
}