import java.util.Scanner;
public class App{
public static void main(String[] args){
double result=0;
System.out.println("请输入需要进行运算的第一个数:");
Scanner scanner=new Scanner(System.in);
int a=scanner.nextInt();
System.out.println("请输入需要进行什么运算(+,-,*,/):");
Scanner scanner2=new Scanner(System.in);
String mark=scanner2.nextLine();
System.out.println("请输入需要进行运算的第二个数:");
Scanner scanner3=new Scanner(System.in);
int b=scanner3.nextInt();
switch(mark){
case "+":result=add(a,b);
break;
case "-":result=substract(a,b);
break;
case "*":result=multiply(a, b);
break;
case "/":result=divide(a, b);
break;
}
System.out.print(a+mark+b+"="+result);
scanner.close();
scanner2.close();
scanner3.close();
}
public static int add(int a,int b){
return a+b;
}
public static int substract(int a,int b){
return a-b;
}
public static int multiply(int a,int b){
return a*b;
}
public static double divide(int a,int b){
return (double)(a/b);
}
}
用Java基础语句实现计算器的基本功能(利用switch进行用户交互)
于 2022-02-13 21:21:26 首次发布