用java(带参方法) 提示用户输入两个整数 并选择加减乘除 输出运算结果运算结果 要简单的。定义方法ope()
展开全部
public class Test {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("请输入第一个数:");
int number_1 = input.nextInt();
System.out.print("请输入第二个数:");
int number_2 = input.nextInt();
System.out.print("请输入运算符号:");
String sign = input.next();
try {
int result = open(number_1, number_2, sign);
System.out.print("运算的结果为:");
System.out.println(result);
} catch (RuntimeException e) {
System.out.println(e.getMessage());
}
}
private static int open(int number_1, int number_2, String sign) {
int result = -1;
if(sign.equals("+")) {
result = number_1 + number_2;
} else if(sign.equals("-")) {
result = number_1 - number_2;
} else if(sign.equals("*")) {
result = number_1 * number_2;
} else if(sign.equals("/")) {
try {
result = number_1 / number_2;
} catch (ArithmeticException e) {
System.out.println("除数不能为0");
}
} else {
throw new RuntimeException("输入的运算符号不正确");
}
return result;
}
}
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("请输入第一个数:");
int number_1 = input.nextInt();
System.out.print("请输入第二个数:");
int number_2 = input.nextInt();
System.out.print("请输入运算符号:");
String sign = input.next();
try {
int result = open(number_1, number_2, sign);
System.out.print("运算的结果为:");
System.out.println(result);
} catch (RuntimeException e) {
System.out.println(e.getMessage());
}
}
private static int open(int number_1, int number_2, String sign) {
int result = -1;
if(sign.equals("+")) {
result = number_1 + number_2;
} else if(sign.equals("-")) {
result = number_1 - number_2;
} else if(sign.equals("*")) {
result = number_1 * number_2;
} else if(sign.equals("/")) {
try {
result = number_1 / number_2;
} catch (ArithmeticException e) {
System.out.println("除数不能为0");
}
} else {
throw new RuntimeException("输入的运算符号不正确");
}
return result;
}
}
2017-08-27
展开全部
publicfloatcalc(inta,intb,Strings){if("+".equals(s))returna+b;if("-".equals(s))returna-b;if("*".equals(s))returna*b;if("/".equals(s))returna/b;if("%".equals(s))returna%b;}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询