在循环错误中调用方法

时间:2013-06-25 17:34:10

标签: java methods error-handling constructor while-loop

我正在尝试使用以下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( );
}
}

3 个答案:

答案 0 :(得分:3)

变量currentTransaction在函数balanceCheckBook的范围内不存在。如果是这种情况,您需要将变量传递给函数balanceCheckBook或在名为currentTransaction的类中创建成员变量。

换句话说,当您在函数animate中调用函数balanceCheckBook并且变量currentTransaction未定义时,编译器将永远不知道您要将哪个值传递给函数{{ 1}}。

animate

因此,您需要定义animate( currentTransaction, currentAmount, currentBalance ); 并让Java知道要传递的值。

答案 1 :(得分:0)

currentTransaction未在方法范围内定义,也不是方法的参数。您需要将变量作为参数传递或使其成为实例变量。声明为:

的方法
public void animate( String currentTransaction, double currentAmount, 
                     double currentBalance )

因此,您需要使用Stringdouble和另一个doubleanimate(String, double ,double)来呼叫它,而您将其称为animate( currentTransaction, currentAmount, currentBalance );,但我不需要看看你在哪里定义currentTransactioncurrentBalance

答案 2 :(得分:0)

对我来说,您似乎应该使用变量balance代替currentBalancetranstransactionName而不是currentTransaction