java - 有人知道为什么这些 if else 语句不起作用吗?

标签 java if-statement

public class Salary
{
    public static void main (String[] args)
    {
        double currentSalary; // employee's current salary
        double raise; // amount of the raise
        double percentRaise;   // percentage of the raise
        double newSalary; // new salary for the employee
        String rating; // performance rating

        Scanner scan = new Scanner(System.in);

        System.out.print ("Enter the current salary: ");
        currentSalary = scan.nextDouble();

        System.out.print ("Enter the performance rating (Excellent, Good, or Poor): ");
        rating = scan.next();
        if (rating.equals("Excellent"))

            percentRaise = .06;   
            raise = (.06 * currentSalary);


            else if (rating.equals("Good"))

                    percentRaise = .04;
                   raise = (.04 * currentSalary); 


                   else  if (rating.equals("Poor"))

                       percentRaise = .015;
                       raise = (.015 * currentSalary);

        //Compute the raise using if ...

        newSalary = currentSalary + raise;

        //Print the results
        NumberFormat money = NumberFormat.getCurrencyInstance();
        System.out.println();
        System.out.println("Current Salary: " + money.format(currentSalary));
        System.out.println("Amount of your raise: " + money.format(raise));
        System.out.println( "Your new salary: " + money. format (newSalary) );
        System.out.println();
        scan.close();
    }
}

如果我在空白处添加 { 和 } ,那么它会说 raise 未初始化。无论我做什么,我似乎都无法弄清楚让它运行起来。现在它告诉我删除 else 让它运行,但如果我这样做,无论我写得优秀、好还是差。它的薪水是 0.015 *,所以我无法获得优秀或良好的运行。

最佳答案

if (rating.equals("Excellent"))
    percentRaise = .06;   
    raise = (.06 * currentSalary);
else if (rating.equals("Good"))

不会编译,因为 Java 将此视为...

if (rating.equals("Excellent"))
    percentRaise = .06;   

raise = (.06 * currentSalary);

else if (rating.equals("Good"))

这意味着编译器会提示没有 if 语句的 else-if

您遇到的另一个问题(当您在语句周围放置 { } 时)是因为 Java 无法确定局部变量的初始值。

这意味着 Java 根本不知道如何处理 newSalary = currentSalary + raise;,因为无法保证 raise 会被分配一个值。

您可以通过在 if-else block 末尾添加 else 条件或简单地为局部变量提供初始值来解决此问题...

    double currentSalary = 0; // employee's current salary
    double raise = 0; // amount of the raise
    double percentRaise = 0;   // percentage of the raise
    double newSalary = 0; // new salary for the employee
    String rating = ""; // performance rating

虽然这可能看起来很烦人,但最好是获得一些完全随机的值,您必须花时间尝试调试该值;)

已更新

请记住,String#equals 区分大小写,这意味着“Excellent”不等于“excellent”。

您可以使用String#equalsIgnoreCase代替

关于java - 有人知道为什么这些 if else 语句不起作用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21977479/

相关文章:

java - 样本有向图和拓扑排序代码

java - 从 github 导入后清空 SRC 文件夹

c - 为什么不能在我的 Magic Square 程序中工作

java - 数据插入数据库android后如何顺序更改textview

java - Java 8 中的 IntStream forEach 方法

java - SonarQube 5.5 支持多种语言吗?

java - 简单的Java图形程序不显示

c - 如果文件不存在,则用 C 创建并写入

Python 3 and-or vs if-else

perl - 有没有办法在 perl if 语句中使用 <=> 运算符?