0

有人可以帮我解决我的逻辑错误。我很新,真的可以使用一些帮助。它是一个简单的介绍类程序(我想这是非常明显的)。我希望用户留在循环中,除非他们输入 -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

4 回答 4

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();
}
于 2013-06-17T05:01:19.510 回答
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!");      
    }
}
于 2013-06-17T05:10:32.537 回答
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!");      
}
于 2013-06-17T05:38:19.050 回答
0

您的代码的问题是您没有检查高低是否为-99。如果您将检查条件带到外面会更好。同样将高低初始化为 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;
}

并打印上面的答案。

于 2013-06-17T05:06:07.773 回答