我试图在此代码中插入“if”语句,但它的效果不佳。一旦有94,261.02,它将获得固定工资和奖励(37.28 * 1.32)。低于94,261.02只是37.28的常规佣金。所有iny“int”行都用红色加下划线加下红色!分数。所以我试图解决这个问题:
System.out.println("Enter your annual sales");
String annual = input.nextLine();
int salary = 7550281;
int commission = 38_28;
int compensation = Integer.parseInt(annual) * commission + salary;
System.out.println("compensation is: "+compensation );
if (Integer.parseInt(annual) < 92416_02) {
int salary = 7550281;
int commission = 37_28 * 1_32;
int compensation = Integer.parseInt(annual) * commission + salary;
System.out.println("compensation is: "+compensation );
} else if (Integer.parseInt(annual) > 92416_02){
int salary = 7550281;
int commission = 38_28;
int compensation = Integer.parseInt(annual) * commission + salary;
System.out.println("compensation is: "+compensation );
}
感谢。
答案 0 :(得分:2)
很多内容将归结为您正在使用的Java版本。
目前,让我们假设您使用的是Java 7,而38_28
是一个有效的语句,您将在每个if
块中重新声明您的变量
// Declared here...
int salary = 7550281;
int commission = 38_28;
int compensation = Integer.parseInt(annual) * commission + salary;
if (Integer.parseInt(annual) < 92416_02) {
// Redeclared here...
int salary = 7550281;
int commission = 37_28 * 1_32;
int compensation = Integer.parseInt(annual) * commission + salary;
} else if (Integer.parseInt(annual) > 92416_02) {
// Redeclared here...
int salary = 7550281;
int commission = 38_28;
int compensation = Integer.parseInt(annual) * commission + salary;
}
这不是必需的。您只需要声明一次,例如......
int salary = 7550281;
int commission = 38_28;
int compensation = Integer.parseInt(annual) * commission + salary;
if (Integer.parseInt(annual) < 92416_02) {
salary = 7550281;
commission = 37_28 * 1_32;
compensation = Integer.parseInt(annual) * commission + salary;
} else if (Integer.parseInt(annual) > 92416_02) {
salary = 7550281;
commission = 38_28;
compensation = Integer.parseInt(annual) * commission + salary;
}
我认为使用long
优于int
以防止任何可能的溢出更安全
Nit Pick
您也会反复转换annual
值。虽然它没有什么问题,但它确实会使代码混乱并使其有点难以阅读。建议将其转换一次,然后简单地重复使用结果值,例如......
int annualAmount = Integer.parseInt(annual);
if (annualAmount < 92416_02) {
//...
compensation = annualAmount * commission + salary;
} else if (annualAmount > 92416_02) {
//...
compensation = annualAmount * commission + salary;
}
答案 1 :(得分:0)
使用浮点数和.
代替_
,例如:float commission = 38.28;
答案 2 :(得分:0)
我修改了你的代码,因为你没有提供完整的代码块,我按照我的想象完成了代码段。我建议你使用BigDecimal类进行更精确的计算。 “
import java.util.Scanner;
public class stack_overflow {
public static void main(String args[]){
System.out.println("Enter your annual sales");
Scanner input = new Scanner(System.in);
String annual = input.nextLine();
double salary = 7550281;
double commission = 38.28;
double compensation = Double.parseDouble(annual) * commission + salary;
System.out.println("compensation is: "+compensation );
if (Double.parseDouble(annual) < 92416.02) {
salary = 7550281;
commission = 37.28 * 1.32;
compensation = Double.parseDouble(annual) * commission + salary;
System.out.println("compensation is: "+compensation );
} else if (Integer.parseInt(annual) > 92416.02){
salary = 7550281;
commission = 38.28;
compensation = Integer.parseInt(annual) * commission + salary;
System.out.println("compensation is: "+compensation );
}
}
答案 3 :(得分:0)
您的代码存在以下问题:
int
)。37_28 * 1_32
给出492096
。下划线根本不重要。您可能需要将结果除以100以使其符合逻辑。 else if
子句并初始化commission
,或者只使用else
而不关注if
。此外,由于共有很多行,你可以将它们移出if块。另请注意,由于parseInt无法识别下划线,因此用户必须输入其年度乘以100。否则,此代码可能会执行您想要的操作: System.out.println(“输入您的年度销售额”); String annual = input.nextLine();
int salary = 7550281;
int commission = 38_28;
if (Integer.parseInt(annual) < 92416_02) {
commission = 37_28 * 1_32 / 100;
}
int compensation = Integer.parseInt(annual) * commission / 100 + salary;
System.out.println("compensation is: "+compensation );
P.S。不要听人们建议使用浮点数或双打进行金钱计算 - 这是一个糟糕的,容易出错的做法,因为计算错误会随着时间的推移而累积。使用int,long,BigInteger或BigDecimal(使用String构造函数)