三大语句(顺序、选择和循环)
一、顺序结构
从上往下,依次执行。
二、选择结构(if\switch)
1.if语句
if(比较表达式或者是boolean类型的值)
{
语句体;
}
执行流程:
先计算比较表达式的值,看其返回值是true还是false。
如果是true,就执行语句体;
如果是false,就不执行语句体;
注意的点:
- 三元运算符实现的,都可以采用if语句实现,反之不成立。
- 当只要有一个条件满足时,if语句结束。else是可以省略,但是不建议省略。
2.switch语句
switch(表达式){
case 值1:
语句体1;
break;
case 值2:
语句体2;
break;
....
default:
语句体n+1;
break;
}
注意的点:
- 表达式的取值:byte,short,int,char,JDK5以后可以是枚举),JDK7以后可以是String。
- default语句表示所有情况都不匹配的时候,就执行该处的内容。
- break表示中断,结束的意思,可以结束switch语句。
- break省略会出现一个现象:case穿透。
- default可以在任意位置。但是建议在最后。
3.两种语句的选择:
在做判断的时候,我们有两种选择,if语句和switch语句,那么,我们到底该如何选择使用那种语句呢?
if语句使用场景:
-
针对结果是boolean类型的判断
-
针对一个范围的判断
-
针对几个常量值的判断
switch语句使用场景:
- 针对几个常量值的判断
三、循环结构(for\while\do~while)
1.for循环
for(初始化表达式语句;判断条件语句;控制条件语句) {
循环体语句;
}
执行流程:
a:执行初始化表达式语句
b:执行判断条件语句,看其返回值是true还是false
如果是true,就继续执行
如果是false,就结束循环
c:执行循环体语句;
d:执行控制条件语句
e:回到开始继续。
2.while循环
while(判断条件语句) {
循环体语句;
控制条件语句;
}
执行流程:
a:执行初始化条件语句;
b:执行判断条件语句,看其返回值是true还是false
如果是true,就继续执行
如果是false,就结束循环
c:执行循环体语句;
d:执行控制条件语句
e:回到开始继续。
3.do…while语句
do {
循环体语句;
}while(判断条件语句);
完整格式;
初始化条件语句;
do {
循环体语句;
控制条件语句;
}while(判断条件语句);
执行流程:
a:执行初始化条件语句;
b:执行循环体语句;
c:执行控制条件语句;
d:执行判断条件语句,看其返回值是true还是false
如果是true,就继续执行
如果是false,就结束循环
e:回到开始继续。
4.循环结构三种循环语句的区别
-
do…while循环至少执行一次循环体。 而for,while循环必须先判断条件是否成立,然后决定是否执行循环体语句。
-
如果你想在循环结束后,继续使用控制条件的那个变量,用while循环,否则用for循环。不知道用for循环。因为变量及早的从内存中消失,可以提高内存的使用效率。
-
建议优先考虑for循环,然后是while循环 ,最后是do…while循环
5.死循环语句
while(true){...}
for( ; ; ){...}
6.优化代码
'\x' x表示任意,这种做法叫转义字符。
'\t' tab键的位置
'\n' 换行
7.跳出(终止)语句
- return: 结束当前方法
- break: 结束单层循环
- continue: 结束当前循环,执行下一次循环
小试牛刀
1:
import java.util.Scanner;
public class MyDemo3 {
public static void main(String[] args) {
//整数(给定一个值输出对应的星期一,星期二,星期三…星期日)
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个整数,1--7");
int num = sc.nextInt();
switch (num){
case 1:
System.out.println("星期一");
break;
case 2:
System.out.println("星期2");
break;
case 3:
System.out.println("星期3");
break;
case 4:
System.out.println("星期4");
break;
case 5:
System.out.println("星期5");
break;
case 6:
System.out.println("星期6");
break;
case 7:
System.out.println("星期7");
break;
default:
System.out.println("莫得东西");
break;
}
}
}
2:
import java.util.Scanner;
public class MyTest {
public static void main(String[] args) {
//键盘录入三个数,使用if else 语句求出三个数的最大值
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个数字");
int num1=sc.nextInt();
System.out.println("请输入第二个数字");
int num2 = sc.nextInt();
System.out.println("请输入第三个整数");
int num3 = sc.nextInt();
//使用if else 来求出三个数的最大值
int max=0;//定义一个变量,用来接收最大值
//if else 语句可以嵌套使用
if(num1>num2){
if(num1>num3){
max=num1;
}else{
max=num3;
}
}else{
if(num2>num3){
max=num2;
}else{
max=num3;
}
}
System.out.println("最大值是:"+max);
}
}
3:
public class MyTest5 {
public static void main(String[] args) {
// if 语句的多重嵌套
// 需求:对成绩进行优良中差的判别
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的成绩 0---100");
int score = sc.nextInt();
if(score<60&&score>=0){
System.out.println("成绩不及格");
}else if(score <80 && score >=60){
System.out.println("差");
}else if(score < 90 && score >=80){
System.out.println("中");
}else if(score < 95 && score >= 90){
System.out.println("良好");
}else if(score <=99 && score >= 95){
System.out.println("优秀");
}else if(score==100){
System.out.println("满分");
}else{
System.out.println("成绩不合法,你是乱输的");
}
}
}
4:
public class ForDemo4 {
public static void main(String[] args) {
//需求:求出1 - 100 之间偶数和
//需求:求出1 - 100 之间奇数和
// 10.fori 快速生成循环语句
int ou=0;
int ji=0;
for (int j = 1; j <= 100; j++) {
//在循环里面得判断 j 是偶数还是奇数
if(j%2==0){
//System.out.println(j);
ou=ou+j; //ou+=j
}else{
ji+=j;
}
}
System.out.println("偶数和: "+ou);
System.out.println("奇数和: " +ji);
}
}
5:
public class ForDemo5 {
public static void main(String[] args) {
//需求:在控制台输出所有的”水仙花数”
//所谓的水仙花数是指一个三位数,其各位数字的立方和等于该数本身。
//153 就是一个水仙花数。
//153 = 1 * 1 * 1 + 5 * 5 * 5 + 3 * 3 * 3 = 1 + 125 + 27 = 153
//水仙花数有几个
int count=0;
for (int i = 100; i <= 999; i++) {
//提取每个位上的数字
int ge = i / 1 % 10;
int shi = i / 10 % 10;
int bai = i / 100 % 10;
int flowerNum = ge * ge * ge + shi * shi * shi + bai * bai * bai;
if (i == flowerNum) {
System.out.println("水仙花数是: " + i);
count++; //统计一下
}
}
System.out.println("水仙花数有 "+count+" 个");
}
}
6:
public class WhileDemo3 {
public static void main(String[] args) {
//求1---100之间的偶数和 奇数和
int i = 1;
int ou = 0;
int ji = 0;
while (i <= 100) {
if (i % 2 == 1) {
ji += i;
} else {
ou += i;
}
i++;
}
System.out.println("偶数和是" + ou);
System.out.println("奇数和是" + ji);
}
}
7:
public class WhileDemo4 {
public static void main(String[] args) {
//求水仙花数
int i=100;
int count=0;
while (i<1000){
//获取出每个位上的数字
int ge=i/1%10;
int shi=i/10%10;
int bai=i/100%10;
int flowerNumber=ge*ge*ge+shi*shi*shi+bai*bai*bai;
if(i==flowerNumber){
System.out.println("水仙花数是"+i);
count++;
}
i++;
}
System.out.println("水仙花数有"+count+"个");
}
}
8:
public class MyTest {
public static void main(String[] args) {
/*
* 需求:请输出一个4行5列的星星(*)图案。如图:
*****
*****
*****
*****
* */
//有两层循环嵌套时,外层循环控制行数 里层循环控制列数
for (int i = 0; i <4; i++) {
for (int j = 0; j <5; j++) {
System.out.print("*"); //打印不换行
}
System.out.println();//只是换行
}
}
}
9:
public class MyTest2 {
public static void main(String[] args) {
/** 需求:请输出下列的形状
*
**
***
****
*****
**/
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <=i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
10:
public class MyTest3 {
public static void main(String[] args) {
/*乘法表
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
*/
for (int i =1; i <=9; i++) {
for (int j =1; j <=i; j++) {
System.out.print(j+"*"+i+"="+(j*i)+"\t");
}
System.out.print("\n");
}
}
}
11:
public class MyTest4 {
public static void main(String[] args) {
//1.我想在控制台输出2次:“Java“
//2.我想在控制台输出7次:“Java“
//3.我想在控制台输出13次:“Java“
for (int x = 1; x <= 10; x++) {
if (x % 3 == 0) {
//在此处填写代码
// break;
//continue;
System.out.println("Java");
}
System.out.println("Java");
}
}
}