选择结构
if语法:
if(布尔表达式){
//如果布尔表达式为true
}
else{
//如果布尔表达式为false
}
if单分支结构
//判断学生成绩是否及格
import java.util.Scanner;
public class 流程控制{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩:");
double scor=scanner.nextDouble();
if (scor>=60){
System.out.println("及格");
}else{
System.out.println("不及格");
}
scanner.close();
}
}
if 多选择结构
用法和c语言一样
import java.util.Scanner;
public class 流程控制{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩:");
double scor=scanner.nextDouble();
if (scor>=90){
System.out.println("A级");
}else if(scor>=80&&scor<90){
System.out.println("B级");
}else if(scor>=70&&scor<80){
System.out.println("c级");
}else if(scor<70&&scor>=60){
System.out.println("d级");
}else{
System.out.println("不及格");
}
scanner.close();
}
}
switch 多选择结构
与c语言相同。支持int char string;
import java.util.*;
public class 流程控制{
public static void main(String[] args) {
char grad= 'b';
switch (grad){
case'a':
System.out.println("优秀");
break;
case'b':
System.out.println("及格");
break;
case'c':
System.out.println("再接再厉");
break;
case'd':
System.out.println("继续努力");
break;
default:
System.out.println("输出错误");
break;
}
}
}
循环结构
while, do…while, for
while(布尔表达式){
//循环内容
}
求1-100里面的所有数之和
int i=0;
int sum=0;
while(i<=100){
sum+=i;
i++;
}
System.out.println(+sum);
}
}
5050
Process finished with exit code 0
for循环
for循环语句支持迭代的一种通用结构,是最有效,最灵活的循环结构。
for语法
for(初始化;布尔表达式;更新){
//代码语句
}
练习一:计算0到100之间的奇数和偶数的和
练习二:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个
练习三:打印九九乘法表
//***练习一:计算0到100之间的奇数和偶数的和***
public class 流程控制 {
public static void main(String[] args) {
int sum=0;
for(int i=0;i<=100;i+=2){
sum+=i;
}
System.out.println(+sum);
}
}
}
//练习二:***用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个***
public class enen {
public static void main(String[] args) {
int t=0;
for(int a=5;a<=1000;a++){
if(a%10==5||a%10==0){
System.out.println(+a);
t++;
if(t==3){
System.out.println("\n");
t=0;
}
}
}
}
}
println()
输出完全会换行
print()
输出不会换行
//练习三:打印九九乘法表
public class enen {
public static void main(String[] args) {
for(int i=1;i<=9;i++){
for(int j=1;j<=i;j++){
int sum=j*i;
System.out.print(j+"*"+i+"=" +(i*j)+"\t");
}
System.out.print("\n");
}
}
}
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
Process finished with exit code 0
break,continue语句
break在任何循环语句的主体部分,均可用break控制循环的流程,break用于强行退出循环,不执行循环中剩余的语句。(break语句也在switch语句中使用)
continue语句用在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。
//break
public class enen {
public static void main(String[] args) {
for(int i=1;i<=10;i++){
if(i==5){
break;
}
System.out.println(+i);
}
}
}
//结果(当i==5时,break,结束循环
1
2
3
4
Process finished with exit code 0
public class enen {
public static void main(String[] args) {
for(int i=1;i<=10;i++){
if(i==5){
System.out.println();
continue;
}
System.out.println(+i);
}
}
}
//结果(当i==5时,continue,跳过本次循环,进行下一次循环,所有结果没有5
1
2
3
4
6
7
8
9
10
练习四:打印三角形
//练习四:打印三角形
public class three {
public static void main(String[] args) {
for(int i = 1; i <= 5; i++) {
for(int k = 1; k <= 5 - i; k++) {
System.out.print(" ");
}
for(int k = 1; k <= 2 * i - 1; k++) {
System.out.print("*");
}
System.out.println();
}
}
}
*
***
*****
*******
*********