Java流程控制
Java流程控制
1. Scanner
1.1 用户交互Scanner
-
我们可以通过Scanner类来获取用户的输入
-
基本语法:
Scanner s = new Scanner(System.in);
-
通过Scanner类的next()与nextLine()方法获取输入的字符串,在读取前我们一般需要 使用hasNext() 与 hasNextLine() 判断是否还有输入的数据
- next()不能得到带有空格的字符串
- nextLine()可以得到带有空格的字符串
package com.xie.scanner; import java.util.Scanner; public class Demo01 { public static void main(String[] args) { //创建一个扫描器对象,用于接收键盘数据 Scanner scanner = new Scanner(System.in); //判断用户有没有输入字符串 if(scanner.hasNext()){ //使用next方式接收 String str = scanner.next(); System.out.println("输出内容为:"+str); } //凡是属于IO流的类如果不关闭会一值占用资源。要习惯用完就关掉 scanner.close(); } }
1.2scanner进阶使用
-
案例
输入数字,并求其总和和平均数
package com.xie.scanner; import java.util.Scanner; 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; sum = sum + x; System.out.println("你输入了第" + m + "个数据,当前的和为" + sum + "。当前的平均数为" + (sum / m)); } System.out.println(m + "个数的和为" + sum); System.out.println(m + "个数的平均值是" + (sum / m)); scanner.close(); } }
2. 语言结构
1.1 顺序结构
- java的基本结构,按照一句一句的执行
- 最简单的算法结构
- 任何一个算法都离不开的基本算法结构
1.2 选择结构
1.2.1 If选择结构
-
if单选则结构
语法:if(布尔表达值){
//如果为true将执行的语句
}
-
if双选则结构
语法:if(布尔表达式){
//如果布尔表达式为true将执行的语句
}
else{
//如果布尔表达式为false将执行的语句
}
-
if多选择结构
多个else if
package com.xie.struct; import java.util.Scanner; public class IfDemo01 { 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 >=80){ System.out.println("优秀"); } else if (score > 60){ System.out.println("及格"); } else{ System.out.println("不及格"); } scanner.close(); } } -
if嵌套结构
在另一个if或else中使用if或else语句
1.2.2 switch选择结构
swich(expression)
case value:
//语句
break;//可有可无
case value:
//语句
break;
//可以有任意数量的case语句
defualt://可有可无
//语句
-
例子
package com.xie.struct; public class SwitchDemo01 { public static void main(String[] args) { //case穿透 char grade = 'a'; switch(grade){ case 'a': System.out.println("优秀"); case'b': System.out.println("良好"); case'c': System.out.println("及格"); case'd': System.out.println("挂科"); default: System.out.println("未知等级"); } } } -
switch支持String类型
package com.xie.struct; public class SwitchDemo02 { public static void main(String[] args) { String name = "狂神";//JDK7的新特性,表达结果可以是字符串 switch(name){ case "秦疆": System.out.println("秦疆"); break; case"狂神": System.out.println("狂神"); break; default: System.out.println("弄啥来"); } } }
1.3 循环结构
1.3.1 while循环
-
先判断后执行
-
例子
计算1~100相加
package com.xie.struct; public class WhileDemo01 { public static void main(String[] args) { int n = 0; int m = 0; while(n < 100){ n = n+1; m = m + n; } System.out.println(m); } }
1.3.2 do...while 循环结构
-
即使不满足条件也要执行一次(先执行后判断)
package com.xie.struct; public class DoWhileDemo02 { 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);//输出0 a++; } while(a < 0); } }
1.3.3 for循环
-
for循环是支持迭代的一种通用结构,值最有效、最灵活的循环结构
-
for循环执行的次数是在执行前就确定的。
-
语法格式:
for(初始化;布尔表达式;更新){
//代码语句
}
-
例子
package com.xie.struct; public class ForDemo01 { public static void main(String[] args) { int a = 1;//初始化条件 while(a <= 100){//条件判断 System.out.println(a);//循环体 a = a + 2;//迭代(a += 2) } System.out.println("while循环结束"); //初始化//条件判断//迭代 for(int b=1;b<=100;b++){ System.out.println(b); } System.out.println("for循环结束"); } }习题1:计算0到100之间的奇数和偶数的和
package com.xie.struct; public class ForDemo02 { public static void main(String[] args) { int m = 0; int n = 0; for (int i = 1; i < 100; i++) { if(i%2 != 0) { m = m + i; } else{ n = n + i; } } System.out.println(m);//2500 System.out.println(n);//2450 } }习题2:用while或for循环输出1~1000之间能被5整除的数,并且每行输出3个
- print输出完 不会换行
- println输出完 会换行
- ”\t“ 转移字符,不换行时每个输出之间会有空格
- ”\n“ 换行符
package com.xie.struct; public class ForDemo03 { public static void main(String[] args) { for (int i = 0; i <= 1000; i++) { if(i%5 == 0){ System.out.print(i + "\t"); } if(i%15 == 0){ System.out.println(); //System.out.print("\n"); } } } }习题3:打印九九乘法表
- 要想输出公式,可以把 计算符号 用 ” “ 括起来,再用 加号 链接起来 各变量
package com.xie.struct; public class EorDemo04{ public static void main(String[] args) { for (int i = 1; i < 10; i++) { for(int a = 1;a <= i;a++){ int m = a*i; System.out.print(a + "*" + i + "=" + m + "\t"); } System.out.println(); } } }11=1
12=2 22=4
13=3 23=6 33=9
14=4 24=8 34=12 44=16
15=5 25=10 35=15 45=20 55=25
16=6 26=12 36=18 46=24 56=30 66=36
17=7 27=14 37=21 47=28 57=35 67=42 77=49
18=8 28=16 38=24 48=32 58=40 68=48 78=56 88=64
19=9 29=18 39=27 49=36 59=45 69=54 79=63 89=72 9*9=81 -
增强for循环(用来简化)
-
主要用于数组或集合对象
-
语法格式:
for(声明语句:表达式){ //代码语句 } -
声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与数组元素的值相等。
-
表达式:表达式是要访问的数组名,或者是返回值的方法
package com.xie.struct; public class ForDemo04 { public static void main(String[] args) { int[] numbers = {10,20,30,40,50};//定义了一个数组 for(int x:numbers){ System.out.println(x); } } }
-
1.4break,continue,gote
-
break在任何循环语句的主题部分,均可用break控制循环的流程。break用于强行退出循环,不执行循环中剩余的语句。
package com.xie.struct; public class BreakDemo01 { public static void main(String[] args) { int i = 0; while(i <= 100){ i++; System.out.println(i); if(i == 30){ break; } } } } -
continue终止某次循环,进行下一次循环
package com.xie.struct; public class ContinueDemo01 { public static void main(String[] args) { int i = 0; while(i <100){ i++; if(i%10 == 0){ System.out.println(); continue; } System.out.print(i+"\t"); } } } -
goto是的Java的一个保留字,但并未在语言中得到正式使用,带标签的brek,continue
- 标签是指后面跟一个冒号的标识符。例如:label:
- break和continue如果伴随标签使用,他们就会中断到存在标签的地方
- 不建议使用
package com.xie.struct; public class LabelDemo01 { public static void main(String[] args) { //打印100~150之间的所有质数 int count = 0; outer:for (int i = 100; i < 150; i++) { for(int j = 2;j < i/2;j++){ if(i%j == 0){ continue outer; } } System.out.print(i + "\t"); } } }
习题:打印三角型及Debug
package com.xie.struct;
public class TextDemo01 {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >= i; j--) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
for(int j = 1; j < i; j++){
System.out.print("*");
}
System.out.println();
}
}
}

浙公网安备 33010602011771号