【狂神说Java】练习

/*
计算器,基本加减乘除。
*/
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        //主程序
        Scanner scanner = new Scanner(System.in);
        boolean flag = true;
        while (flag) {
            System.out.println("请输入\"加\"、\"减\"、\"乘\"、\"除\"进行运算");
            String str = scanner.nextLine();
            String[] str1;
            String str2;
            Main m = new Main();
            switch (str) {
                case "加":
                    System.out.println("请输入两个数字,用\",\"分隔");
                    str2 = scanner.nextLine();
                    str1 = str2.split(",");//分隔
                    System.out.println(m.add(Double.valueOf(str1[0]), Double.valueOf(str1[1])));
                    break;
                case "减":
                    System.out.println("请输入两个数字,用\",\"分隔");
                    str2 = scanner.nextLine();
                    str1 = str2.split(",");//分隔
                    System.out.println(m.sub(Double.valueOf(str1[0]), Double.valueOf(str1[1])));
                    break;
                case "乘":
                    System.out.println("请输入两个数字,用\",\"分隔");
                    str2 = scanner.nextLine();
                    str1 = str2.split(",");//分隔
                    System.out.println(m.mutli(Double.valueOf(str1[0]), Double.valueOf(str1[1])));
                    break;
                case "除":
                    System.out.println("请输入两个数字,用\",\"分隔");
                    str2 = scanner.nextLine();
                    str1 = str2.split(",");//分隔
                    System.out.println(m.div(Double.valueOf(str1[0]), Double.valueOf(str1[1])));
                    break;
                default:
                    System.out.println("已退出");
                    flag = false;
            }
        }


    }

    public void pr() {

    }

    public double add(double a, double b) {
        //加
        return a + b;
    }

    public double sub(double a, double b) {
        //减
        return a - b;
    }

    public double mutli(double a, double b) {
        //乘
        return a * b;
    }

    public double div(double a, double b) {
        //除
        double c = 0.0;
        try {
            c = a / b;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return c;
    }


}
上一篇:C++ String //string字符串查找和替换 比较 存取 修改单个字符 插入和删除 string字串


下一篇:题图脚本