Java 使用If-Else语句时出现Else错误
如果50美元以内没有退款,如果超过50美元,那么总税额减去50美元,出于其他原因,试图计算出来,这是不可取的。思想?谢谢Java 使用If-Else语句时出现Else错误,java,if-statement,Java,If Statement,如果50美元以内没有退款,如果超过50美元,那么总税额减去50美元,出于其他原因,试图计算出来,这是不可取的。思想?谢谢 import java.util.Scanner; public class germany { public static void main (String[] args) { //constants final double Tax_Rate = 0.10; int Max_Tax = 50;
import java.util.Scanner;
public class germany {
public static void main (String[] args) {
//constants
final double Tax_Rate = 0.10;
int Max_Tax = 50;
Scanner reader = new Scanner(System.in);
//Variables
double purchases;
double taxespaid;
double taxOwed;
//Request Input
System.out.print("Enter your total amount of purchases:");
purchases = reader.nextDouble();
taxespaid = purchases * Tax_Rate;
taxOwed = taxespaid - Max_Tax;
if (Max_Tax <=taxespaid); {
System.out.println("You are within $50 limit");
} else if (Max_Tax <=taxespaid);
System.out.println("The amount is owed to you is $" + taxOwed);
}
}
你现在有
if (Max_Tax <=taxespaid); {
System.out.println("You are within $50 limit");
} else if (Max_Tax <=taxespaid);
System.out.println("The amount is owed to you is $" + taxOwed);
这相当于
if (Max_Tax <=taxespaid) {
}
System.out.println("You are within $50 limit");
} else if (Max_Tax <=taxespaid) {
}
System.out.println("The amount is owed to you is $" + taxOwed);
所以你的System.out.printlns总是执行,这不是你想要的。除了System.outs之外,当满足任何一个条件时,都不会执行与空块等效的操作,这也不是您想要的
消除条件与其开头大括号{之间的分号,并始终使用大括号。代码中似乎有语法错误,请按如下方式键入:
if (Max_Tax <= taxespaid)
{
System.out.println("You are within $50 limit");
}
else
{
System.out.println("The amount is owed to you is $" + taxOwed);
}
对于一些Java教程和基本编程,您可以访问:删除每个if和else if条件后的分号;它们充当语句体,println语句始终在执行。您使用了两次相同的条件。Geez louise 13次投票支持一条注释。也许我应该转到Java.C相比之下,这是一个贫瘠的荒原。这可能是如何编译的重复?我认为应该有一个编译错误,如果…这是一个语义错误,而不是语法错误。是的,你是对的!这是一个语义错误。你指出这一点很好。公平地说,两者都有。语法错误是有一个else语句挂起而不是att指定为if语句。语义错误是分号的有效语法,但不是您想要的。