求能实现加减乘除的java程序(输入1,2,3,4,分别对应实现加,减,乘,除,输入0返回)
1个回答
展开全部
package swingTest;
import java.util.InputMismatchException;
import java.util.Scanner;
public class numberinput {
public static double jia(double n1,double n2){ //加
return n1+n2;
}
public static double jian(double n1,double n2){ //减
return n1-n2;
}
public static double chen(double n1,double n2){//chen
return n1*n2;
}
public static double chu(double n1,double n2){ //除
if(n2 == 0){
System.out.println("除数不能为零");
return 0;
}
return n1/n2;
}
public static void r(int n){
switch (n) {
case 1:
System.err.println("请输入数字");
System.err.println("答案" + jia(input(), input()));
break;
case 2:
System.err.println("请输入数字");
System.err.println("答案" + jian(input(), input()));
break;
case 3:
System.err.println("请输入数字");
System.err.println("答案" + chen(input(), input()));
break;
case 4:
System.err.println("请输入数字");
System.err.println("答案" + chu(input(), input()));
break;
case 0:
start();
break;
default:
break;
}
}
public static double input(){
Scanner sc = new Scanner(System.in);
double d = 0;
try{
d = sc.nextDouble();
}catch(InputMismatchException e){
System.out.println("输入错误,已停止");
}
return d;
}
public static void start(){
while(true){
System.out.println("请选择 1 , 2 , 3 , 4 , 0");
Scanner sc = new Scanner(System.in);
try{
r(sc.nextInt());
}catch(InputMismatchException e){
System.out.println("输入错误,已停止");
}
}
}
public static void main(String[] args) {
start();
}
}
大概是这样子了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |