这个小的for/if循环逻辑有什么问题?

问题描述:

此代码要求用户输入多少个数字,然后每个数字都需要输入。最后它应该返回最小值。我知道Math.min方法,我只是为了下面的逻辑不起作用而挣扎,它总是打印最后一个输入,而不是最小的输入。这个小的for/if循环逻辑有什么问题?

import java.util.Scanner; 

public class Ch5_smallestValue { 

    public static void main(String[] args) { 

     Scanner sc = new Scanner(System.in); 
     System.out.println("Input how many numbers and then input each one"); 
     int hMany = sc.nextInt(); 

     int firstNum = sc.nextInt(); 
     int smallest = firstNum; 

     for (int i = hMany; i > 1; i--){ 

      int input = sc.nextInt(); 
      if (smallest < input){ 
       smallest = input; 
      } 

     } 

     System.out.println("smallest = " + smallest); 

    } 

} 
+8

不应该条件是'最小>输入'?手动运行代码,你将能够自己发现这样的错误。 – Codebender

+0

它因为你在'if'的条件是错误的。查看以上评论 –

+0

好的,非常感谢!它的混乱虽然在我的头最小值应该是更小,即“)比其他输入数:)编辑:我现在得到它:) – ApRax

变化(最小<输入)到(最小>输入)。