2017-01-05 136 views
-8

此代碼不起作用。我得到我似乎無法以下錯誤(在Eclipse)來解決:在Java中打印循環結果

語法錯誤,插入「:: IdentifierOrNew」完成ReferenceExpression
語法錯誤,插入「;」完成BlockStatements
重複的局部變量的興趣

import java.util.Scanner; 

public class DoWhile { 

    public static void main (String[] args) 
    { 
     Scanner in = new Scanner(System.in); 
     System.out.print("balance: "); 
     int balance = in.nextInt(); 
     System.out.print("interestRate: "); 
     double interestRate = in.nextDouble(); 
     System.out.print("year: "); 
     int year = in.nextInt(); 

     System.out.print("input: "); 
     String input = in.next(); 
     Integer interest = null; //to define interest  

     do 
     { 
      double interest = balance * interestRate/100; 
      balance += interest; 
      year++; // print current balance 
     } 
     while (input.equals("N")); 
     System.out.println("interest: " + interest + "balance: " + balance + "year: " + year) ; 
    }; 
} 
+2

如果您的固定縮進它會更容易看出問題。 – khelwood

+0

'interest'必須定義爲'Double':'Double interest = null; do { interest = balance * interestRate/100; ' – DimaSan

回答

-1

變量interest聲明兩次。

這裏是你的代碼稍微清理版本:

​​
+0

確實,修正了縮進。 – cringe