有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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*所以我不能跑得很好


共 (4) 个答案

  1. # 1 楼答案

    您需要将if语句包装到这些内容中-->;“{}”

    所以它是这样的:

    if(statement){
       //then do someting
    }else{
       //then do something else
    }
    
  2. # 2 楼答案

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

  3. # 3 楼答案

    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块的末尾添加一个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区分大小写,这意味着“优秀”不等于“优秀”

    您可以使用String#equalsIgnoreCase

  4. # 4 楼答案

    您必须确保,如果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();
    }
    

    }