繁体   English   中英

任何人都知道为什么这些if else声明不起作用?

[英]Anyone know why these if else statement wont work?

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

如果我添加{和}空格,那么它表示加注未初始化。 无论我做什么,我似乎无法弄清楚它是否正在运行。 现在它告诉我删除其他让它运行,但如果我不管我写的是优秀的,好的还是差的。 它的工资为.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"))

这意味着编译器会抱怨else-if没有if语句。

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

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

您可以通过在if-else块的末尾添加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代替

如果你的if语句包含多行代码,你必须确保它包含在大括号中。

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

}

你需要将if语句包装成这些东西--->“{}”

所以它是这样的:

if(statement){
   //then do someting
}else{
   //then do something else
}

如果只包含一个命令,则只能编写没有大括号的if语句,如果有多个命令(多行),则需要将这些命令括在大括号中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM