【问题标题】:variable declaration not allowed here. java [duplicate]这里不允许变量声明。 java [重复]
【发布时间】:2018-08-03 18:33:58
【问题描述】:

代码如下:

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 remainingbalance = carloan -downpayment;

我不明白这个问题。

修正错误:

CarLoan.java:9:错误:没有“if”的“else” else if (首付 > carloan);

【问题讨论】:

  • if (loanlengh &lt;= 0 || interestrate &lt;= 0); 和else if (downpayment &gt; carloan);需要去掉;
  • 请不要使用没有花括号的if、else和else if{}。特别是作为初学者,您很容易遇到这样的错误。没有大括号,只有下一行属于if。因此,对于您的最后一个else,只有以下行在里面,其余的又在else 之外。另外,请更正您的缩进。

标签: java


【解决方案1】:

问题出在你的if statement 行,你正在做的事情:

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

注意末尾的分号;。因此,if 语句到此结束。 正如错误所述:

错误:没有“if”的“else”

因此错误。

【讨论】:

    【解决方案2】:

    以下两行需要删除;

    if (loanlengh <= 0 || interestrate <= 0);   
    
    else if (downpayment > carloan);
    

    【讨论】:

      【解决方案3】:

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

      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 {
          ...
      }
      

      【讨论】:

        【解决方案4】:

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

        在 else(此处为第 12 行)进行此更正,它将解决它。我已经测试过了。

         else
               {int remainingbalance = carloan -downpayment;}
        

        使用 if else 正确控制流程

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

        【讨论】:

          【解决方案5】:
          if (loanlengh <= 0 || interestrate <= 0) // removed `;`
              System.out.printl("no");
          else if (downpayment > carloan);
              System.out.printl("thats not how it works");
          

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

          【讨论】:

          • 请在文本中添加原始海报代码中的错误以及您为解决问题所做的工作。只包含代码的答案不是很有用。
          猜你喜欢
          • 1970-01-01
          • 2015-09-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-08-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多