为什么此代码显示异常?

时间:2016-07-09 16:00:52

标签: java for-loop exception exception-handling java.util.scanner

我首先要获得2个输入作为第一和第二参数。然后显示这些参数之间的所有奇数。 即使我导入了java util:

,这也是我得到的输出
import java.util.Scanner;

public class HelloWorld {
    public static void main(String[] args) {
        Scanner ask = new Scanner(System.in);
        System.out.print("first");
        int a = ask.nextInt();
        System.out.print("second");
        int b = ask.nextInt();

        int sum = 0;
        for (int n = a; n <= b; n++) {
            if (n % 2 == 1) {
                sum = sum + n;
                System.out.print(n + " ");

            }
        }
        System.out.print("*" + sum);
    }
}

<div class="col-xl-11 col-xl-offset-1 col-lg-11 col-lg-offset-1 col-md-11 col-md-offset-1 col-sm-11 col-sm-offset-1 col-xs-11 col-xs-offset-1">

0 个答案:

没有答案