【问题标题】:Incorporating min and max value checker in my while loop?在我的while循环中加入最小值和最大值检查器?
【发布时间】:2023-03-11 18:54:02
【问题描述】:

我正在尝试结合从用户输入中获取最小值和最大值的功能,但似乎无法使其正常工作。我尝试了一个while循环,但不确定如何真正存储最小值和最大值。

public class examReview
{
    public static void main (String[]args)
    {
        Scanner input = new Scanner(System.in);
        int numOfInputs=0;
        int currentMax
        int currentMin=0;
        double sum=0;
        int intInput = 1;
        int num;

        while (intInput != 0)
        {
            intInput = input.nextInt();

            currentMin = intInput;
            currentMax = intInput;
            System.out.println("currentminis" + currentMin);
            System.out.println("currentmaxis" + currentMax);

            sum += intInput;
            numOfInputs++;
        }

        System.out.println(numOfInputs - 1);     //Prints number of input
        System.out.println(sum);                 //Prints sum of all values entered
        System.out.println(sum/(numOfInputs-1)); //Prints average
        System.out.println(currentMin);
    }
}

【问题讨论】:

  • 问题部分是什么使 int currentMax?这就是你省略分号的原因吗?顺便说一句,没有“if循环”之类的东西
  • 我真的不知道您是在谈论 if 语句还是 while 循环。我假设是后者。
  • Java Input and Out put 的可能重复项

标签: java while-loop


【解决方案1】:

您当前每次在 while 循环中更改 currentMincurrentMax
显然这些需要在循环之外设置。

   int currentMax=Integer.MIN_VALUE; 
   int currentMin=Interger.MAX_VALUE;

在while循环中根据需要进行调整

    while (intInput != 0)
    {
        intInput = input.nextInt();

        if(intInput<currentMin) currentMin = intInput;
        if(intInput>currentMax) currentMax = intInput;
        System.out.println("currentminis" + currentMin);
        System.out.println("currentmaxis" + currentMax);

        sum += intInput;
        numOfInputs++;
    }

【讨论】:

  • 您可以为currentMax 使用零,因为在这种情况下这是绝对最小值。也可以使用Math.maxMath.min来整理代码。
  • @BoristheSpider 为什么绝对最小值为零?
  • 因为intInput != 0(我想应该是intInput &gt; 0)。
  • @BoristheSpider 扫描仪是否不允许 nextInt 为 -1?
  • 我看到了一个非常相似的问题,并且 OP 说任何大于 0 的数字,而不是任何不为零的数字 - 我认为这个 OP 有同样的问题。在阅读了未在任何地方说明的问题后 - 我的错误。
【解决方案2】:
while (intInput != 0)
  {
     intInput = input.nextInt();

     if(numOfInputs==0 || intInput<currentMin)currentMin = intInput;
     if(numOfInputs==0 || intInput>currentMax)currentMax = intInput;
     System.out.println("currentminis" + currentMin);
     System.out.println("currentmaxis" + currentMax);

     sum += intInput;
     numOfInputs++;

  }

【讨论】:

  • flag 是做什么的? 1 在 Java 中未计算为布尔值“真”。
  • @maksimov 标志是愚蠢的......现在它消失了。
  • 这有点不雅,看看我的解决方案here
【解决方案3】:

Tom 有正确的答案,您想根据 intInput 测试您当前的{Min,Max}:

if (intInput < currentMin ) {
      currentMin = intInput;
}

您可以通过将 currentMin 和 currentMax 分配给异常值来简化标志日志,只要您获得至少一个输入,这已经导致问题,因为您的平均值将被零除,您应该没问题。

int currentMax = Integer.MIN_VALUE;
int currentMin = Integer.MAX_VALUE;

这样任何值都将大于 currentMax 并小于 currentMin 并且您可以摆脱该标志。

还要注意平均值可能应该是sum / numOfInputs 而不是sum / numOfInputs-1

【讨论】:

  • 这是负 1,因为输入的数量正在计算最后输入的 0。
【解决方案4】:

您需要将最大值设置为最小值,将最小值设置为最大值。然后根据需要比较设置:

int currentMax = Integer.MIN_VALUE;
int currentMin = Integer.MAX_VALUE;

while(haveInput) {
   if (intInput > currentMax) currentMax = intInput;
   if (intInput < currentMin) currentMin = intInput;
   // get more input
}

【讨论】:

    【解决方案5】:

    我认为你写的代码太多了。您应该使用 java API:

    试试这个:

    public static void main (String[]args) {
        Scanner input = new Scanner(System.in);
        List<Integer> list = new ArrayList<Integer>();
        int sum = 0;
        for (int i = input.nextInt(); i > 0; i = input.nextInt()) {
            sum += i;
            list.add(i);
        }
    
        System.out.println(list.size());          //Prints number of input
        System.out.println(sum);                  //Prints sum of all values entered
        System.out.println(list.isEmpty() ? 0d ; sum/list.size()); //Prints average
        System.out.println(Collections.min(list));//Prints min
        System.out.println(Collections.max(list));//Prints max
    }
    

    【讨论】:

    • 这就是这样做的方法,但我怀疑“即使您知道数组,也不要在此分配中使用数组。”这个问题也是如此,因为它们是very similar
    • @BoristheSpider A List 不是数组 :)
    • 虽然理论上输入的大小是不确定的,但这可能是一种危险的方法。
    • @maksimov 危险吗?为什么?
    • 输入可能是无限的,列表不是。
    猜你喜欢
    • 1970-01-01
    • 2019-03-12
    • 2022-09-29
    • 1970-01-01
    • 2021-11-27
    • 2013-10-05
    • 2016-06-07
    • 2018-09-21
    • 2018-07-09
    相关资源
    最近更新 更多