标签:通过 alt line static ble 内容 info inf 用户交互
public class Demo03 {
public static void main(String[] args) {
//从键盘接收数据
Scanner scanner = new Scanner(System.in);
System.out.println("请输入数据:");
String str=scanner.nextLine();
System.out.println("输出内容为:"+str);
scanner.close();
}
}
public class Demo04 {
public static void main(String[] args) {
Scanner scanner =new Scanner(System.in);
//从键盘接收数据
int i=0;
float f = 0.0f;
System.out.println("请输入整数:");
//如果那么
if (scanner.hasNextInt()){
i = scanner.nextInt();
System.out.println("整数数据:"+i);
}else{
System.out.println("你输入的不是整数!");
}
System.out.println("请输入小数:");
if (scanner.hasNextFloat()){
f = scanner.nextFloat();
System.out.println("小数数据:"+f);
}else{
System.out.println("你输入的不是小数!");
}
}
}
public class Demo05 {
public static void main(String[] args) {
//输入多个数字,求总和和平均数。每输入一个数字用回车确认,通过输入非数字来结束输出并输出结果
Scanner scanner = new Scanner(System.in);
//和
double sum=0;
//计算输入了多少个数字
int m=0;
//通过循环判断是否还有输入,并再里面对每一次进行求和计算
while (scanner.hasNextDouble()){
double x=scanner.nextDouble();
m=m+1;//m++
sum = sum +x;
System.out.println("你输了了第"+m+"个数据,当前结果为sum="+sum);
}
System.out.println(m+"个数和为"+sum);
System.out.println(m+"个数的平均值"+(sum/m));
scanner.close();
}
}
标签:通过 alt line static ble 内容 info inf 用户交互
原文地址:https://www.cnblogs.com/styh/p/14493190.html