簡體   English   中英

Java超出范圍

[英]Java out of scope

除了一個問題,我的程序幾乎完成了。 我在for循環中遇到范圍外的問題。 該計划的目標是使用戶輸入的金額和期限的每月利息增加。

以3年期,利率為5%的本金$ 5000計算的輸出示例為:

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

在循環開始之前聲明它們,以便它們將在循環內部及其之后存在:

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

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

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

// ...

在我的回答中,我假設當您說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();
    }
}

因此,在您遇到的問題中,您需要在要使用它的范圍內,在要使用它的位置上方聲明變量。 然后不再聲明。 因此在循環之前聲明。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM