此处不允许使用变量声明。爪哇

时间:2018-08-03 08:44:22

标签: java

代码如下:

public class CarLoan {
    public static void main(String[] args) {
        int carloan = 10000;
        int loanlengh = 3;
        int interestrate = 5;
        int downpayment= 2000;
        if (loanlengh <= 0 || interestrate <= 0);   
        System.out.printl("no");
        else if (downpayment > carloan);
        System.out.printl("thats not how it works");

        else
            int remainingbalance = carloan -downpayment;
        int months = loanlengh * 12;
        int monthlybalance = remainingbalance / months;
        int interest = placeholder /100;
        int placeholder = (monthlybalance * interestrate);
        int monthlypayment = monthlybalance + interest;
        System.out.println(monthlypayment);
    }
}

以下是错误:

  

CarLoan.java:13:错误:此处不允许变量声明int剩余余额= carloan-首付;

我没收到问题。

已修复的错误:

  

CarLoan.java:9:错误:“ else”不带“ if”       否则(首付>贷款);

5 个答案:

答案 0 :(得分:1)

问题出在您正在执行的if statement行中:

if (loanlengh <= 0 || interestrate <= 0);

请注意在末尾使用分号;。因此,if语句到此结束。 如错误所述:

  

错误:“ else”不带“ if”

出现错误。

答案 1 :(得分:1)

需要删除下面两行的;

if (loanlengh <= 0 || interestrate <= 0);   

else if (downpayment > carloan);

答案 2 :(得分:1)

;if语句之后删除else if。并在{ }之后添加花括号else,以在else部分中包含所有必要的行。

if (loanlengh <= 0 || interestrate <= 0) // removed `;`
    System.out.printl("no");
else if (downpayment > carloan) // removed `;`
    System.out.printl("thats not how it works");
else { 
    ... // added curly braces `{ }`
}

您应该更好地格式化代码以避免此类错误。

并尝试始终将大括号{ }if-else一起使用,会有所帮助。

if (loanlengh <= 0 || interestrate <= 0) {
    System.out.printl("no");
} else if (downpayment > carloan) {
    System.out.printl("thats not how it works");
} else {
    ...
}

答案 3 :(得分:0)

if (loanlengh <= 0 || interestrate <= 0) // removed `;`
    System.out.printl("no");
else if (downpayment > carloan);
    System.out.printl("thats not how it works");

您已经在if语句的末尾添加了semicolon

答案 4 :(得分:0)

您的问题在第7、9行以及else语句中。 删除第7行和第9行的;

在其他地方进行校正(此处是第12行),它将解决该问题。我已经测试过。

 else
       {int remainingbalance = carloan -downpayment;}

如果不能正确控制流量,则使用

if (condition){
 //statements here
}
else if (condition){
 //statements here
}
else {
//statements here
}