之前学的基本语法中还并没有实现程序和人的交互,java提供了这样一个交互类,我们可以获取用户的输入。 java.util.Scanner(工具包)是java5的新特性。即可以通过Scanner类类获取用户输入。
基本语法
Scanner s = new Scanner (System.in); //System out输出,System in输入
package com.shusheng01.scanner; import java.util.Scanner; public class Demo01 { 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();//关闭scanner /* 使用next方式接收 hello world(输入的字符串) 输入的内容为:hello(输出以后的字符) Process finished with exit code 0 */ } }
package com.shusheng01.scanner; import java.util.Scanner; public class Demo02 { 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); /* 使用nextLine方式接收 hello world 输出的内容为:hello world Process finished with exit code 0 */ } scanner.close(); } }
next:
nextLine:
package com.shusheng01.scanner; import java.util.Scanner; 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("输入的不是小数数据"); } scanner.close(); /* 请输入整数: 30 整数数据:30 请输入小数: 35 小数数据:35.0 Process finished with exit code 0 */ } }
求和及平均数:
package com.shusheng01.scanner; import java.util.Scanner; public class Demo05 { public static void main(String[] args) { System.out.println("请输入数据"); //我们要输入多个数字,并且求其总和与平均数,每输入一个数字 //用回车键确认,通过输入非数字来结束输入并且输出执行结果 Scanner scanner =new Scanner(System.in); //求和 double sum = 0; //计算输入了多少个数字 int m = 0; //通过循环判断是否还有输入,并且在里面对每一次进行求和统计 while (scanner.hasNextDouble()){ double x = scanner.nextDouble(); //输入一个数x m = m+1; sum = sum +x; System.out.println("你输出了第"+m+"个数据,当前结果为"+sum); } System.out.println("共输入"+m+"个数据"); System.out.println(m+"个数的和为"+sum); System.out.println(m+"个数的平均数为"+(sum/m)); scanner.close(); /* 请输入数据 10 你输出了第1个数据,当前结果为10.0 20 你输出了第2个数据,当前结果为30.0 30 你输出了第3个数据,当前结果为60.0 40 你输出了第4个数据,当前结果为100.0 55 你输出了第5个数据,当前结果为155.0 60 你输出了第6个数据,当前结果为215.0 70 你输出了第7个数据,当前结果为285.0 60 你输出了第8个数据,当前结果为345.0 8888 你输出了第9个数据,当前结果为9233.0 x 共输入9个数据 9个数的和为9233.0 9个数的平均数为1025.888888888889 Process finished with exit code 0 */ } }
if单选择结构
if双选择结构
if多选择结构
嵌套的if结构
switch多选择结构
很多时候判断一个东西是否可行,然后再去执行,这样一个过程在程序中用if语句来表示
if(布尔表达式){ //如果布尔表达式为true将执行的语句 }
package com.shusheng01.struct; import java.util.Scanner; public class ifDemo01 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入内容"); String s = scanner.nextLine(); //equals:判断字符串是否相等 if (s.equals("Hello")){ System.out.println(s); } System.out.println("END"); scanner.close(); } }
即:A计划行不通,则进行B计划
if(布尔表达式){ //如果布尔表达式的值为true }else{ //如果布尔表达式的值为false }
package com.shusheng01.struct;import java.util.Scanner;public class IfDemo02 { public static void main(String[] args) { //考试分数大于60分及格,小于60分不及格 Scanner scanner = new Scanner(System.in); System.out.println("请输入成绩"); int score = scanner.nextInt(); if (score>60){ System.out.println("及格"); }else { System.out.println("不及格"); }/* 请输入成绩78及格Process finished with exit code 0*/ }}
当问题有多个选项的时候使用多选择结构
if(布尔表达式1){ //如果布尔表达式1的值为true执行代码}else if(布尔表达式2){ //如果布尔表达式2的值为true执行代码}else if(布尔表达式3){ //布尔表达式3的值为true执行代码}else{ //如果以上布尔表达式的值都不为true执行代码}
package com.shusheng01.struct;import java.util.Scanner;public class IfDemo03 { public static void main(String[] args) { /*考试分数大于60及格 考试分数小于60不及格 考试分数大于80优秀 考试分数小于40差劲 */ Scanner scanner = new Scanner(System.in); System.out.println("请输入成绩"); int score = scanner.nextInt(); if (score>80){ System.out.println("优秀"); }else if (score>60){ System.out.println("及格"); }else if (score==60){ System.out.println("不及格"); }else if (score>=0 && score<=40){ System.out.println("叫家长"); } scanner.close(); /*请输入成绩33叫家长Process finished with exit code 0 */ }}