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

时间:2016-08-22 09:49:48

标签: java

此代码询问用户将输入多少个数字,然后它会采用每个数字。最后它应该返回最小的值。我知道Math.min方法,我只是在努力解决为什么下面的逻辑无法工作,它总是打印最后一个输入而不是最小输入。

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

    }

}

1 个答案:

答案 0 :(得分:-3)

将(最小&lt;输入)更改为(最小&gt;输入)。

相关问题