0

メソッドの 1 つに「金額」という変数がありますが、別のメソッドでそれを参照する必要があります。使用されている構文がわかりません。

コードは次のとおりです。

public static void startup()
{
    String message = "Welcome to Toronto Mutual!";
    //prompts user to enter their employee ID
    String logIn = JOptionPane.showInputDialog("Please enter your employee `enter code here`ID.");
    int employeeID = Integer.parseInt(logIn);

    String input = JOptionPane.showInputDialog("Please enter the transaction `enter code here`type, bank ID and amount all separated by a comma.");
    input=input.toUpperCase();
    if (input.equals("END")){
        JOptionPane.showMessageDialog(null,"Goodbye.");
        System.exit(0);
    }
    else
    {
        int balance = 100;
        String []transaction = new String[3];
        transaction = input.split(",");
        String type = transaction[0]; 
        int bankID = Integer.parseInt(transaction[1]);
        type=type.toUpperCase();
... // there's more but it's irrelevant

メソッドの外で変数「金額」と「銀行 ID」を使用するにはどうすればよいですか?

4

1 に答える 1

1

ふたつのやり方:

  1. amount引数を介して他のメソッドに渡す

    private void otherMethod(int amount) {...}

  2. クラススコープ内でアクセスできるようにインスタンス変数を作成する

    private int amount;

    この路線で行くならゲッターとセッターを使ったほうがいいです。

    public int getAmount() {
        return amount;
    }
    
    public void setAmount(int amount) {
        this.amount = amount;
    }
    
于 2013-06-02T16:56:19.383 に答える