java 使用 for 循环查找最小值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36615551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:40:42  来源:igfitidea点击:

find minimum value using a for loop

javaif-statementfor-loopminimum

提问by ceggleston55

I am trying to take 10 integers from the user's input and find the minimum value using a forloop.

我试图从用户的输入中获取 10 个整数并使用for循环找到最小值。

I am struggling to correctly write the ifstatement. It is supposed to take the first input and make that the smallest and compare the next inputs to that.

我正在努力正确地编写if声明。它应该采用第一个输入并将其设为最小并将下一个输入与之进行比较。

My final print statement just prints the last number entered.

我的最终打印语句只打印输入的最后一个数字。

Scanner scan = new Scanner(System.in);

int smallest = 0;
int number = 0;

for (int i = 1; i <= 10; i++) {
   System.out.print("Enter a number > ");
   number = scan.nextInt();

   if (number < smallest) {
       smallest = number; 
   } else {  
       smallest = number; 
   }       
}
System.out.println("The minimum is " + smallest);

回答by Dawood ibn Kareem

One of your problems is that you're starting with smallest = 0, which means it will only change if one of the inputs is less than zero. There are two ways you could fix this. EITHER

您的问题之一是您从 开始smallest = 0,这意味着只有当其中一个输入小于零时它才会改变。有两种方法可以解决这个问题。任何一个

  • Start with int smallest = Integer.MAX_VALUE;
  • 从...开始 int smallest = Integer.MAX_VALUE;

OR

或者

  • Change the condition for updating smallestto if (number < smallest || i == 1 )
  • 将更新条件更改smallestif (number < smallest || i == 1 )

Additionally, you don't want to update smallestif the ifclause doesn't fire, so remove the elseblock.

此外,smallest如果if子句没有触发,您不想更新,因此删除else块。

回答by Stultuske

With this:

有了这个:

if (number < smallest) {
    smallest = number; 
}  else {  
    smallest = number; 
}

You always override the value of smallest, whether number is smaller or not.

无论数字是否较小,您始终会覆盖最小的值。

Remove the else block completely, and it will work.

完全删除 else 块,它将起作用。

EDIT Also: don't use 0 as default value. Take the first value you read as the 'original smallest'

编辑另外:不要使用 0 作为默认值。将您读取的第一个值作为“原始最小”

System.out.print("Enter a number > ");
int smallest = scan.nextInt();
  int number = 0;

  for (int i = 1; i <= 9; i++) {
     System.out.print("Enter a number > ");
     number = scan.nextInt();
        if (number < smallest) {
           smallest = number; 
        }
  }

回答by Ahmed M. Abed

Try something like this

尝试这样的事情

Scanner scan = new Scanner(System.in);

int smallest = 0;
int number = 0;

for (int i = 1; i <= 10; i++) {
   System.out.print("Enter a number > ");
   number = scan.nextInt();
   if (i == 1){
       smallest = number;
   }
   if (number < smallest) {
       smallest = number; 
   }

}

System.out.println("The minimum is " + smallest);

回答by nhouser9

Two issues.

两个问题。

1 - your if should look like this (remove the else block):

1 - 你的 if 应该是这样的(去掉 else 块):

if (number < smallest) {
 smallest = number; 
}

2 - you should initialize smallest to a very large number so the first number seen is always smaller than it:

2 - 您应该将smallest初始化为一个非常大的数字,以便看到的第一个数字始终小于它:

int smallest = Integer.MAX_VALUE;

回答by GHajba

Solution: remove your elsestatement.

解决方案:删除您的else声明。

if (number < smallest) {
    smallest = number; 
}

Without any else. With the usage of elseyou set the value of smallestevery time to the value entered.

没有任何else. 随着使用,else您将smallest每次的值设置为输入的值。

回答by Topani

This would be my preference for making the first assignment to smallestvariable. Make a separate assignment to smallestaltogether before the loopbegins. This way we know exactly which is the first statement to assign to smallest, and as others have stated previously get rid of the else block for ifstatement within the for loop.

这将是我对smallest变量进行第一次分配的偏好。smallestloop开始之前对总做一个单独的分配。通过这种方式,我们确切地知道要分配给哪个语句的第一个语句smallest,并且正如其他人之前所说的那样,去掉 for 中的 else 块 forif语句loop

The elseblock is causing what OP stated as problem of prints the last number entered. N Now since the prompt is presented in 2 different places also added a String variable for 'prompt' so it can be reused. Notice for loop count reduced to 9 from 10 to keep with only prompting user input 10 times.

else块导致 OP 所说的打印输入的最后一个数字的问题。N 现在,由于提示出现在 2 个不同的地方,因此还为“提示”添加了一个字符串变量,因此可以重复使用。注意 for 循环计数从 10 减少到 9,以保持只提示用户输入 10 次。

    Scanner scan = new Scanner(System.in);

      int smallest = 0;
      int number = 0;
      String prompt = "Enter a number > ";

      // First user prompt
      // and first assignment to 'smallest'
      System.out.print(prompt);
      smallest = scan.nextInt();

      for (int i = 1; i <= 9; i++) {
         System.out.print(prompt);
         number = scan.nextInt();

            if (number < smallest) {
               smallest = number; 
            }

      }
         System.out.println("The minimum is " + smallest);