这节主要的学习内容:(选择控释)1.if结构;2.switch结构;(循环选择)3.while循环;4.do-while循环;5.for循环6.break语句;7.continue语句
1.if结构
1.1 多重if else语句
package com_imooc;
import java.util.Scanner;
public class Huozhe {
public static void main(String[] vvv) {
/*
90-100 优
80-90 良
60-80 中
<60 不及格
*/
System.out.print("请输入你得成绩: ");
Scanner s = new Scanner(System.in);
int n = s.nextInt();
if (n >= 90) {
System.out.println("优");
} else if (80 <= n && n < 90) {
System.out.println("良");
}else if (60 <= n && n <80){
System.out.println("中");
}else if (n < 60){
System.out.println("不及格");
}
}
}
1.2 嵌套if语句
package com_imooc;
public class Huozhe {
public static void main(String[] vvv) {
//判断int aa,bb的大小关系
int aa = 5, bb = 6;
if (aa != bb) {
if (aa > bb){
System.out.println("aa大于bb");
}else {
System.out.println("bb大于aa");
}
} else {
System.out.println("aa等于bb");
}
}
}
2.switch结构
package com_imooc;
import java.util.Scanner;
public class Huozhe {
public static void main(String[] vvv) {
//键盘输入1-7,对应出礼拜一到日
System.out.print("请输入小数字1-7: ");
Scanner s = new Scanner(System.in);
int n = s.nextInt();
switch (n) {
case 1:
System.out.println("礼拜一");
break;
case 2:
System.out.println("礼拜二");
break;
case 3:
System.out.println("礼拜三");
break;
case 4:
System.out.println("礼拜四");
break;
case 5:
System.out.println("礼拜五");
break;
case 6:
System.out.println("礼拜六");
break;
case 7:
System.out.println("礼拜日");
break;
default:
System.out.println("你超出去1-7的范围了");
}
}
}
3.while循环
基本语法
package com_imooc;
public class Xunhuan {
public static void main(String[] vvv) {
//案例一,将小于5的数字输出
int n = 0;
while (n < 5) {
System.out.println(n);
n++;
}
//案例二,求1到5的累加和
int count = 0;
while (n <= 5) {
count += n;
if (n == 5) {
System.out.println("最后的和是:" + count);
}
n++;
}
//案例三,循环26个字母,分两行输出
char ch = 'a';
int sum = 1;
while (ch <= 'z') {
System.out.print(ch + " ");
if (sum == 13){
System.out.println();
}
ch++;
sum++;
}
}
}
4.do-while循环
基本语法
package com_imooc;
import java.util.Scanner;
public class Xunhuan {
public static void main(String[] vvv) {
//do-while
//案例一,将小于5的数字输出
int m = 0;
do {
System.out.println(m);
m++;
} while (m < 5);
//案例二,求1到5的累加和
int do_m=1;
int do_count=0;
do {
do_count+=do_m;
if (do_m == 5){
System.out.println("和是:"+do_count);
}
do_m++;
}while (do_m < 6);
//案例三,猜数字
int x = 5;
boolean xx = true;
do {
System.out.print("请输入一个整数: ");
Scanner s = new Scanner(System.in);
int n = s.nextInt();
if (n > 5) {
System.out.println("大了");
} else if (n < 5) {
System.out.println("小了");
} else if (n == 5) {
System.out.println("对了");
xx = false;
}
} while (xx);
}
}
5.for循环
基本语法
package com_imooc;
import java.util.Scanner;
public class Xunhuan {
public static void main(String[] vvv) {
//for
//案例一,求1到5的累加和
int for_count = 0;
for (int a = 0; a <= 5; a++) {
for_count += a;
if (a == 5) {
System.out.println("和是:" + for_count);
}
}
//案例二,1!+2!+3!+4!+...+10!
long jia = 0,cheng = 1;
for (int n = 1; n <= 10; n++) {
for (int m = 1; m <= n; m++) {
cheng*=m;
}
jia+=cheng;
cheng=1;
}
System.out.println(jia);
}
}
6.break语句
package com_imooc;
import java.util.Scanner;
public class Xunhuan {
public static void main(String[] vvv) {
//break
//案例一,输出0时,退出循环
while (true) {
System.out.print("请输入一个整数: ");
Scanner s = new Scanner(System.in);
int n = s.nextInt();
if (n == 0) break;
}
}
}
7.continue语句
作用是:
例题: