if(判断语句){
执行语句
}if(判断语句){
执行语句
}if(判断语句){
执行语句
}else{
执行语句
}
前面的if只要满足条件就会执行,最后的else只要前一个if不满足条件就会执行
if(判断语句){
执行语句
}else if(判断语句){
执行语句
else if(判断语句){
执行语句
}else{
执行语句
}
只要前面有一个条件被满足后面的所有的else语句就不会执行
if else判断最大值,最小值,中间值
import java.util.Scanner;
public class Demo03IfElseMax {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("输入i:");
int i = sc.nextInt();
System.out.print("输入j:");
int j = sc.nextInt();
System.out.print("输入k:");
int k = sc.nextInt();
if (i > j) {
if (i > k) {
System.out.println("max=i=" + i);
} else {
System.out.println("max=k=" + k);
}
} else if (i > k) {
if (i > j) {
System.out.println("max=i=" + i);
} else {
System.out.println("max=j=" + j);
}
} else {
if (j > k) {
System.out.println("max=j=" + j);
} else {
System.out.println("max=k=" + k);
}
}
}
}
输入i:8
输入j:10
输入k:2
max=j=10
switch 语句
注意:
switch(值){
case 值:
执行语句;
break;
case 值:
执行语句;
break;
case 值:
执行语句;
break;
default: 执行语句;
}
break的作用:从当前跳出switch语句
default的作用:如果没有符合case的值就执行default后的语句
for循环
fot(初始化条件;条件判断;步进语句){
循环体
}
执行顺序:初始化条件->条件判断->循环体->步进语句->条件判断->循环体->步进语句->条件判断->…
初始化条件,条件判断,步进语句都可以省略,但没必要
for循环应用
public class Demo07For99 {
public static void main(String[] args) {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + "*" + i + "=" + i * j);
System.out.print("\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=32 5*8=40 6*8=48 7*8=56 8*8=64 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
while循环
语法
while(条件语句){
循环体
}
break的作用:终止循环
do while循环
语法
do{
循环体
}while(条件语句)
先执行一遍循环体的内容,再判断条件语句是否满足
循环总结
本文地址:https://blog.csdn.net/weixin_44115522/article/details/107361580
如对本文有疑问, 点击进行留言回复!!
[杭电多校2020]第一场 1004 Distinct Sub-palindromes
Swift -- 将本地生成的UIImage进行持久化保存(存到文件中fileManager.createFile)
网友评论