栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/871968.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号