Java流程控制

Java流程控制

1、Scanner

public class ScannerDemo {
    public static void main(String[] args) {
        //创建一个扫描器对象,用于接收键盘数据
        Scanner sc = new Scanner(System.in);
        System.out.println("使用next方式接收:");
        //判断用户有没有输入字符串
        if(sc.hasNext()){
            String str = sc.next();
            System.out.println("输出的内容为" + str);
        }else{
            System.out.println("输入的不是整数");
        }
        if(sc.hasNextFloat()){
            float str = sc.nextFloat();
            System.out.println("输出的内容为" + str);
        }else{
            System.out.println("输入的不是小数");
        }
        //凡是属于IO流的类如果不关闭会一直占用资源
        sc.close();
    }
}

应用案例:简易计算器

package com.qiu.method;

import java.util.Scanner;


public class Calculator {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double a, b;
        String c, flag;
        while (true) {
            a = enterLeftNum();
            c = enterOperator();
            b = enterRightNum();
            calcu(a, b, c);
            System.out.println("继续吗?继续Y,任意键退出");
            flag = sc.next();
            if (flag == "Y") {
                continue;
            } else {
                break;
            }
        }
        System.out.println("End");
        sc.close();
    }

    public static double enterLeftNum() {
        double right;
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入 Dog 运算符 B 的A: ");
            if (sc.hasNextLine()) {
                if (sc.hasNextDouble()) {
                    right = sc.nextDouble();
                    return right;
                } else {
                    System.out.println("输入错误,请重新输入!");
                    sc.next();//清除输入流
                }
            }
        }
    }

    public static String enterOperator() {
        Scanner sc = new Scanner(System.in);
        String s;
        while (true) {
            System.out.println("请输入 Dog 运算符 B 的运算符: ");
            s = sc.nextLine();
            if (!s.equals("+") && !s.equals("-") && !s.equals("*") && !s.equals("/")) {
                System.out.println("输入错误,请输入+ - * /中的一项!");
                continue;
            }
            return s;
        }
    }

    public static double enterRightNum() {
        Scanner sc = new Scanner(System.in);
        double right;
        while (true) {
            System.out.println("请输入 Dog 运算符 B 的B: ");
            if (sc.hasNextDouble()) {
                right = sc.nextDouble();
                return right;
            } else {
                System.out.println("输入错误,请重新输入!");
                sc.next();  //清除输入流
            }
        }
    }

    public static void calcu(double a, double b, String c) {
        switch (c) {
            case "+":
                add(a, b);
                break;
            case "-":
                sub(a, b);
                break;
            case "*":
                multi(a, b);
                break;
            case "/":
                div(a, b);
                break;
            default:
                System.out.println("运算符输入错误!");
                break;
        }
    }

    public static void add(double a, double b) {
        System.out.println(a + b);
    }

    public static void sub(double a, double b) {
        System.out.println(a - b);
    }

    public static void multi(double a, double b) {
        System.out.println(a * b);
    }

    public static void div(double a, double b) {
        if (b == 0) {
            System.out.println("除数不能为0!");
            return;
        }
        System.out.println(a / b);
    }
}

2、循环

增强型for循环

public class AdvanceFor {
    public static void main(String[] args) {
        int[] numbers = {10, 20, 30, 40, 50};
        //遍历数组元素
        for(int x:numbers){
            System.out.println(x);
        }
    }
}

3、选择

Switch

public class SwitchDemo01 {
    public static void main(String[] args) {
        String s = "345";
        // 支持String类型   case后必须接常量
        switch (s) {
            case "123":
                System.out.println("111");
                break;
            case "345":
                System.out.print("123");
            default:
                System.out.println("null");
        }
    }
}
posted @   TimQiu  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示