用Java

时间:2017-01-05 14:45:41

标签: java java.util.scanner do-while

此代码不起作用。我得到以下错误(在eclipse中)我似乎无法解决:

  

语法错误,插入" :: 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) ;
    };
}

1 个答案:

答案 0 :(得分:-1)

变量interest被声明两次。

以下是代码的略微清理版本:

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("press 'N' to exit");
        String input = in.next();
        double interest = 0;    //to define interest

        do
        {
            interest = balance * interestRate / 100;
            balance += interest;
            year++; // print current balance
        }
        while (input.equals("N"));

        System.out.println("interest: " + interest + "balance: " + balance + "year: " + year) ;
    }
}