2013-10-28 172 views
0

我试图在此代码中插入“if”语句,但它不能很好地工作。其固定工资和奖励(37.28 * 1.32)将给予94,261.02一次。低于94,261.02只是一个普通的37.28佣金。所有iny“int”行用红色加下划线显示!分数。所以我试图找出问题:如何使用“if”语句

 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

@MadProgrammer Java 1.7允许在数值中使用_,以获得更好的表示/可读性。 – Batty

+0

自编译器版本“1.6”以来我们不支持使用'_'的数字,现在我们就像'7' ... –

+0

@Batty严重吗?这使得更好的介绍:P – MadProgrammer

回答

0

使用花车和.代替_,如:float commission = 38.28;

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); 
    } 
} 
+0

将它改为double可以帮助,但它会弄乱我的代码,当我插入它。我的整数补偿线以红色加下划线 –

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

我想你也将使用longint,以防止任何可能的溢出会更安全

尼特挑

你也一再地转换annual值。虽然它没有什么问题,但它确实会使代码混乱并使其难以阅读。它会建议将其转换一次,简单地重新使用所得到的值,例如...

int annualAmount = Integer.parseInt(annual); 
if (annualAmount < 92416_02) { 
    //... 
    compensation = annualAmount * commission + salary; 
} else if (annualAmount > 92416_02) { 
    //... 
    compensation = annualAmount * commission + salary; 
} 
+0

+1好干净的描述。 –

0

你的代码具有以下问题:

  1. 局部变量重复(工资,报酬和佣金被宣布两次)。如果要将值分配给已存在的变量,则不应在变量名称前面指定类型(此处为int)。
  2. 您的乘法无效。 37_28 * 1_32给出492096。下划线根本不重要。你可能不得不将结果除以100来赋予它逻辑意义。
  3. 当年份恰好是92416_02时,您的代码无法处理这种情况。删除else if子句并初始化您的commission,或者仅使用else而不遵循if。另外,由于有很多共同的线,你可以将它们从块中移出。

还要注意,用户必须输入他们的年度乘以100,因为parseInt不会识别下划线。否则,此代码可能可以做你想做的事情: System.out.println(“输入你的年销售额”); 字符串年度=输入。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构造函数)

+0

嗯,我喜欢这个如何与我没有需要使用委员会的其他声明。谢谢。它运行但符合以下错误:线程“main”中的异常java.lang.RuntimeException:不可编译的源代码 - 变量工资已经在方法main(java.lang.String [])中定义,在commission.main()中已经定义了 \t。 java:24) Java结果:1 –

+0

@MoMarks,这是因为_variable工资已被定义。看看你的整个源代码,这段代码编译没有问题。 –