0

我试图在这段代码中插入一个“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 );
        }   

谢谢。

4

4 回答 4

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;
}

我认为你也会更安全地使用longoverint来防止任何可能的溢出

挑剔

您也在反复转换annual价值。虽然它没有什么问题,但它确实倾向于使代码混乱并使其有点难以阅读。它会建议将其转换一次并简单地重新使用结果值,例如......

int annualAmount = Integer.parseInt(annual);
if (annualAmount < 92416_02) {
    //...
    compensation = annualAmount * commission + salary;
} else if (annualAmount > 92416_02) {
    //...
    compensation = annualAmount * commission + salary;
}
于 2013-10-28T05:31:00.733 回答
0

使用浮点数而.不是_,例如:float commission = 38.28;

于 2013-10-28T05:19:59.713 回答
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 );
    }
}
于 2013-10-28T05:26:24.130 回答
0

您的代码存在以下问题:

  1. 局部变量重复(工资、补偿和佣金被声明两次)。如果要为已经存在的变量赋值,则不应int在变量名之前指定类型(此处)。
  2. 你的乘法无效。37_28 * 1_32492096. 下划线根本不重要。您可能必须将结果除以 100 才能使其具有逻辑意义。
  3. 当年度正好是 92416_02 时,您的代码无法处理这种情况。删除该else if子句并初始化您的commission,或者只使用else而不跟随if. 此外,由于有很多常见的行,您可以将它们移出 if 块。

另请注意,用户必须输入他们的年乘以 100,因为 parseInt 无法识别下划线。否则,此代码可能会执行您想要的操作: System.out.println("输入您的年销售额"); 字符串年度 = 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 );

PS 不要听人们建议用钱计算浮点数或双精度数 - 这是一种不好的、容易出错的做法,因为计算错误会随着时间的推移而累积。使用 int、long、BigInteger 或 BigDecimal(使用 String 构造函数)

于 2013-10-28T05:41:08.967 回答