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。无论我做什么,我似乎无法弄清楚它是否正在运行。现在它告诉我删除其他让它运行,但如果我不管我写的是优秀的,好的还是差的。它的工资为.015 *,所以我无法获得优秀或良好的支持。
答案 0 :(得分:2)
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
会为其分配值。
您可以通过在else
块的末尾添加if-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
代替
答案 1 :(得分:0)
如果你的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();
}
}
答案 2 :(得分:0)
你需要将if语句包装成这些东西---&gt; “{}”
所以它是这样的:
if(statement){
//then do someting
}else{
//then do something else
}
答案 3 :(得分:0)
如果只包含一个命令,如果你有多个命令(多行)需要将这些命令括在大括号中,你只能编写if语句而不用大括号