2013-06-25 63 views
-2

我嘗試使用下面的API來調用while循環動畫:在循環錯誤調用方法

public void animate(String currentTransaction, double currentAmount, double currentBalance) 

我想以此來撥打電話:

animate( currentTransaction, currentAmount, currentBalance); 

但是,我不斷收到錯誤。我究竟做錯了什麼?

的錯誤是:

currentTransaction cannot be resolved to a variable and 
currentBalance cannot be resolved to a variable 

下面發佈是我的代碼:

import javax.swing.JOptionPane; 
import java.text.DecimalFormat; 
import javax.swing.JFrame; 
import java.awt.Graphics; 
import java.util.StringTokenizer; 
import java.util.Scanner; 
import java.io.File; 
import java.io.FileReader; 
import java.io.FileWriter; 
import java.io.BufferedWriter; 
import java.io.IOException; 
import java.io.FileNotFoundException; 

public class Accounting extends JFrame { 
private BankAccount bankAccount; 

public Accounting() { 
    bankAccount = new BankAccount(getBackground()); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setSize(300, 300); 
    setVisible(true); 
} 

public void balanceCheckBook() { 
    double balance; 
    double currentAmount; 
    String nextLine; 
    StringTokenizer st; 
    String transactionName; 
    Scanner scan = null; 
    try { 
     scan = new Scanner (new FileReader("transactions.txt")); 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    Scanner callParse; 
    String trans; 
    while (scan.hasNextLine()) { 
     trans = scan.nextLine(); 
     scan.useDelimiter(":"); 
     callParse = new Scanner(trans); 
     callParse.useDelimiter(":"); 
    } 
    try { 
     scan = new Scanner (new FileReader("checkbook.txt")); 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    Scanner callParse2; 
    String check; 
    while (scan.hasNextLine()) { 
     check = scan.nextLine(); 
     scan.useDelimiter(":"); 
     callParse2 = new Scanner(check); 
     callParse2.useDelimiter(":"); 
    } 

    animate( currentTransaction, currentAmount, currentBalance); 
} 

public void animate(String currentTransaction, double currentAmount, double currentBalance) { 
    // set the current transaction in the bankAccount object 
    if (currentTransaction.startsWith("Ch")) 
     bankAccount.setCurrentTransaction(new Check(currentAmount)); 
    else if (currentTransaction.startsWith("With")) 
     bankAccount.setCurrentTransaction(new Withdrawal(currentAmount)); 
    else if (currentTransaction.startsWith("Dep")) 
     bankAccount.setCurrentTransaction(new Deposit(currentAmount)); 
    else 
     bankAccount.setCurrentTransaction(new UnknownTransaction(currentAmount)); 
    // set the currentBalance data member in the bankAccount object 
    bankAccount.updateBalance(currentBalance); 

    repaint(); 
    try { 
     Thread.sleep(3000); // wait for the animation to finish 
    } catch (Exception e) { 
    } 
} 

public void paint(Graphics g) { 
    super.paint(g); 
    bankAccount.draw(g); 
} 

public static void main(String [] args) { 
    Accounting app = new Accounting(); 
    app.balanceCheckBook(); 
} 
} 
+2

什麼是錯誤?你可以發佈嗎? – Smit

+0

你能發送錯誤嗎? –

+0

'currentTransaction'未定義 – nachokk

回答

3

變量currentTransaction不會在你的函數balanceCheckBook的範圍存在。如果是這種情況,您需要傳遞一個變量來運行balanceCheckBook或在名爲currentTransaction的類中創建一個成員變量。

換句話說,當你與可變currentTransaction未定義調用函數animate在功能balanceCheckBook,編譯器將永遠不會知道哪個值你要傳遞給函數animate

animate( currentTransaction, currentAmount, currentBalance); 

因此,您需要定義currentTransaction並讓Java知道要傳遞什麼值。

0

currentTransaction未在該方法的範圍內定義,也不是該方法的參數。您需要可以傳遞變量作爲參數,或使其實例變量。該方法聲明爲:

public void animate(String currentTransaction, double currentAmount, 
        double currentBalance) 

因此,你需要用String,一個double和另一double叫它:animate(String, double ,double),而你叫它作爲animate( currentTransaction, currentAmount, currentBalance);,但我不知道你在哪裏定義了currentTransactioncurrentBalance

+0

我會如何通過變量作爲參數? – StackP

0

對我來說,它看起來像你應該使用變量balance代替currentBalancetranstransactionName代替currentTransaction