scanner
scanner 类中的next() , nextLine()方法
package com.study.scanner; import java.util.Scanner; public class a1 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("使用NEXTLINE方式接受:"); if(scanner.hasNextLine()){ String str = scanner.nextLine(); System.out.println("输出的内容为:"+str); } scanner.close(); } } /* 使用NEXTLINE方式接受: study english 输出的内容为:study english Process finished with exit code 0 */
package com.study.scanner; import java.util.Scanner; public class b1 { public static void main(String[] args) { //创建一个扫描器对象,用于接受键盘数据 Scanner scanner = new Scanner(System.in); System.out.println("使用NEXT方式接受:"); //判断用户有没有输入字符串 if(scanner.hasNext() == true){ //使用next方式接收 String str = scanner.next(); System.out.println("输出的内容为:"+str); } //凡是属于IO流的类如果不关闭会一直占用资源,要养成好习惯用完就关闭 scanner.close(); } } /* 使用NEXT方式接受: study english 输出的内容为:study Process finished with exit code 0 */
package com.study.scanner; import java.util.Scanner; public class b2 { 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; sum = sum+x; System.out.println("你输入了第"+m+"个数据,然后当前结果是sum="+sum); } System.out.println(m+"个数的和为"+sum); System.out.println(m+"个数的平均值是"+(sum/m)); scanner.close(); } }