1.顺序结构
从上到下
public class c1{
public static void main(String[] args){
System.out.println("1");
System.out.println("2");
System.out.println("3");
System.out.println("4");
}
}
2.选择结构(判断语句)
2.1 if
if第一种格式:if
//单if语句
public class c1{
public static void main(String[] args){
System.out.println("是否可以进入网吧");
int age =16;
if (age>=18){
System.out.println("可以进入");
}
System.out.println("不可以进入");
}
}
if第二种格式:if...else
//标准if..else语句
public class c1{
public static void main(String[] args){
int num =13;
if(num % 2 == 0 ){
System.out.println("偶数");
}
else{
System.out.println("奇数");
}
}
}
if 第三种格式 if...else if...else
//扩展if..else
public class c1{
public static void main(String args[]){
int x = 10;
int y;
if(x >=3){
y = 2 * x +1;
}
else if(-1 < x && x<3){
y = 2 * x;
}
else{
y = 2 * x - 1;
}
System.out.println("结果是:" + y);
}
}
练习:
指定成绩,判断成绩成绩;
public class c1{
public static void main(String arg[]){
int score = 98;
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 if(score >=0 && score <60){
System.out.println("不及格");
}
else{
System.out.println("数据不合理");
}
}
}
2.2 swith
//标准switch
public class c1{
public static void main(String[] args){
int num = 1;
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 7:
System.out.println("星期日");
break;
default:
System.out.println("数据不合理");
break;
}
}
}
/*
switch语句使用的注意事项:
- 1.多个case后面的数值不可以重复。
- 2.switch后面小括号当中只能是下列数据类型:
- 基本数据类型:byte,short,char,int
- 引用数据类型:String类型,enum枚举
- 3.switch语句格式可以很灵活,前后顺序可以颠倒,而且break语句还可以省略。
- 4.匹配到哪一个case就从哪一个位置向下执行,直到遇到了break或者整体结束位置
public class c1{
public static void main(String[] args){
int num = 2;
switch(num){
case 1:
System.out.println("这是1");
break;
case 2:
System.out.println("这是2");
break;
case 3:
System.out.println("这是3");
break;
default:
System.out.println("数据不合理");
break;
}
}
}
3.循环结构
3.1 for循环
3.2 while循环
3.3 do...while循环
.............(待更新)