Java第三次作业

import java.util.Scanner;


public class TestScanner {
    public static void main(String[] args) {
         int nextValue;
         int sum = 0;
         Scanner kbIntput = new Scanner(System.in);
        kbIntput.useDelimiter("\s");//设置分隔符
        while(kbIntput.hasNextInt())
         {
            nextValue = kbIntput.nextInt();
            sum+=nextValue;
         }
        kbIntput.close();
        System.out.println("Sum:"+sum);
    }
}
public boolean hasNextInt()
如果通过使用 nextInt() 方法,此扫描器输入信息中的下一个标记可以解释为默认基数中的一个 int 值,则返回 true。扫描器不执行任何输入。
返回:
当且仅当此扫描器的下一个标记是有效的 int 值时才返回 true。

在没有设置分隔符的情况下必须在输入的数后面加一个字母才能运行得出sum

设置了分隔符后不用在后面加入一个字母就可以得出sum

原文地址:https://www.cnblogs.com/Mrsli/p/5329739.html