if else if 分支、switch case 分支、 while 循环 、do while 循环

1.Scanner接收用户输入的数据:

import java.util.Scanner;

Scanner s = new Scanner(System.in);

int a = s.nextInt();

2.分支结构

  1. if else if 多路分支,适用于多个条件
    语法:
      if(boolean-1){
        语句块1
      }else if(boolean-2){
        语句块2
      }else if(boolean-3){
        语句块3
      }else{
        语句块4
      }..............................
    package day04;
    import java.util.Scanner;
    //成绩等级判断
    public class ScoreLevel {
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            System.out.println("请输入成绩:");
            double score = scan.nextDouble();
            //带数(888,-56,95,85,65,45)
            if(score<0 || score>100){
                System.out.println("成绩不合法");
            }else if(score>=90){ //合法
                System.out.println("A-优秀");
            }else if(score>=80){
                System.out.println("B-良好");
            }else if(score>=60){
                System.out.println("C-中等");
            }else{
                System.out.println("D-不及格");
            }
    
        }
    }
  2. swich ...case  多路分支结构
     

    优点:效率高、结构清晰

    缺点:只能对整数判断相等

    break:跳出switch
    不可以用continue

    package day04;
    import java.util.Scanner;
    //命令解析程序
    public class CommandBySwitch {
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            System.out.println("请选择功能: 1.取款 2.存款 3.查询余额 0.退卡");
            int command = scan.nextInt();
    
            switch(command){
                case 1:
                    System.out.println("取款业务...");
                    break;
                case 2:
                    System.out.println("存款业务...");
                    break;
                case 3:
                    System.out.println("查询余额业务...");
                    break;
                case 0:
                    System.out.println("退卡业务...");
                    break;
                default:
                    System.out.println("输入错误");
            }
        }
    }

  3. 循环3要素
      a.循环变量的初始化
      b.循环的条件(以循环变量为基础)
      c.循环变量的改变(向着循环变量的结束变)

  4. 循环结构
    a.while :先判断后执行,有可能一次都不执行
      语法
      while(boolean){
        语句块---------------反复执行的代码
      }
    案例1:输出5次“行动是成功的阶梯”
     

    //1)输出5次"行动是成功的阶梯":
    int times = 0;  //1.循环变量的初始化
    while(times<5){ //2.循环的条件
        System.out.println("行动是成功的阶梯");
        times++;    //3.循环变量的改变
    }
    System.out.println("继续执行...");

    案例2 :猜数字游戏,由系统提供随机数,用户来猜,猜对了输出“猜对了”,猜错了输出“猜大了”或“猜小了”。
     

    import java.util.Scanner;
    //猜数字游戏
    public class Gueesing {
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            int num = (int)(Math.random()*1000+1); //1到1000之内的随机数
      
    
            //300(大),200(小),250(对)
            System.out.println("猜吧!");
            int guess = scan.nextInt(); //1.
            while(guess!=num){ //2.
                if(guess>num){
                    System.out.println("太大了");
                }else{
                    System.out.println("太小了");
                }
                System.out.println("猜吧!");
                guess = scan.nextInt(); //3.
            }
            System.out.println("恭喜你猜对了!");
        }
    }

    b. do.....while:   先执行后判断,至少执行一次
        语法:
         do{
             语句块
          }while(boolean);
    案例:猜数字游戏

import java.util.Scanner;
//猜数字游戏
public class Gueesing {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int num = (int)(Math.random()*1000+1); //1到1000之内的随机数
        System.out.println(num); //作弊

        //假设num=250
        //300(大),200(小),250(对)
        int guess;
        do{
            System.out.println("猜吧!");
            guess = scan.nextInt(); //1+3
            if(guess>num){
                System.out.println("太大了");
            }else if(guess<num){
                System.out.println("太小了");
            }else{
                System.out.println("恭喜你猜对了");
            }
        }while(guess!=num); //2
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值