Java超出范围

时间:2014-11-04 02:20:05

标签: java

除了一个问题,我的程序几乎完成了。我对for循环有一个超出范围的问题。该计划的目标是为用户输入的金额和时间复合每月利息。术语。

以5000美元本金为期3年的5%利息产出的例子是:

Month:  Interest:   Principal:
1       $20.83      $5020.83 
2       $20.92      $5041.75
etc      etc         etc 

 Starting Balance        = $ 5000.00 // having problem outputting these w/ for-loop
 Final Account Balance   = $ 5807.36 // System.out.print keeps repeating multiple times
 Total Interest Paid     = $  807.36 // but i can't use variables outside of loop

我的问题是,在我的for循环中,每次程序进入循环时,我都会一直输出起始余额,最终余额和总利息。但是如果我尝试在循环外使用变量,它就会超出范围,如果我尝试在循环外部声明变量,我就不能在循环中使用它们,因为它已经在构造函数中声明了。

有人可以给我一些提示或建议吗?

我的代码:

    public class Calculator
{

    public Calculator()
    {
        Scanner input = new Scanner(System.in);
        boolean error = false;

        while (!error){
        System.out.print("Please input the following: principal, interest rate, term >> ");
        double principal = input.nextDouble();
        double interest_rate = input.nextDouble(); 
        int term = input.nextInt();
        String Month = input.next();


         char dollar_sym = 36;

        if (interest_rate <= 0 || term <= 0 || principal <= 0) // input validation
           {
             System.out.println("The term, interest rate and principal must be greater 
             than zero");
             continue; 
           }
        if (!Month.equals("month")) // input validation
           {
             System.out.println("Please input month after term"); 
             continue; 
           }

        System.out.println("Month: " + "  Interest: " + "Principal:  "); 

        if (Month.equals("month"))
        {
           for (int month = 1; month <= term; month++)
            { 
              double interest = (principal * interest_rate / 100) / 12;
              principal = principal + interest; 

              System.out.printf("%4d      %c%5.2f    %c%5.2f\n", month, 
              dollar_sym, interest, dollar_sym, principal );

              double start_principal = principal - interest; // problem
              double final_principal = principal; // problem 
              double total_interest = interest * interest_rate; // problem

              System.out.println(" Starting balance = " + start_principal ); // problem
              System.out.println("Final account balance = " + final_principal ); // problem
              System.out.println("Total Interest Paid = " + total_interest); // problem
           } 
         }
       }
     }
   }

2 个答案:

答案 0 :(得分:1)

在循环开始之前声明它们,因此它们将存在于循环内部及之后:

double start_principal = 0;
double final_principal = 0;
double total_interest = 0;

Scanner input = new Scanner(System.in);
boolean error = false;

while (!error) {
    // ...
}

// ...

答案 1 :(得分:0)

在我的回答中,我假设当你说goes out of scope时,你的意思是你得到编译时错误。 (注意,如果您提供错误消息和导致错误消息的行,它将使答案更容易解决。)

变量的范围是指变量可访问的位置。例如,如果if语句中声明变量,则范围为 if语句。一些示例代码:

public void updateStatus(){
    Boolean shouldCheckStatus = true;//this variable is declared inside of the updateStatus method, so that is the scope of the variable
    if(shouldCheckStatus == true){
        Int hitCounter = 0;//this variable is declared inside of the if statement, so it is only accessible inside of the if statement
        //do some work
        if(hitCounter > 100){
            self.registerHits(hitCounter);//hitCounter is still accessible here, because it is still inside of the if statement
        }
        else{
            shouldCheckStatus = false;
        }
    }//close the if statement and so close the scope...
    //the hitCounter variable is no longer in scope, because we are no longer in the if statement
    //but shouldCheckStatus is still in scope, because we are still in the method
    if(shouldCheckStatus == true){
       self.callAnotherMethod();
    }
}

所以在你的问题中,你需要在你想要使用它的范围内声明你的变量。然后不再声明它。所以在循环之前声明。