[英]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();
}
}
if i add { and } where the whitespace is then it says raise is not initialized. 如果我添加{和}空格,那么它表示加注未初始化。 No matter what i do i cant seem to figure out to get it running.
无论我做什么,我似乎无法弄清楚它是否正在运行。 Right now it tells me to delete the else to let it run but if i do no matter i write excellent, good, or poor.
现在它告诉我删除其他让它运行,但如果我不管我写的是优秀的,好的还是差的。 It does .015 * salary so i cant get excellent or good to run.
它的工资为.015 *,所以我无法获得优秀或良好的经营能力。
if (rating.equals("Excellent"))
percentRaise = .06;
raise = (.06 * currentSalary);
else if (rating.equals("Good"))
Won't compile because Java sees this as... 不会编译,因为Java认为这是......
if (rating.equals("Excellent"))
percentRaise = .06;
raise = (.06 * currentSalary);
else if (rating.equals("Good"))
Meaning that the compiler will complain about the else-if
without a if
statement. 这意味着编译器会抱怨
else-if
没有if
语句。
The other problem you're having (when you place { }
around the statements) is because Java makes no determination about what the initial value of a local variable will have. 您遇到的另一个问题(当您在语句周围放置
{ }
)是因为Java无法确定局部变量的初始值将具有什么。
This means Java simply doesn't know what to do with newSalary = currentSalary + raise;
这意味着Java根本不知道如何处理
newSalary = currentSalary + raise;
as there is no guarantee that raise
will have a value assigned to it. 因为无法保证
raise
会分配给它的值。
You could overcome this by adding an else
condition to the end of your if-else
block or simply supplying an initial value to your local variables... 您可以通过在
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
And while it might seem annoying, it's better then getting some completely random value which you would have to spend time trying to debug ;) 虽然它看起来很烦人,但最好还是获得一些你需要花时间去调试的完全随机的值;)
Updated 更新
Remember, String#equals
is case sensitive, this means "Excellent" is not equal to "excellent". 请记住,
String#equals
区分大小写,这意味着“Excellent”不等于“excellent”。
You could use String#equalsIgnoreCase
instead 您可以使用
String#equalsIgnoreCase
代替
You have to make sure that if your if statement contains more than one line of code, it is wrapped in braces. 如果你的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();
}
} }
You need to wrap if statements into these things ---> "{}" 你需要将if语句包装成这些东西--->“{}”
so its like this: 所以它是这样的:
if(statement){
//then do someting
}else{
//then do something else
}
如果只包含一个命令,则只能编写没有大括号的if语句,如果有多个命令(多行),则需要将这些命令括在大括号中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.