while() 和 if()else() 和 switch()case()break

whlie和if...else 和 switch()case()break

if...else

package com.scanner;

import java.util.Scanner;
                                          //if判断
public class Demo01 {
    public static void main(String[] args) {
        int i = 0;
        float f = 0.0f;
        System.out.println("请输入整数:");

        Scanner A = new Scanner(System.in);
        if (A.hasNextInt()) {
            i = A.nextInt();
            System.out.println("整数为:" + i);
        } else {
            A.next();
            System.out.println("输入不是整数");

        }


        System.out.println("请输入小数:");


        if (A.hasNextFloat()) {
            f = A.nextFloat();
            System.out.println("小数为:" + f);
        } else {
            System.out.println("输入不是小数");

        }
        A.close();
    }
}

while

package com.scanner;

import java.util.Scanner;

                                       //whlie
                                       // 输入数字,非数字结束,求和,平均数
public class Demo03 {
    public static void main(String[] args) {
        double sum = 0;
        int x = 0;

        System.out.println("请输入数据,非数字结束");
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextDouble()) {
                                            //当while()为ture,{}会一直循环
            double y = scanner.nextDouble();
            sum = sum + y;
            x = x + 1;
            System.out.println("和为:" + sum + " ,共" + x + "数");

        }
        System.out.println("和为:" + sum);
        System.out.println("平均为:" + (sum / x));


                                                //do()while()至少走一次
        int i = 0;
        int Sum = 0;
        do {
            Sum = Sum + i;
            i++;
        }
        while (i <= 100);
        System.out.println(Sum);
    }
}

switche()case()break

                        //switch   case   break
public class SwitchCsaeBreak {
        public static void main(String[] args) {
        String name="小哥";
//JDK7的新特性,表达式结果可以是字符串! ! !
// 字符的本质还是数字
//反编译java---class (字节码文件)----反编译(IDEA)
switch (name){
            case"小姐姐":
                System.out.println("小姐姐");
                break;
            case "小哥":
                System.out.println("小哥哥");
                break;
            default:
                System.out.println("弄啥呦!");
        }
    }
posted @   小幼虫虫  阅读(36)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示