JAVA学习---流程控制

1.顺序结构

2.选择结构

2.1 if语句(if语句基本结构、if…else语句、if…else if…结构)

import java.util.Scanner;

//根据身高选择衣服尺寸
public class Demo3_1 {
    public static void main(String[] args) {
        System.out.print("请输入身高(>=155):\n");

        Scanner sc1 = new Scanner(System.in);
        double dbheight = sc1.nextDouble();
        
        if(dbheight >=155 && dbheight < 160) {
            System.out.println("155-160对应的衣服尺码是S");
        }
        else if (dbheight < 165) {
            System.out.println("160-165对应的衣服尺码是M");
        }
        else if (dbheight < 170) {
            System.out.println("165-170对应的衣服尺码是L");
        }
        else if (dbheight < 175) {
            System.out.println("170-175对应的衣服尺码是XL");
        }
        else if (dbheight >= 175) {
            System.out.print("175以上对应的衣服尺码是XXL");
        }
    }
}

2.2 switch语句

import java.util.Scanner;

//根据手机号判断运营商(中国大陆)
public class Demo3_2 {
    public static void main(String[] args) {
        Scanner sc1 = new Scanner(System.in);
        System.out.print("请输入完整手机号码:\n");
        
        long l1 = sc1.nextLong();
        int n1 = (int) (l1/1000000000);
        
        switch (n1) {
        case 13:
            System.out.print("祝您大吉大利!");
            break;
        case 15:
            System.out.print("祝您万事顺利!");
            break;
        case 17:
            System.out.print("祝您阖家吉利!");
            break;
        case 18:
            System.out.print("祝您顺顺利利!");
            break;
        default:
            System.out.print("祝您一切都利!");
            break;
        }
    }
}
//太多了就不真正写了,祝大家开心吧

3.循环结构

3.1 while语句

3.2 do…while语句

3.3 for语句

//嵌套循环打印字符输出
public class Demo3_3 {
    public static void main(String[] args) {
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4 - i; j++) {
                System.out.print(" ");
            }
            for (int j = 0; j < 2*i + 1; j++) {
                System.out.print("*");
            }
            System.out.print("\n");
        }
    }
}

4跳转操作

4.1 break语句

跳出循环;跳出指定循环(标签outer)

4.2 continue语句

结束本次运算,进入本循环的下一次运算

4.3 return语句

返回

import java.util.Random;
import java.util.Scanner;
//猜字小游戏
public class Demo3_4 {
    public static void main(String[] args) {
        Scanner sc1 = new Scanner(System.in); //输入猜的数字
        Random rd1 = new Random(); //产生随机数
        
        int n1 = rd1.nextInt(10) + 1;    
        int ntimes = 0;
        
        System.out.println("***************************");
        System.out.println("**********猜字游戏************");
        System.out.println("***************************");
        
        while(true) {
            System.out.print("请输入您猜的数字:\n");
            int ng1 = sc1.nextInt();
            ntimes = ntimes + 1;
            
            if (ng1 > n1) {
                System.out.print("您猜的数大了!\n");
            }
            else if (ng1 < n1) {
                System.out.print("您猜的数小了!\n");
            }
            else {
                System.out.println("您猜对了!共猜了"+ ntimes + "次!");
                System.out.print("请选择:\n");
                System.out.print("1:继续\n");
                System.out.print("2:退出\n");
                
                int n2 = sc1.nextInt(); //功能选择
                if (n2 == 1) {
                    n1 = rd1.nextInt(10) + 1; //重新产生随机数
                    ntimes = 0;
                }
                else if (n2 == 2) {
                    System.out.print("欢迎下次光临!\n");
                    break; //跳出游戏
                }
            }
        }    
    }
}

 

posted @ 2019-03-15 21:03  凌·杰特  阅读(139)  评论(0)    收藏  举报