Java流程控制
-
通过scanner类来获取用户的输入
-
基本语法
Scanner S = new Scanner(System.in);
-
通过scanner类的next()与nextLine()方法输入的字符串,在读取前我们一般需要使用hasNext()与hasNextLine()判断是否还有输入的数据
next()方法
//先打new Scanner(System.in);再按Alt+Enter键,再按Enter键,即可得出下面第一句
Scanner scanner = new Scanner(System.in); //创建一个扫描器的对象,用于接收键盘数据
System.out.println("使用next方式接收:");
if (scanner.hasNext()){ //判断用户有没有输入字符串
//不用if语句也可以得到输出的内容
String str = scanner.next(); //使用next方式来接收,程序会等待用户输入完毕
System.out.println("输出的内容为:"+str);
}
scanner.close(); //凡是使用IO流的类如果不关闭会一直占用资源
-
一定要读取到有效字符之后才可以结束输入
-
若输入的有效字符中含有空格,会将空格前的保留,空格后的自动将其去掉,所以next()不能得到带有空格的字符串
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();
-
nextLine()可以得到带有空格的字符串
注意:凡是使用IO流的类如果不关闭会一直占用资源,要及时关闭
顺序结构
-
Java的基本结构就是顺序结构,除非特别指明,否则就按照顺序一句一句执行。
-
顺序结构是最简单的算法结构
-
语句和语句之间,框与框之间是按照从上到下的顺序进行的,它是由若干个依次执行的处理步骤组成的,它是任何一个算法都离不开的一种基本算法结构
选择结构
if单选择结构
-
判断一个东西是否可行,然后再去执行,这样一个过程在程序中用if语句来表示
-
语法:
if(布尔表达式){
//如果布尔表达式为true将执行的语句
}
例如:
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine(); //将字符串s设置为变量
if (s.equals("Hello")){ //equals:判断字符串是否相等
System.out.println(s);
}
System.out.println("End");
scanner.close();
/*若输入hello,则输出为End
若输入为Hello,则输出为
Hello
End*/
if双选择结构
语法:
if(布尔表达式){
//如果布尔表达式的值为true
}else{
//如果布尔表达式的值为false
}
例如:
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩:");
int score = scanner.nextInt(); //将整数score设置为变量
if (score>60){
System.out.println("及格");
}else {
System.out.println("不及格");
}
scanner.close();
if多选择结构
例如:
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩:");
int score = scanner.nextInt();
if (score==100){
System.out.println("Congratulations!");
}else if (score<100 && score>=80){
System.out.println("Nice!");
}else if (score<80 && score>=60){
System.out.println("Good!");
}else if (score<60 && score>=0){
System.out.println("You need to study hard");
}else{
System.out.println("error");
}
scanner.close();
注意:
-
if语句最多有一个else语句,else语句在所有的else if语句之前,else语句可以有若干个
-
一旦其中一个else if语句检测为true,其他的else if语句以及else语句将跳过执行
嵌套的if结构
-
使用嵌套的 if else 语句是合法的,也就是说可以在另一个 if 或者 else if 语句中使用 if 或 else if 语句
语法:
if(布尔表达式1){
////如果布尔表达式1的值为true执行代码
if(布尔表达式2){
////如果布尔表达式的值为true执行代码
}
}
switch多选择 结构
-
多选择结构还有一个实现方式就是switch语句
-
switch case语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支
语法:
switch(expression){
case value:
//语句
break;//可选
case value:
//语句
break;//可选
//可以有任意数量的case语句
default:
//语句
}
例如:
char grade = 'A';
switch(grade){
case 'A':
System.out.println("优秀");
break;
case 'B':
System.out.println("良好");
break;
case 'C':
System.out.println("及格");
break;
case 'D':
System.out.println("加油");
break;
case 'E':
System.out.println("挂科");
break;
default:
System.out.println("未知等候");
}
//输出结果为:优秀
-
switch语句中的变量类型可以是:
-
byte,short,int或者char
-
switch支持字符串String类型
-
case标签必须为字符串常量或字面量
-
String name = "白";
switch (name){
case "黑":
System.out.println("black");
break;
case "白":
System.out.println("white");
break;
default:
System.out.println("green");
}
//输出结果为:white
循环结构
while循环
-
whlie是最基本的循环,结构:
while(布尔表达式){
//循环内容
}
例如:
int i = 0;
while(i<50){
i++;
System.out.println(i);
//输出数据为:1~50所有数字
-
只要布尔表达式为true,循环就会一直执行下去
-
我们大多数情况会让循环停止下来的,我们需要一个让表达式失效的方式来结束循环
-
少部分情况需要循环一直执行,如发武器的请求响应监听
-
循环条件一直为true就会造成无限循环(死循环)我们正常的业务编程中应该尽量避免死循环,会影响程序性能或者造成程序卡死崩溃
例如:计算从1加到100总和
int sum = 0;
int i = 0;
while(i<=100){
sum = sum + i;
i++;
System.out.println(sum);
do...while循环
while与do...while区别:
-
while语句,如果不满足条件,则不能进入循环,但do...while语句即使不满足条件,也至少执行一次
-
while先判断后执行,do...while先执行后判断(do...while总是保证循环体至少被执行一次)
例如:
int a=0;
while(a<0){
System.out.println(a);
a++;
}
do{
System.out.println(a);
a++;
}while(a<0);
//输出结果为0
For循环
-
for循环语句是支持迭代的一种通用结构,是最有效,最灵活的循环结构
-
for循环执行次数是在执行前就确定的。
-
语法:
for(初始化;布尔表达式;更新){
//代码语句
}
例题:
-
计算0到100之间奇数的和以及偶数的和
int oddSum = 0;
int evenSum = 0;
for (int i = 0; i < 100; i++) {
if (i % 2 != 0) {
oddSum += i;
} else {
evenSum += i;
}
}
System.out.println("奇数的和"+oddSum);
System.out.println("偶数的和"+evenSum);
//输出结果:
奇数的和2500
偶数的和2450
-
输出1~1000之间能被5整除的奇数和偶数,并且每行输出3个
for (int i = 1; i <= 1000; i++) {
if(i % 5 == 0){
System.out.print(i+"\t");
}
if(i % 15 == 0){
System.out.println();
}
}
//输出结果自己实验吧
九九乘法表
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j+"*"+i+"="+i*j+"\t");
}
System.out.println();
}
//输出:
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=