简体   繁体   English

如何在新方法中使用来自两种不同方法的变量?

[英]How do I get to use variables from two different methods, in a new method?

I have written a code which reads in two text files in seperate methods.我编写了一个代码,它以单独的方法读取两个文本文件。

These are the files:这些是文件:

Accountnumbers and balances帐号和余额

0015      112.34
0020     4000.00
0034     9345.89
0069      723.50
0085     1500.00
0091     8237.31
0128       29.75
0129     -483.14
0135     2908.83
0189    10045.90
0251     5700.10
0396       29.75
0404     3299.99
0563     1000.02
2678    -5679.23
4561       12.00
8888        0.00
9999     9876.78

Accountnumbers, a deposit (1) or withdrawal (2), and the amount帐号、存款 (1) 或取款 (2) 以及金额

0015   1   2000.00
2678   1   5000.00
0189   1    250.00
0034   2    500.00
0085   1    375.00
0404   2     72.49
0128   2     30.00
0189   1    250.00
4561   1     10.00
0020   2    189.70
0015   2   1000.00
0010   1    500.00

From these files I created different variables, also in two different methods.从这些文件中,我创建了不同的变量,也是用两种不同的方法。 Now, I want to create a new method, in which I work with variables from the other two methods.现在,我想创建一个新方法,在其中我使用其他两种方法中的变量。

Here is my code with an extra class.这是我的代码,带有额外的 class。

package Bank;

import java.util.Scanner;
import java.io.PrintStream;
import ui.UIAuxiliaryMethods;

class Bank {

    PrintStream out;

     Bank() { 
            out = new PrintStream(System.out); 
            }

     void readAccountsIn() {
         Scanner fileScanner = UIAuxiliaryMethods.askUserForInput().getScanner();

         while(fileScanner.hasNext()) {
             String Account = fileScanner.nextLine();
             readAccountLine(Account);
         }
     }

     void readMutationsIn() {
         Scanner fileScanner1 = UIAuxiliaryMethods.askUserForInput().getScanner();
         while(fileScanner1.hasNext()) {
             String Mutation = fileScanner1.nextLine();
             readMutationLine(Mutation);
         }
     }


     void readAccountLine(String Account) {
         String[] parts = Account.split("\\s+");
         String account1 = parts[0];
         String balance1 = parts[1];
         double balance = Double.parseDouble(balance1);
             Accounts line = new Accounts(account1, balance);
             out.printf("%s %.2f \n",line.account1, line.balance);
         }

     void readMutationLine(String Mutation) {
         String[] parts = Mutation.split("\\s+");
         String account = parts[0];
         String type1 = parts[1];
         String mutation1 = parts[2];
         int type = Integer.parseInt(type1);
         double mutation = Double.parseDouble(mutation1);
         Mutations line1 = new Mutations(account, type, mutation);
         out.printf("%s %d %.2f \n",line1.account, line1.type, line1.mutation);
     }

     void start() {
         readAccountsIn();
         readMutationsIn();
        }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new Bank().start();
    }

}

package Bank;

class Accounts {

    String account1;
    double balance;

    Accounts(String account1, double balance){

        this.account1 = account1;
        this.balance = balance;
    }

}

I want to create a new method called Summary() in which I use all the variables (account1 and balance) from readAccountline() and (account, type, mutation) readMutationLine().我想创建一个名为 Summary() 的新方法,其中我使用 readAccountline() 和(帐户、类型、突变)readMutationLine() 中的所有变量(account1 和 balance)。 Is there any way to do this?有没有办法做到这一点?

First name your class Account and use accountName for your string, the model should reflect the real use首先命名您的 class Account并使用accountName作为您的字符串,model 应该反映真实用途


Your Bank should hve a list of Account to store and use them您的Bank应该有一个Account列表来存储和使用它们

class Bank {

    PrintStream out;
    List<Account> accounts;
    Bank() { 
        out = new PrintStream(System.out); 
        accounts = new ArrayList<>();
    }
}

Then when creating Account instanes, save them然后在创建Account实例时,保存它们

void readAccountLine(String account) {
   //...
   Account acc = new Account(account1, balance);
   accounts.add(acc);
}

Then you could write the toString method that would summarize the Bank's content然后你可以编写toString方法来总结银行的内容

@Override
public String toString() {
    StringBuilder sb = new StringBuilder("Bank");
    for (Account a : accounts)
        sb.append(a);         // you may write the toString() for Account too
    return sb.toString();
}

And use like:并使用如下:

public static void main(String[] args) {
    Bank b = new Bank();
    b.start();
    System.out.println(b); // will call toString();
}

Then do the same for Mutations然后对Mutations做同样的事情

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用来自不同方法的变量 - How do I use variables from different methods 如何在java中使用来自不同方法的变量? - How do I use variables from different method in java? 如何在两个不同的方法中使用相同的变量而不使这些变量成为全局变量? - How do I use same variable inside two different methods without making those variables global? 如何将这些变量从不同方法传递到其他方法 - How do I pass these variables from different methods in to other methods 如何在Java中的其他方法中使用此构造函数中的变量? - How do I use variables from this constructor in other methods in Java? 如何使用 on 方法获取两个不同大小的数组列表的大小? - How do I use on method to get the sizes of two different-sized arrayslists? 如何使用在不同类中实例化的对象的方法? - How do I use methods from an object instantiated in a different class? 如何制作一种计算方法,该方法可以从4种以上的方法中获取变量进行计算? - How do I make one method that does calculations take variables from 4+ methods to do the calculations with? 如何从两个不同的循环将数据放入构造函数 - How do I get data into a constructor from two different loops 如何在此类中使用公共变量和方法? - How do I use the public variables and methods in this class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM