8.java循环判断语句

一、顺序结构

顺序结构语句是java程序默认的执行流程,按照代码的先后顺序,从上到下依次执行。

如:

public class sunxv {
    public static void main(String[] args) {
        System.out.println("大鹏一日同风起,扶摇直上九万里。");
        System.out.println("假令风歇时下来,犹能簸却沧溟水。");
        System.out.println("世人见我恒殊调,闻余大言皆冷笑。");
        System.out.println("宣父犹能畏后生,丈夫未可轻年少。");
    }
}

在这里插入图片描述

二、分支结构

1.if(判断)

if语句的第一种格式:

if(关系表达式)
{
   语句体;
}

在这里插入图片描述
在这里插入图片描述

需求:定义变量记录女婿的酒量,如果女婿的酒量大于2斤,老丈人就搭理他,否则就不搭理

示范:

public class ifpanduan {
    public static void main(String[] args) {
        Scanner cs =new Scanner(System.in);
        System.out.println("请输入女婿的酒量:");
        int njiu = cs.nextInt();

        //对酒量进行判断
        if( njiu > 2 ) {
            System.out.println("小伙子不错哦!");
        }
    }
}

if 语句的第二种格式

if(关系表达式){
   语句体1;
   }else{
   语句体2;
   }

在这里插入图片描述
在这里插入图片描述

需求:键盘录入一个整数,表示身上的钱。
如果大于等于100块,就是网红餐厅。
否则,就吃经济实惠的沙县小吃。

示范:

import java.util.Scanner;

public class ifdier {
    public static void main(String[] args) {
        Scanner cs =new Scanner(System.in);
        System.out.println("请输入有多少钱:");
        int mey = cs.nextInt();
        if(mey >= 100) {
            System.out.println("吃网红餐厅!");
        }else{
            System.out.println("吃沙县小吃!");
        }
    }
}

在这里插入图片描述
示范:

import java.util.Scanner;

public class iflianx {
    public static void main(String[] args) {
        //分析:
        //1.键盘录入一个整数表示电影票的票号。
        Scanner cs =new Scanner(System.in);
        System.out.println("请输入票号");
        int piao = cs.nextInt();
        //2.判断票号是奇数还是偶数
        if(piao >= 0 && piao <= 100){
            if(piao %2 == 1){
                System.out.println("坐左侧");
            }else{
                System.out.println("坐右侧");
            }
        }else{
            System.out.println("对不起,没有搜到你的票号");
        }
    }
}

if 语句的第三种格式

在这里插入图片描述
在这里插入图片描述

2.switch(选择)

Switch语法格式:

switch(表达式){
       case1:
           语句体1;
           break;
       case2:
            语句体2;
            break;
        ...
        default:
            语句体n+1;
            break; 
}

在这里插入图片描述
在这里插入图片描述
运行流程图

在这里插入图片描述
示范:

package com.guitongwan.demo;
import java.util.Scanner;

public class shifan {
    public static void main(String[] args) {
        //兰州拉面,武汉热干面,北京炸酱面,陕西油泼面

        //1.定义变量记录 想吃的面
        String noodles = "兰州拉面";

        //2.拿着这个面利用switch跟四种面条匹配
        switch (noodles) {
            case "兰州拉面":
                System.out.println("吃兰州拉面!!!");
                break;
            case "武汉热干面":
                System.out.println("吃武汉热干面!!!");
                break;
            case"北京炸酱面":
                System.out.println("吃北京炸酱面!!!");
                break;
            case"陕西油泼面":
                System.out.println("吃陕西油泼面!!!");
                break;
            default:
                System.out.println("吃方便面!");
                break;
        }
    }
}

在这里插入图片描述

switch新特性(JDK12)

public class texin {
    public static void main(String[] args) {
        //需求:
        // 1 2 3 一 二 三
        int number = 1;
        switch (number){
            case 1 -> {
                System.out.println("一");
            }
            case 2 ->{
                System.out.println("二");
            }
            case 3 ->{
                System.out.println("三");
            }
            default -> {
                System.out.println("没有这种选项");
            }
        }
    }
}

三、循环结构

  • 重复的做某些事情
  • 具有明确的开始和停止标记

for循环

格式

for( 初始化语句 ; 条件判断语句 ; 条件控制语句 ){
    循环体语句;
}

示范:

for(int i = 1;i <= 10;i++){
  System.out.println("HelloWorld");
}

在这里插入图片描述
图解:
在这里插入图片描述
示范:

public class xunhf {
    public static void main(String[] args) {
        for(int i = 1 ;i <= 10 ; i++){
            System.out.println("Helloworld");
        }
    }
}

在这里插入图片描述

while循环

语法格式:

初始化语句;
while(条件判断语句){
   循环体语句;
   条件控制语句;
}

在这里插入图片描述

需求:利用while循环打印 1~100

示范:

public class whDemo {
    public static void main(String[] args) {
        //需求:利用while循环打印 1~100
        int i = 1;
        while(i <= 100 ) {
            System.out.println(i);
            i++;
        }
    }
}

无限循环

示范:

while(true){
            System.out.println("wx");
        }

跳转控制语句(continue)

示范:

public class tioa {
    public static void main(String[] args) {
        //跳过某一次循环
        for (int i = 1; i <= 5; i++){
            if(i == 3){
                //结束本次循环,继续下一次循环。
                continue;
            }
            System.out.println("小老虎在吃第" + i + "个包子");
        }
    }
}

在这里插入图片描述

跳转控制语句(break)

示范:

public class tioa {
    public static void main(String[] args) {
        //跳过某一次循环
        for (int i = 1; i <= 5; i++){
            if(i == 3){
                //结束整个循环
                break;
            }
            System.out.println("小老虎在吃第" + i + "个包子");
        }
    }
}

在这里插入图片描述

四、猜数字游戏

生成随机数

从 0 ~ 99

//1.导包
import java.util.Random;

public class suiji {
    public static void main(String[] args) {
       //先获取一个随机数。
        // 范围: 0 ~ 99

        //2.创建对象
        Random r = new Random();

        //3.生成随机数
        //判断技巧:
        //在小括号中,书写的是生成随机数的范围
        //这个范围一定是从0开始的。
        //到这个数 -1 结束
        for(int i = 0; i < 100;i++) {
            int number = r.nextInt(100);// 0 ~ 100
            System.out.println(number);
        }

获得指定位随机数

从 10 ~ 99

rand.nextInt(90) + 10; 

猜数字实例

需求:程序自动生成一个 1 ~ 100之间的随机数字,使用程序实现猜出这个数字是多少?

实例:

//1.导包
import java.util.Random;
import java.util.Scanner;

public class caishuzi {
    public static void main(String[] args) {

        //2.生成一个 1 ~ 100 随机数
        Random r = new Random();
        int shu = r.nextInt( 100)+1;//1 ~ 100

        //3.猜这个数字是多少
        Scanner sc = new Scanner(System.in);
        
        //4.循环你需要猜的东西
        while(true) {
            System.out.println("请输入要输入的参数");
            int nub = sc.nextInt();
            if(nub > shu) {
                System.out.println("猜大了");
            }else if(nub < shu){
                System.out.println("猜小了");
            }else{
                System.out.println("猜中了");
                break;
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Java中,while循环语句是一种常见的循环控制结构,用于重复执行一段代码,直到指定的条件不再满足为止。while循环语句的基本语法如下所示: ``` while (condition) { // 循环体代码 } ``` 其中,`condition`是一个布尔表达式,表示循环条件。只要`condition`的值为`true`,就会重复执行循环体代码。当`condition`的值为`false`时,循环就会终止。 下面是一个简单的示例,演示了如何使用while循环语句来计算1到10的整数之和: ``` int sum = 0; int i = 1; while (i <= 10) { sum += i; i++; } System.out.println("1到10的整数之和是:" + sum); ``` 上面的代码中,`sum`变量用于存储1到10的整数之和。`i`变量用于控制循环次数,初始值为1。在每次循环中,先将`i`加到`sum`中,然后将`i`的值加1,直到`i`的值大于10为止。 另外,Java中还有一种类似的循环语句叫做do...while循环语句。它的语法与while循环语句有所不同,具体如下: ``` do { // 循环体代码 } while (condition); ``` do...while循环语句先会执行一次循环体代码,然后再判断循环条件是否满足。只要循环条件满足,就会继续执行循环体代码,直到循环条件不再满足为止。 下面是一个简单的示例,演示了如何使用do...while循环语句来输入一个整数,直到输入的值为0为止: ``` Scanner scanner = new Scanner(System.in); int num; do { System.out.print("请输入一个整数(输入0结束):"); num = scanner.nextInt(); } while (num != 0); System.out.println("输入结束。"); ``` 上面的代码中,使用Scanner类从控制台读取整数,然后使用do...while循环语句判断输入的值是否为0。只要输入的值不为0,就会继续输入,直到输入的值为0为止。最后输出“输入结束”。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值