5.1 四则运算

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

public class Test {
    
    public static void main(String[] args) {
        String condition = "";
        Test test= new Test();
        do{
        Scanner scanner = new Scanner(System.in);
        try{
        System.out.print("请输入第一个数:");
        double a = scanner.nextDouble();
        System.out.print("请输入第二个数:");
        double b = scanner.nextDouble();
        System.out.print("请输入运算符:");
        String s = scanner.next();
        char c = s.charAt(0);
        test.yunsuan(a, b, c);
        }catch(Exception e){
            System.out.println("请输入正确的数据!");
        }
        System.out.print("是否继续?Y:继续,任意字符:结束");
        condition = scanner.next();
    
        }while("Y".equals(condition));
    }
    
    public static void yunsuan(double a,double b,Character c){
        DecimalFormat r=new DecimalFormat(); 
        r.applyPattern("#0.00");
        if(c.equals('+')){
            System.out.println(a+"+"+b+"=" + r.format((a+b)));
        } else if(c.equals('-')){
            System.out.println(a+"-"+b+"=" + r.format((a-b)));
        } else if(c.equals('*')){
            System.out.println(a+"*"+b+"=" + r.format((a*b)));
        } else if(c.equals('/')){
            if(b==0){
                System.out.println("被除数不能为0");
            } else{
                System.out.println(a+"/"+b+"=" + r.format((a/b)));
            }
    
        }else{
            System.out.println("无法识别改运算符");
        }
    }

}

我的结对伙伴  06 刘泽豪http://www.cnblogs.com/wsnd/

 

posted @ 2015-04-30 18:09  10易华生  阅读(111)  评论(0编辑  收藏  举报