一,顺序结构:程序自上而下按顺序执行,也是最基本的流程控制结构
public class Demo01 {
public static void main(String[] args) {
System.out.println("hello1");
System.out.println("hello2");
System.out.println("hello3");
System.out.println("hello4");
}
}
输出结果:
hello1
hello2
hello3
hello4
二,if条件
public class Demo02 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
//equals:比较字符串是否相对
if(s.equals("hello")){
System.out.println("yes");
}
scanner.close();
}
}
输出结果:
输入:hello
输出:yes
引申知识:
代码中:Scanner scanner = new Scanner(System.in); String s = scanner.nextLine();
获取用户输入,导入import java.util.Scanner;即可使用
equals()是一个比较字符串的函数,注意字符串的比较不能使用“==”
还需注意,凡是属于IO流的类如果不关闭会一直占用资源,所以需要语句scanner.close()
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int score = scanner.nextInt();
if(score >= 60){
System.out.println("及格");
}else{
System.out.println("不及格");
}
scanner.close();
}
输出结果:
输入:76
输出:及格
三,switch多选择结构
public static void main(String[] args) {
//例子:成绩分等级
char grade = 'C';
switch(grade) {//注意switch后可以是char也可以是string
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("你可能输错了");
}
}
输出结果:合格
switch语句中的变量类型可以是:
1,byte,short,int或char
2,从javase7开始,switch支持字符串String类型了
3,同时case标签必须为字符串常量或字面量。
注意:每个case里面加上break,会使程序执行完此条件,终止执行;但如果没有break,程序会挨着往下执行,直到程序代码结束。
四,循环控制
1,while 先判断,在执行
public static void main(String[] args) {
//计算1+2+3....+100=?
int i = 0;
int sum = 0;
while(i<=100){
sum += i;
i++;
}
System.out.println(sum);
}
输出结果:5050
2,do...while 先执行,再判断
public static void main(String[] args) {
//计算1+2+3....+100=?
int i = 0;
int sum = 0;
do {
sum += i;
i++;
}while(i<=100);
System.out.println(sum);
}
输出结果:5050
3,for循环 for(初始化;布尔表达式;更新){}
public static void main(String[] args) {
//计算0-100之间奇数和偶数的和
int oddSum = 0;
int evenSum = 0;
for (int i = 0; i <= 100; i++) {
if(i % 2 != 0){
oddSum += i;
}else{
evenSum +=i;
}
}
System.out.println(oddSum);
System.out.println(evenSum);
}
输出结果:
2500
2550
//用for循环输出1-1000之间能被5整除的数,并且每行输出3个
public static void main(String[] args) {
for (int i = 0; i <= 1000; i++) {
if(i % 5 == 0){
System.out.print(i+"\t");
}
if(i %(5*3) == 0){//每行
System.out.println();
}
}
}//print输出不会换行
//println输出会换行
输出结果:
0
5 10 15
20 25 30
35 40 45
50 55 60
65 70 75
80 85 90
95 100 105
110 115 120
125 130 135
140 145 150
155 160 165
170 175 180
185 190 195
200 205 210
215 220 225
230 235 240
245 250 255
260 265 270
275 280 285
290 295 300
305 310 315
320 325 330
335 340 345
350 355 360
365 370 375
380 385 390
395 400 405
410 415 420
425 430 435
440 445 450
455 460 465
470 475 480
485 490 495
500 505 510
515 520 525
530 535 540
545 550 555
560 565 570
575 580 585
590 595 600
605 610 615
620 625 630
635 640 645
650 655 660
665 670 675
680 685 690
695 700 705
710 715 720
725 730 735
740 745 750
755 760 765
770 775 780
785 790 795
800 805 810
815 820 825
830 835 840
845 850 855
860 865 870
875 880 885
890 895 900
905 910 915
920 925 930
935 940 945
950 955 960
965 970 975
980 985 990
995 1000
//打印99乘法表
public static void main(String[] args) {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j+"*"+i+"="+(i*j)+"\t");
}
System.out.println("");
}
}
输出结果:
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
4,增强for循环
public static void main(String[] args) {
int[] numbers = {10,20,30,40,50};//定义一个数组
for(int x:numbers){
System.out.print(x+"\t");
}
}
输出结果:
10 20 30 40 50
五,break
public static void main(String[] args) {
int i = 0;
while(i<10){
i++;
System.out.println(i);
if(i == 6){
break;//跳出当前while循环
}
}
System.out.println("这是循环外");
}
输出结果:
1
2
3
4
5
6
这是循环外
六,continue
public static void main(String[] args) {
int i = 0;
while(i<100){
i++;
if(i%10!=0){
continue;//让程序跳到开始的地方
}
System.out.print(i+"\t");
}
}
输出结果:
10 20 30 40 50 60 70 80 90 100
七,带标签的continue(了解)
public static void main(String[] args) {
//打印101-150之间的所有质数
int count = 0;
outer:for (int i = 101; i < 150; i++) {
for(int j = 2;j < i/2;j++){
if(i % j == 0){
continue outer;
}
}
System.out.print(i+"\t");
}
}
输出结果:
101 103 107 109 113 127 131 137 139 149