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 else語句不工作?
將代碼包裹在花括號中{}。這與Eclipse無關。閱讀關於變量範圍。 –
如果我這樣做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); } – user3344826
然後我不能初始化raise – user3344826