实现计算器 加减乘除
public class calculator {public static void main(String[] args) {
}
public static double xiangchen(double num1, double num2) {
double result = 0;
if (num1 != 0 && num2 != 0) {
result = num1 * num2;
System.out.println(result);
return result;
} else {
System.out.println(0);
}
System.out.println(result);
return result;
}
public static double xiangchu(double num1, double num2) {
double result = 0;
if (num1 != 0 && num2 != 0) {
result = num1 + num2;
} else {
System.out.println(0);
System.out.println(result);
double result1 = result;
return result1;
}
public static double xiangjian(double num1,double num2){
double result2 = 0;
if (num1 != 0 && num2 != 0) {
result = num1 - num2;
} else {
System.out.println(0);
System.out.println(result);
return result2;
}
public static double xiangchu(double num1, double num2){
result = 0;
if (num1 != 0 && num2 != 0) {
result = num1 / num2;
} else {
System.out.println(0);
System.out.println(result);
return result;
}
}
}
}
}
好久没玩论坛了,问问大家的代码格式是怎么弄出来的 , 我这个方法 为啥前面两个if不报错后面两个报错编译器时idea
代码格式在 <> 这个按钮处,或者自己输入 [/ code] 这个标签
你这里代码错误是因为你定义了两个 xiangchu,你第一个 xiangchu 函数应该是 xiangjia 吧
另外你代码很乱,最后括号啥的也是非常的混乱,我帮你改过来了,删掉了无用部分的代码
参考代码:
public class calculator {
public static void main(String[] args) {
System.out.println(xiangchen(10, 10));
System.out.println(xiangjia(10, 10));
System.out.println(xiangjian(10, 10));
System.out.println(xiangchu(10, 10));
}
public static double xiangchen(double num1, double num2) {
double result = 0;
if (num1 != 0 && num2 != 0) {
result = num1 * num2;
return result;
}
return result;
}
public static double xiangjia(double num1, double num2) {
double result = 0;
if (num1 != 0 && num2 != 0) {
result = num1 + num2;
}
return result;
}
public static double xiangjian(double num1, double num2) {
double result = 0;
if (num1 != 0 && num2 != 0) {
result = num1 - num2;
}
return result;
}
public static double xiangchu(double num1, double num2) {
double result = 0;
if (num1 != 0 && num2 != 0) {
result = num1 / num2;
}
return result;
}
} 为啥这里不用列出等于0的情况 还能正常执行不会报错的 Twilight6 发表于 2022-5-17 22:28
代码格式在这个按钮处,或者自己输入
为啥这里不用列出等于0的情况 还能正常执行不会报错的
页:
[1]