1、单if语句
表达格式:
if(关系表达式){
语句体;
}
2、if…else if…else语句
表达格式:
if(判断条件1){
执行语句1;
}else if(判断条件2){
执行语句2;
}
……
}else if(判断条件n){
执行语句n;
}else{
执行语句n+1;
}
3、switch语句使用的注意事项:
(1)多个case后面的数值不可以重复。
(2)switch后面小括号当中只能是下列数据类型:
基本数据类型:byte/short/char/int
引用数据类型:String字符串、emum枚举
(3)switch语句格式可以很灵活:前后顺序可以颠倒,而且break语句还可以省略。“匹配哪一个case就从哪一个位置向下执行,直到遇到了break或者整体结束为止。”
4、for循环语句
表达格式:
for(初始化表达式;布尔表达式;步进表达式){
循环体;
}
5、循环结构的基本组成部分,一般可以分成四部分:
(1)初始化语句:在循环开始最初执行,而且只做唯一一次。
(2)条件判断:如果成立,则循环继续;如果不成立,则循环退出。
(3)重复要做的事情内容,若干行语句。
(4)步进语句:每次循环之后都要进行的扫尾工作,每次循环结束之后都要执行一次。
6、while循环语句
表达格式:
初始化表达式
while(布尔表达式){
循环体
步进表达式
}
7、do-while循环语句
标准格式:
do{
循环体
}while(条件判断);
扩展格式:
初始化语句
do{
循环体
步进语句
}while(条件判断);
8、break关键字的用法有常见的两种:
(1)可以用在switch语句当中,一旦执行,整个switch语句立刻结束。
(2)还可以用在循环语句当中,一旦执行,这个循环语句立刻结束。打断循环。
建议:
凡事次数确定的场景多用for循环;否则多用while循环。
运行代码1:
public class one{
public static void main(String[] args){
System.out.println(“今天天气不好”);
System.out.println(“下了一天的雨”);
System.out.println(“这不舒服”);
}
}
运行代码2:
public class two{
public static void main(String[] args){
System.out.println(“今天运气真不错,在压马路的时候……突然发现一个好地方:网吧”);
int ght=20;
if(ght>=28);{
System.out.println(“进入网吧开始玩游戏!”);
System.out.println(“遇到一群猪队友,开始和他们对骂。”);
System.out.println(“感觉心情不爽,结账走人。”);
}
System.out.println(“跑回家干饭!!!”);
}
}
运行代码3:
public class three{
public static void main(String[] args){
int num=444;
if (num % 2 == 0){
System.out.println(“偶数”);
} else {
System.out.println(“奇数”);
}
}
}
运行代码4:
public class four{
public static void main(String[] args){
int x = -1;
int y;
if (x>=3){
y=2x+1;
}else if(-1<x && x<3){
y=2x;
} else {
y=2*x-1;
}
System.out.println(“最大值:” +y);
}
}
运行代码5:
public class five{
public static void main(String[] args){
int a=60;
int b=30;
int bor1=a>b?a:b;
System.out.println(“最大值:” +bor1);
int max;
if (a>b){
max=a;
}else{
max=b;
}
System.out.println(“最大值:” +max);
}
}
运行代码7:
public class seven{
public static void main(String[] args){
int num=3;
switch (num){
case 1:
System.out.println(“你好”);
break;
case 2:
System.out.println(“你听”);
break;
case 3:
System.out.println(“你看”);
break;
default:
System.out.println(“不好,不听,不看”);
break;
}
}
}
运行代码6:
public class six{
public static void main(String[] args){
int day=18;
int num=day%7;
switch (num){
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 0:
System.out.println(“星期日”);
break;
default:
System.out.println(“数据不合理”);
break;
}
}
}
运行代码8:
public class eight{
public static void main(String[] args){
int sum=0;
for (int i=200;i<=100;i++){
sum+=1;
}
System.out.println(“程序停止” +sum);
}
}
运行代码9:
public class nine{
public static void main(String[] args){
for (int i=1;i<=5;i++){
System.out.println(“我错了” +i);
}
System.out.println("");
int i=1000;
while (i<=10){
System.out.println(“我错了” +i);
i++;
}
}
}
运行代码10:
public class t333{
public static void main(String[] args){
for (int i=1;i<=5;i++){
System.out.println(“起来吧!” +i);
}
System.out.println("=");
int i=1000;
do{
System.out.println(“起来吧!” +i);
i++;
}while(i<=10);
System.out.println(i);
}
}
运行代码11:
public class t444{
public static void main(String[] args){
for (int i=1;i<=10;i++){
if (i4)
{
break;
}
System.out.println(“Hello” +i);
}
}
}
运行代码12:
public class t555{
public static void main(String[] args){
for (int i=1;i<=10;i++){
if(i4)
continue;
System.out.println(i+ “完结”);
}
}
}
运行结果如下:
2021-03-16
最新推荐文章于 2023-07-30 00:17:17 发布