这是Java,我正在使用TextWrangler。我收到了几个错误,包括'else'没有'if'错误,无法找出原因。我还不得不拿出“公共课税”{ public static void main(String [] args){“因为它告诉我他们没有正确编码。我得到的错误的一些例子包括:
Tax.java:85: error: 'else' without 'if'
else(taxable <= 186475)
Tax.java:85: error: ';' expected
else(taxable <= 186475)
^
Tax.java:85: error: not a statement
else(taxable <= 186475)
^
import java.util.Scanner;
java.util.Scanner input = new java.util.Scanner(System.in);
double tax;
System.out.print("Please enter filing status: ");
int status = input.nextInt();
System.out.println("Please enter taxable income: ");
double taxable = input.nextDouble();
if(status == 0){
if(taxable <= 8350)
tax = taxable * .10;
System.out.println("Tax is " + tax);
else if(33950 >= taxable && taxable >= 8351)
taxable = taxable - 8350;
tax = taxable * .15 + 8350 * .10;
System.out.println("Tax is " + tax);
else if(82250 >= taxable && taxable >= 33951)
taxable = taxable - 33950 - 8350;
tax = taxable * .25 + 33950 * .15 + 8350 * .10;
System.out.println("Tax is " + tax);
else if(171550 >= taxable && taxable >= 82251)
taxable = taxable - 82250 - 33950 - 8350;
tax = taxable * .28 + 82250 * .25 + 33950 * .15 + 8350 * .10;
System.out.println("Tax is " + tax);
else if(372950 >= taxable && taxable >= 171551)
taxable = taxable - 171550 - 82250 - 33950 - 8350;
tax = taxable * .33 + 171550 * .28 + 82250 * .25 + 33950 * .15 + 8350 * .10;
System.out.println("Tax is " + tax);
else(taxable <= 372951)
taxable = taxable - 372950 - 171550 - 82250 - 33950 - 8350;
tax = taxable * .35 + 372950 * .33 + 171550 * .28 + 82250 * .25 + 33950 * .15 + 8350 * .10;
System.out.println("Tax is " + tax);
}
答案 0 :(得分:3)
始终用花括号括起 所有 代码块,尤其是在开始时,包括所有if块,所有循环,所有控制结构。
你错误的缩进是在愚弄你让你认为if块只包含一行代码时包含3行代码。
你看到了:
if (taxable <= 8350)
tax = taxable * .10;
System.out.println("Tax is " + tax);
else if(33950 >= taxable && taxable >= 8351)
taxable = taxable - 8350;
tax = taxable * .15 + 8350 * .10;
System.out.println("Tax is " + tax);
真的相当于:
if (taxable <= 8350) {
tax = taxable * .10;
}
System.out.println("Tax is " + tax);
// you've got a hanging else here
else if(33950 >= taxable && taxable >= 8351) {
taxable = taxable - 8350;
}
tax = taxable * .15 + 8350 * .10;
System.out.println("Tax is " + tax);
// and more hanging else's below...
所以改成它:
if (taxable <= 8350) {
tax = taxable * .10;
System.out.println("Tax is " + tax);
} else if(33950 >= taxable && taxable >= 8351) {
taxable = taxable - 8350;
tax = taxable * .15 + 8350 * .10;
System.out.println("Tax is " + tax);
} //.... and continue similarly throughout your program
答案 1 :(得分:3)
你需要在'then'子句中加上大括号。例如:
if(taxable <= 8350) {
tax = taxable * .10;
System.out.println("Tax is " + tax);
}
else .....
答案 2 :(得分:1)
如果你有if-else的多个语句,那么请使用大括号。最好总是使用牙箍。
表格Doc
块是平衡大括号之间的一组零个或多个语句,可以在允许单个语句的任何位置使用。以下示例BlockDemo说明了块的使用。
写如下:
if(status == 0){
if(taxable <= 8350){
tax = taxable * .10;
System.out.println("Tax is " + tax);
}
else if(33950 >= taxable && taxable >= 8351){
taxable = taxable - 8350;
tax = taxable * .15 + 8350 * .10;
System.out.println("Tax is " + tax);
}
....
}
答案 3 :(得分:1)
您需要为if添加花括号。
if(taxable <= 8350){
tax = taxable * .10;
System.out.println("Tax is " + tax);
}else if(33950 >= taxable && taxable >= 8351){
taxable = taxable - 8350;
tax = taxable * .15 + 8350 * .10;
System.out.println("Tax is " + tax);
}
否则,它只是第一行在if
中答案 4 :(得分:0)
因为你的if语句没有用{}的
括起来这意味着只有下一行在if语句下计数。以下行在if语句之外执行,然后“else”语句放置时没有if。
答案 5 :(得分:0)
如果IF或ELSE的主体跨越多个语句,则需要使用花括号。