码迷,mamicode.com
首页 > 其他好文 > 详细

计算器

时间:2018-02-28 01:12:38      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:system   nbsp   double   als   使用   tca   turn   param   trim   

 

public class Calculate {

/**
* @param args
*/
public Double add(Double num1, Double num2){
return num1 += num2;
}

public Double reduce(Double num1, Double num2){
return num1 -= num2;
}

public Double multip(Double num1, Double num2){
return num1 *= num2;
}

public Double division(Double num1, Double num2){
if(num2 == 0){
System.out.println ("error");
}
return num1 /= num2;
}

}

import java.util.Scanner;

public class TestCalculate {

public static void main(String[] args) {
Calculate js = new Calculate();
System.out.println("欢迎使用简单加减乘除");
String record ="";
while(true) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入第一个数字");
double s1 = scanner.nextDouble();
record += s1;
System.out.println("请输入操作符");
String operator = scanner.next();
record += operator;
//double s2 = 0;
while(!operator.trim().equals("=")) {
System.out.print("请输入第二个数字:");
double s2 = scanner.nextDouble();
record += s2;
switch (operator){
case "+":
js.add(s1,s2);
//s1 += s2;
break;
case "-":
js.reduce(s1,s2);
//s1 -=s2;
break;
case "*":
js.multip(s1,s2);
//s1 *=s2;
break;
case "/":
js.division(s1,s2);
//s1 /=s2;
break;
}
System.out.print("请输入操作符:");
operator = scanner.next();
record += operator;
}
System.out.printf("您输入的%s=%.2f\n", record,s1);
System.out.print("是否继续(Y/N)");
String isContinue = scanner.next();
if(!"Y".equals(isContinue.toUpperCase())){
break;
}
}

}
}

计算器

标签:system   nbsp   double   als   使用   tca   turn   param   trim   

原文地址:https://www.cnblogs.com/vickytjh/p/8481415.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!