头歌java实训作业答案控制语句
时间: 2023-09-01 07:10:49 浏览: 114
以下是头歌Java实训作业中可能涉及到的控制语句的一些示例:
1. if语句:
```
int x = 10;
if (x > 5) {
System.out.println("x is greater than 5");
}
```
2. if-else语句:
```
int x = 10;
if (x > 15) {
System.out.println("x is greater than 15");
} else {
System.out.println("x is less than or equal to 15");
}
```
3. switch语句:
```
int day = 4;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
default:
System.out.println("Weekend");
}
```
4. for循环:
```
for (int i = 0; i < 10; i++) {
System.out.println(i);
}
```
5. while循环:
```
int i = 0;
while (i < 10) {
System.out.println(i);
i++;
}
```
6. do-while循环:
```
int i = 0;
do {
System.out.println(i);
i++;
} while (i < 10);
```
阅读全文