(1)package Bokeyuan;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
int nextValue;
int sum = 0;
Scanner kbInput = new Scanner(System.in);//创建Scanner对象
kbInput.useDelimiter("\s");//设置分隔符
while (kbInput.hasNextInt()) {//使用hasNextInt方法判断是否还有整数
nextValue = kbInput.nextInt();//读取数据流中的下一个数
sum += nextValue;
}
kbInput.close();//关闭流对象
System.out.println(sum);
}
}
(2)
package Bokeyuan;
import java.util.Scanner;
public class Add {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int sum = 0;
while(a != 0){
sum += a;
a = in.nextInt();
}
System.out.println(sum);
}
}
本来想用循环的办法看能不能给做出来,结果循环的条件实在想不出来是什么。。就只能用一个条件来结束循环了。。。
期末作业还在商讨当中。。好了补上。。