0
点赞
收藏
分享

微信扫一扫

Java学习手册——第七篇基础语法

Java学习手册——第七篇基础语法

在这里插入图片描述

1. 注释

/**
* 文档注释
*/
public class HelloWorld {
    /* 
     * 多行注释
     */
    public static void main(String[] args) {
        // 单行注释
        System.out.println("Hello World"); 
    }
}

2. 顺序语句

public static void main(String[] args) {
	System.out.println("开始");
	System.out.println("语句1");
	System.out.println("语句2");
	System.out.println("语句3");
	System.out.println("结束");
}

3. 条件语句

3.1 if语句

在这里插入图片描述

int score = 88;
if (score < 0 || score > 100) {
    System.out.println("成绩有问题");
} else if (score >= 90 && score <= 100) {
    System.out.println("优秀");
} else if (score >= 80 && score < 90) {
    System.out.println("良好");
} else if (score >= 70 && score < 80) {
    System.out.println("良");
} else if (score >= 60 && score < 70) {
    System.out.println("及格");
} else {
    System.out.println("不及格");
}

3.2 switch语句

int weekday = 7;

switch(weekday) {
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("你输入的数字有误");
	break;
}

switch里面经常用到的有break语句,
记住它的主要用法即可:
跳出当前整个switch语句,立马进入下一条语句。

4. 循环语句

4.1 for循环

// 初始化语句;判断条件语句;控制条件语句
for(int x = 1; x <= 5; x++) {
	System.out.println(x);
}

4.2 while 语句

int x=1;
while(x <= 10) {
	System.out.println(x);
	x++;
}

4.3 do…while语句

int x=1;
do {
	System.out.println(x);
	x++;
} while(x <= 10);

循环语句里面经常用的有continue、break语句。
break语句:跳出当前整个循环。
continue语句:跳出当前语句,进入下一次循环。

// break执行之后,后面的循环都不在执行了。
for(int x=1; x<=10; x++) {
	if(x == 3) {
		break;
	}
	System.out.println(x);
}

// continue执行后,当前的循环不执行,剩下的循环继续
for(int x=1; x<=10; x++) {
	if(x == 3) {
		continue;
	}
	System.out.println(x);
}
举报

相关推荐

0 条评论