使用If Else语句时出现其他错误

时间:2014-07-09 19:39:31

标签: 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;

        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);

    }
}

2 个答案:

答案 0 :(得分:1)

你目前有

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.println总是执行,这不是你想要的。并且(除了System.out s)没有(相当于空块)在满足任一条件时执行,这也不是你想要的。

消除条件和它们的大括号({)之间的分号,总是使用大括号。

答案 1 :(得分:0)

您的代码似乎存在语法错误,请尝试输入:

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教程和基本编程,您可以访问:http://www.learnjavaonline.org