如何从java控制台获得用户输入
1个回答
展开全部
java.util.Scanner类
import java.util.Scanner;
publicclass Test {
public static void main(String[] args) {
System.out.println("请输入若干个数,每输入一个数回车确认");
System.out.println("最后输入数字0结束输入操作");
Scanner reader = new Scanner(System.in);
double sum = 0;
double x = reader.nextDouble();
while(x != 0){
sum = sum + x;
x =reader.nextDouble();
}
System.out.println("sum = " +sum);
}
}
import java.util.Scanner;
publicclass Test {
public static void main(String[] args) {
System.out.println("请输入若干个数,每输入一个数回车确认");
System.out.println("最后输入数字0结束输入操作");
Scanner reader = new Scanner(System.in);
double sum = 0;
double x = reader.nextDouble();
while(x != 0){
sum = sum + x;
x =reader.nextDouble();
}
System.out.println("sum = " +sum);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |