要求
1,写四个方法,加减乘除
2,利用循环+switch进行用户交互,判断用户输入的是哪个运算符
3,传递需要操作的两个数
4,输出结果
package net.yang.Exerises;
import java.util.Scanner;
public class computer {
public static void main(String[] args) {
System.out.println("请输入你想计算的次数:");
Scanner t = new Scanner(System.in);
int t1 = t.nextInt();
for (int i = 0; i <t1; i++) {
System.out.println("请输入第一个数");
Scanner s1 = new Scanner(System.in);
double x=s1.nextDouble();
System.out.println("请输入运算符");
Scanner s2 = new Scanner(System.in);
String n=s2.nextLine();
System.out.println("请输入第二个数");
Scanner s3 = new Scanner(System.in);
double y=s3.nextDouble();
switch (n){
case "+":
Add(x, y);
System.out.println("你输入的运算是加法运算");
break;
case "-":
Substract(x, y);
System.out.println("你输入的运算是减法运算");
break;
case "*":
Product(x, y);
System.out.println("你输入的运算是加法运算");
break;
case "/":
Divide(x, y);
System.out.println("你输入的运算是加法运算");
default:
System.out.println("你输入的运算符有误,请重新输入");
}
}
}
public static void Add(double a,double b){
double sum=a+b;
System.out.println("结果是:"+a+"+"+b+"="+sum);
}
public static void Substract(double a,double b){
double sum=a-b;
System.out.println("结果是:"+a+"-"+b+"="+sum);
}
public static void Product(double a,double b){
double sum=a*b;
System.out.println("结果是:"+a+"*"+b+"="+sum);
}
public static void Divide(double a,double b){
double sum=a/b;
System.out.println("结果是:"+a+"/"+b+"="+sum);
}
}