求大神!!JAVA这题怎么做
3个回答
展开全部
Calculator类:
public class Calculator {
public double jia(double one,double two){
return one+two;
}
public double jian(double one,double two){
return one-two;
}
public double cheng(double one,double two){
return one*two;
}
public double chu(double one,double two){
return one/two;
}
}
Index类:
import java.util.Scanner;
public class Index {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Calculator cal=new Calculator();
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个数:");
String one = sc.next();
System.out.println("请输入运算符号:");
String fuhao = sc.next();
System.out.println("请输入第二个数:");
String two=sc.next();
switch (fuhao) {
case "+":
System.out.println("结果为: "+cal.jia(Double.parseDouble(one), Double.parseDouble(two)));
break;
case "-":
System.out.println("结果为: "+cal.jian(Double.parseDouble(one), Double.parseDouble(two)));
break;
case "*":
System.out.println("结果为: "+cal.cheng(Double.parseDouble(one), Double.parseDouble(two)));
break;
case "/":
System.out.println("结果为: "+cal.chu(Double.parseDouble(one), Double.parseDouble(two)));
break;
default:
break;
}
}
}
public class Calculator {
public double jia(double one,double two){
return one+two;
}
public double jian(double one,double two){
return one-two;
}
public double cheng(double one,double two){
return one*two;
}
public double chu(double one,double two){
return one/two;
}
}
Index类:
import java.util.Scanner;
public class Index {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Calculator cal=new Calculator();
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个数:");
String one = sc.next();
System.out.println("请输入运算符号:");
String fuhao = sc.next();
System.out.println("请输入第二个数:");
String two=sc.next();
switch (fuhao) {
case "+":
System.out.println("结果为: "+cal.jia(Double.parseDouble(one), Double.parseDouble(two)));
break;
case "-":
System.out.println("结果为: "+cal.jian(Double.parseDouble(one), Double.parseDouble(two)));
break;
case "*":
System.out.println("结果为: "+cal.cheng(Double.parseDouble(one), Double.parseDouble(two)));
break;
case "/":
System.out.println("结果为: "+cal.chu(Double.parseDouble(one), Double.parseDouble(two)));
break;
default:
break;
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2017-06-09 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
public class Calculator {
private int number1;
private int number2;
public Calculator(int number1, int number2) {
this.number1 = number1;
this.number2 = number2;
}
public double add() {
return number1 + number2;
}
public double subtract() {
return number1 - number2;
}
public double multiple() {
return number1 * number2;
}
public double divide() {
if(number2 == 0) {
return 0;
}
return number1 / number2;
}
}
public class TestCalculator {
public static void main(String[] args) {
Calculator calculator = new Calculator(5, 2);
System.out.println(calculator.add());
System.out.println(calculator.subtract());
System.out.println(calculator.multiple());
System.out.println(calculator.divide());
}
}
private int number1;
private int number2;
public Calculator(int number1, int number2) {
this.number1 = number1;
this.number2 = number2;
}
public double add() {
return number1 + number2;
}
public double subtract() {
return number1 - number2;
}
public double multiple() {
return number1 * number2;
}
public double divide() {
if(number2 == 0) {
return 0;
}
return number1 / number2;
}
}
public class TestCalculator {
public static void main(String[] args) {
Calculator calculator = new Calculator(5, 2);
System.out.println(calculator.add());
System.out.println(calculator.subtract());
System.out.println(calculator.multiple());
System.out.println(calculator.divide());
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
写个类。定义4个方法。很简单的。然后判断录入调用就行了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询