- 用户交互Scanner
- Scanner对象
- next&nextLine
- 其他方法
- 顺序结构
- 选择结构
- if单选择结构
- if双选择结构
- if多选择结构
- switch多选择结构
- 循环结构
- while循环
- do...while循环
- for循环
- 增强for循环
- break&continue
- break关键字
- continue关键字
- 两者的区别
Scanner对象
创建Scanner对象的基本语法:
Scanner scanner = new Scanner(System.in);next&nextLine
next方法接收输入数据:
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Next方式接收"); if (scanner.hasNext()){ String str = scanner.next(); System.out.println("输入内容:"+str); } scanner.close(); }
nextLine方法接收数据:
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(); }
两者区别:
next():
- 一定要读取到有效字符后才可以结束输入。
- 对输入有效字符之前遇到的空白,next()方法会自动将其去掉。
- 只对输入有效字符后才将其后面输入的空白作为分隔符或者结束符。
- next()不能得到带有空格的字符串。
nextLine():
- 以Enter为结束符,也就是说nextLine()方法返回的是输入回车之前的所有字符。
- 可以获的空白。
如果要输入int或float类型的数据要使用hasNextXxx来验证,nextXxx来读取。
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(); }
实例:实现输入多个数组,并求其总和与平均数,每输入一个数组回车确认,通过输入非数字来结束输入并输出执行结果。
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++; sum += x; } System.out.println(m + "个数的和为" + sum); System.out.println(m + "个数的平均值是" + (sum/m)); scanner.close(); }顺序结构
Java的基本结构就是顺序结构。
public static void main(String[] args) { System.out.println("JAVA1"); System.out.println("JAVA2"); System.out.println("JAVA3"); System.out.println("JAVA4"); System.out.println("JAVA5"); }选择结构
if单选择结构
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入内容:"); String s = scanner.nextLine(); if (s.equals("Hello")){ System.out.println("输入的是:"+ s); } System.out.println("End"); scanner.close(); }if双选择结构
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入成绩:"); int score = scanner.nextInt(); if (score >= 60){ System.out.println("及格"); }else { System.out.println("不及格"); } scanner.close(); }if多选择结构
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入成绩:"); int score = scanner.nextInt(); if (score == 100){ System.out.println("恭喜满分"); }else if (score < 100 && score >= 90){ System.out.println("A级"); }else if (score < 90 && score >= 80){ System.out.println("B级"); }else if (score < 80 && score >= 70){ System.out.println("C级"); }else if (score < 70 && score >= 60){ System.out.println("D级"); }else if (score < 60 && score >= 0){ System.out.println("不及格"); }else { System.out.println("成绩输入不合法!"); } scanner.close(); }switch多选择结构
switch case语句有如下规则:
- switch语句中的变量类型可以是:byte,short,int或者char。从JavaSE7开始,switch支持字符串String类型了,同时case标签必须为字符串常量或字面量。
- switch语句可以拥有多个case语句。每个case后面跟一个要比较值和冒号。
- case语句中的值的数据类型必须与变量的数据类型相同,而且只能是常量或者字面常量。
- 当变量的值与case语句的值相等时,那么case语句之后的语句才开始执行,直到break语句出现才会跳出switch语句。
- 当遇到break语句时,switch语句终止。程序跳转到switch语句后面的语句执行。case语句不必须咬包含break语句。如果没有break语句出现,程序会继续执行下一条case语句,直到出现break语句。
- switch语句可以包含一个default分支,该分支一般是switch语句的最后一个分支(可以在任何位置,但建议在最后一个)。default在没有case语句的值和变量值相等的时候执行。default分支不需要break语句。
switch case 执行时,一定会先继续匹配,匹配成功返回当前case的值,在根据是否有break,判断是否继续输出,或是跳出判断。
public static void main(String[] args) { char grade = 'C'; switch (grade){ case 'A': System.out.println("优秀"); break; case 'B': case 'C': System.out.println("良好"); break; case 'D': System.out.println("及格"); break; case 'F': System.out.println("你需要在继续努力努力"); break; default: System.out.println("未知等级"); } System.out.println("你的等级是 " + grade); }
如果case语句块中没有break语句时,匹配成功后,从当前case开始,后续所有case的值都会输出。如果后续的case语句快有break语句则会跳出判断。【case穿透】
public static void main(String[] args) { int i = 1; switch (i){ case 0: System.out.println("0"); case 1: System.out.println("1"); case 2: System.out.println("2"); case 3: System.out.println("3"); break; default: System.out.println("default"); } }
【JDK7增加了字符串表达式】
public static void main(String[] args) { String name = "波涛"; switch (name){ case "小涛": System.out.println("小涛"); break; case "波涛": System.out.println("波涛"); break; default: System.out.println("没有匹配项目"); } }循环结构
Java中有三种主要的循环结构:
- while循环
- do…while循环
- for循环
语法格式如下:
while (布尔表达式){ //循环内容 }
方式有:循环内部控制,外部设立标志位!
public static void main(String[] args) { int i = 0; while (i < 100){ i++; System.out.println(i); } }do…while循环
do…while循环和while循环相似,不同的是,do…while循环至少会执行一次。
语法格式如下:
do { //代码语句 }while (布尔表达式);
While和do-while的区别:
public static void main(String[] args) { int a = 0; while (a<0){ System.out.println(a); a++; } System.out.println("-------------"); do { System.out.println(a); a++; }while (a<0); }for循环
语法格式如下:
for (初始化;布尔表达式;更新){ //代码语句 }
关于for循环有以下几点说明:
- 最先执行初始化步骤。可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。
- 然后,检测布尔表达式的值。如果为true,循环体被执行。如果为false,循环终止,开始执行循环体后面的语句。
- 执行一次循环后,更新循环控制变量(迭代因子控制循环变量的增减)。
- 再次检测布尔表达式。循环执行上面的过程。
【for循环输出】
public static void main(String[] args) { for (int i = 1;i <= 100;i++){ System.out.println(i); } System.out.println("for循环结束"); }增强for循环
语法格式如下:
for(声明语句:表达式){ //代码语句 }
声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。
表达式:表达式是要访问的数组名,或是返回值为数组的方法。
【增强for循环遍历输出数组元素】
public static void main(String[] args) { int[] numbers = {10,20,30,40,50}; for (int x : numbers) { System.out.print(x); System.out.print(","); } System.out.println("n"); String[] names={"James","Larry","Tom","Lacy"}; for (String name : names) { System.out.print(name); System.out.print(","); } }break&continue
break关键字
break主要用在循环语句或者switch语句中,用来跳出整个语句块。
break跳出最里层的循环,并且继续执行该循环下面的语句。
【跳出循环】
public static void main(String[] args) { int i = 0; while (i < 100){ i++; System.out.println(i); if (i == 30){ break; } } }continue关键字
continue适用于任何循环控制结构中。作用是让程序立刻跳转到下一次循环的迭代。
在for循环中,continue语句使程序立即跳转到更新语句。
在while或者do…while循环中,程序立即跳转到布尔表达式的判断语句。
public static void main(String[] args) { int i = 0; while (i < 100){ i++; if (i % 10 == 0){ System.out.println(); continue; } System.out.println(i); } }两者的区别
break在任何循环语句的主体部分,均可用break控制循环的流程。break用于强行退出循环,不执行循环剩余的语句。
continue语句用在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。