简易计算器
实现加减乘除功能,通过用户交互实现
思路
1.写四个方法
2.利用switch进行交互
3.传递需要操作的两个数
4.输出结果
package com.kuang.method;
import java.util.Scanner;
public class TestDemo {
public static void main(String[] args) {
TestDemo t = new TestDemo();
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个数字:");
double num1 = sc.nextDouble();
System.out.println("请输入第二个数字:");
double num2 = sc.nextDouble();
System.out.println("请输入运算符:");
String operate = sc.next();
switch (operate){
case "+" :
System.out.println(t.add(num1,num2));
break;
case "-" :
System.out.println(t.jian(num1,num2));
break;
case "*" :
System.out.println(t.chen(num1,num2));
break;
case "/" :
System.out.println(t.chu(num1,num2));
break;
default:
System.out.println("请勿输入除加减乘除以外的运算符!");
}
sc.close();
}
public double add(double num1,double num2){
return num1+num2;
}
public double jian(double num1,double num2){
return num1-num2;
}
public double chen(double num1,double num2){
return num1*num2;
}
public double chu(double num1,double num2){
return num1/num2;
}
}