import java.util.Scanner;
public class Demo3 {
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();
}
}
进阶
importjava.util.Scanner;publicclassDemo4{publicstaticvoidmain(String[] args){Scanner scanner =newScanner(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("输入的不是小数数据!");}
scanner.close();}}
importjava.util.Scanner;publicclassDemo5{publicstaticvoidmain(String[] args){Scanner scanner =newScanner(System.in);double sum =0;int m =0;while(scanner.hasNextDouble()){double x = scanner.nextDouble();++m;
sum = sum + x;}System.out.println(m +"个数的和为"+ sum);System.out.println(m +"个数的平均值为"+(sum / m));
scanner.close();}}