Java结构学习

1.顺序结构

按顺序输出

		System.out.println("student1");
        System.out.println("student2");
        System.out.println("student3");
        System.out.println("student4");
        System.out.println("student5");
        System.out.println("student6");

2.If选择结构

If结构

		Scanner scanner = new Scanner(System.in);
        System.out.println("请输入字符串");
        String s = scanner.nextLine();
        if (s.equals("hello")){
            System.out.println(s);
        }
        System.out.println("End");
        scanner.close();

If-else结构

		//考试分数大于60就是及格,小于60就是不及格
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入成绩:");
        int score = scanner.nextInt();
        if (score>60){
            System.out.println("及格");
        }else {
            System.out.println("不及格");
        }
        scanner.close();

If-else if-else结构

		Scanner scanner = new Scanner(System.in);
        System.out.println("请输入成绩:");
        int score = scanner.nextInt();
        if (score==100){
            System.out.println("恭喜满分");
        }else if (score<100 && score>=90){
            System.out.println("A");
        }else if (score<90 && score>=80){
            System.out.println("B");
        }else if (score<80 && score>=70){
            System.out.println("C");
        }else if (score<70 && score>=60){
            System.out.println("D");
        }else if (score<60){
            System.out.println("不及格");
        }else {
            System.out.println("输入成绩不合法");
        }
        scanner.close();

3.Switch选择结构

判断char类型

		char grade = 'A';
        switch (grade){
            case 'A':
                System.out.println("优秀");
                break;
            case 'B':
                System.out.println("良好");
                break;
            case 'C':
                System.out.println("一般");
                break;
            case 'D':
                System.out.println("再接再厉");
                break;
            case 'E':
                System.out.println("挂科");
                break;
            default:
                System.out.println("未知等级");
        }

判断String类型

	//jdk7的新特性,表达式结果可以是字符串
    public static void main(String[] args) {
        String str = "葡萄";
        switch (str){
            case "奶茶":
                System.out.println("奶茶");
                break;
            case "葡萄":
                System.out.println("葡萄");
                break;
            default:
                System.out.println("啥呀");
        }
    }

4.While循环

输出1~100

		//输出1~100
        int i = 0;
        while (i<100){
            i++;
            System.out.println(i);
        }

死循环

		//死循环
        while (true){
            //等待客户连接
            //定时检查
            //.....
        }

计算1+2+…+100

		//计算1+2+...+100
        int i = 0;
        int sum = 0;
        while (i<100){
            i++;
            sum += i;
        }

        System.out.println(sum);

5.DoWhile循环

		int i = 0;
        int sum = 0;
        do {
            i++;
            sum += i;
        }while (i<100);
        System.out.println(sum);

与while对比

		int i = 0;
        while (i<0){
            System.out.println(i);
            i++;
        }
        System.out.println("=============");
        do {
            System.out.println(i);
            i++;
        }while (i<0);

6.For循环

转换while

		int a = 1;//初始化条件
        while (a<=100){ //条件判断
            System.out.println(a);//循环体
            a += 2;//迭代
        }
        System.out.println("while循环结束");
        
        for (int i=1;i<=100;i++){
            System.out.println(i);
        }
        System.out.println("for循环结束");

死循环

		//死循环
        for ( ; ; ){

        }

计算0到100间的奇数和偶数的和

		//计算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);

输出1到1000能被5整除的数,并且每行输出3个

		//输出1到1000能被5整除的数,并且每行输出3个
        for (int i = 0; i <= 1000; i++) {
            if (i%5 == 0){
                System.out.print(i+"\t");
            }
            if (i%(5*3) == 0){
                System.out.println();
            }
        }

九九乘法表

		for (int j = 1; j <= 9; j++) {
            for (int i = 1; i <= j; i++) {
                System.out.print(j+"*"+i+"="+(1*i)+"\t");
            }
            System.out.println();
        }

增强for循环

		int[] numbers = {10,20,50,40};
        for (int x : numbers) {
            System.out.println(x);
        }

打印三角形

		//打印三角形 5行 13579
        for (int i = 1; i <= 5; i++) {
            for (int j = 5;j>=i;j--){
                System.out.print(" ");
            }
            for (int j = 1;j<=i;j++){
                System.out.print("*");
            }
            for (int j=1;j<i;j++){
                System.out.print("*");
            }
            System.out.println();
        }

7.break,continue,goto

break直接跳出循环

		int i=0;
        while (i<100){
            i++;
            System.out.println(i);
            if (i==30){
                break;
            }
        }
        System.out.println("123");

continue跳过这一次循环

		int i = 0;
        while (i<100){
            i++;
            if (i%10 == 0){
                System.out.println();
                continue;
            }
            System.out.print(i);
        }

goto设置标签

	//打印101到150之间所有质数
    //质数是大于1的自然数中,除了1和它本身外没有其他因数的自然数
    public static void main(String[] args) {
        //不建议使用
        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+" ");
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值