简体   繁体   English

使用for循环查找最小值

[英]find minimum value using a for loop

I am trying to take 10 integers from the user's input and find the minimum value using a for loop. 我试图从用户的输入中获取10个整数,并使用for循环查找最小值。

I am struggling to correctly write the if statement. 我正在努力正确地编写if语句。 It is supposed to take the first input and make that the smallest and compare the next inputs to that. 应该采用第一个输入并使之最小,然后将第二个输入与之比较。

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

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. 您的问题之一是,您从smallest = 0 ,这意味着仅当输入之一小于零时,它才会更改。 There are two ways you could fix this. 有两种方法可以解决此问题。 EITHER 以太网

  • Start with int smallest = Integer.MAX_VALUE; int smallest = Integer.MAX_VALUE;

OR 要么

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

Additionally, you don't want to update smallest if the if clause doesn't fire, so remove the else block. 此外,如果if子句未触发,您也不想更新smallest的内容,因此请删除else块。

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. 还要编辑:不要使用0作为默认值。 Take the first value you read as the 'original smallest' 将您读取的第一个值称为“原始最小”

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

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

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-您应该将最小值初始化为一个非常大的数字,以便看到的第一个数字始终小于该数字:

int smallest = Integer.MAX_VALUE;

Solution: remove your else statement. 解决方案:删除您的else语句。

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

Without any else . 没有else With the usage of else you set the value of smallest every time to the value entered. 使用else的用法,每次将smallest设置为输入的值。

This would be my preference for making the first assignment to smallest variable. 这是我对第一个smallest变量进行赋值的偏好。 Make a separate assignment to smallest altogether before the loop begins. loop开始之前,请分别对smallest对象进行分配。 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 if statement within the for loop . 这样,我们就确切地知道哪个是要分配给smallest的第一个语句,并且正如其他人先前所说的,摆脱了for loop if语句的else块。

The else block is causing what OP stated as problem of prints the last number entered . else块导致OP所说的问题是打印最后输入的数字 N Now since the prompt is presented in 2 different places also added a String variable for 'prompt' so it can be reused. N现在,由于提示在2个不同的位置显示,因此还为“提示”添加了String变量,因此可以重复使用它。 Notice for loop count reduced to 9 from 10 to keep with only prompting user input 10 times. 注意循环计数从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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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