zoukankan      html  css  js  c++  java
  • java四则运算

    import java.text.DecimalFormat;
    import java.util.Scanner;

    public class Zhidao {

    public static void main(String[] args) {
    String condition = "";
    Zhidao zhidao = new Zhidao();
    do{
    Scanner scanner = new Scanner(System.in);
    try{
    System.out.print("请输入第一个数:");
    double x = scanner.nextDouble();
    System.out.print("请输入第二个数:");
    double y = scanner.nextDouble();
    System.out.print("请输入运算符:");
    String s = scanner.next();
    char z = s.charAt(0);
    zhidao.yunsuan(x, y, z);
    }catch(Exception e){
    System.out.println("请输入正确的数据!");
    }
    System.out.print("是否继续?continue:继续,任意字符:结束");
    condition = scanner.next();

    }while("continue".equals(condition));
    }

    public static void yunsuan(double x,double y,Character z){
    DecimalFormat r=new DecimalFormat();
    r.applyPattern("#0.00");
    if(z.equals('+')){
    System.out.println(x+"+"+y+"=" + r.format((x+y)));
    } else if(z.equals('-')){
    System.out.println(x+"-"+y+"=" + r.format((x-y)));
    } else if(z.equals('*')){
    System.out.println(x+"*"+y+"=" + r.format((x*y)));
    } else if(z.equals('/')){
    if(y==0){
    System.out.println("被除数不能为0");
    } else{
    System.out.println(x+"/"+y+"=" + r.format((x/y)));
    }

    }else{
    System.out.println("无法识别改运算符");
    }
    }

    }

    
    
  • 相关阅读:
    String字符串性能优化的探究
    Spring笔记(5)
    Spring笔记(4)
    力扣217. 存在重复元素
    力扣1480. 一维数组的动态和
    Ribbon 和 Feign 的区别
    Eureka的自我保护机制
    Planar graph and map 3-colorability reduce to one another
    3-colorability
    一少年独坐屋中突然顿悟!-回溯
  • 原文地址:https://www.cnblogs.com/myy69/p/6542069.html
Copyright © 2011-2022 走看看