1

我正在学习 Java 入门课程,并且正在 Wallet 实验室工作。

我需要将一个钱包(捐赠者钱包)的钱包内容转移到另一个钱包(接收者钱包)的末尾。

我相信我的构造函数设置正确。

    import.java.util.Arrays;

    public class Wallet
    {
       private static final int MAX = 10;  //Max possible # of banknotes in a wallet

   // instance variables       
       private int contents[]; //array of banknotes
       private int count; //number of banknotes stored in contents[]

   /**
    * Default constructor for objects of class Wallet
    */
   public Wallet()
   {
        // initialize instance variables
        contents = new int[MAX];
        count = 0;       
   }

   /**
    * Overloaded constructor for objects of class Wallet
    */
   public Wallet(int a[])
   {
       contents = new int[MAX]; 

       for (int i = 0; i<a.length; i++)
       {
            contents=a;
            if (contents[i] > 0) 
                count++;
       }
   }

但需要帮助验证我编写的以下 add() 方法是否正确:

public void add(int banknote)//Not sure if this is right
   {  
       StringBuilder sb = new StringBuilder();

       for (int i = 0; i<contents.length; i++)
                sb.append(contents[i] + ", ");

       sb.append(banknote);

       count++;

   }

或者应该是:

 contents[count] = banknote;
  count++;

我还需要将捐赠者钱包的内容转移到接收者钱包,清空捐赠者钱包并添加到接收者,我编写了以下代码,但它似乎关闭并且无法正常工作:

 public void transfer(Wallet donor)
   {

      for(int i = 0; i < count; i++)
        {
            add(donor.contents[i]);
            count++;
        }
      donor.count=0;
   }

关于我可能在哪里出错的任何指导,已经在这几个小时了..谢谢

4

2 回答 2

0

好吧,这应该可以解决一些问题:

public Wallet(int a[]) {
  contents = new int[MAX];
  count = 0;
  for (int i = 0; i < a.length; i++) {
    if (a[i] > 0)
      contents[count++] = a[i];  
      // only increase your content position for actual notes
  }

public void add(int banknote) {
  contents[count++] = banknote;  
  /* this version makes a lot more sense
   * as you actually mutate your instance */
}

public void transfer(Wallet donor) {
  for(int i = 0; i < donor.count; i++) // gotta use donor.count here
    add(donor.contents[i]);
  donor.count=0;
}
于 2016-11-14T01:42:23.180 回答
-1

更新:我建议您使用整数列表,因为这将大大简化您的代码。我在这个答案中的代码已经适应了这一点。

首先,您不需要在单独的变量中跟踪数组中有多少“钞票”:只需调用contents.length,它将返回钱包中有多少“钞票”。

然后,要将钞票添加到钱包中,我会让你的“添加”函数返回一个布尔值:如果添加了钞票,则返回 true,如果钱包没有足够的空间,则返回 false。为此,您可以执行以下操作:

static int MAX = 10;
static List<Integer> contents = new ArrayList<Integer>();

public static void main(String... args){
    add(5);
    add(10);

    transfer(Arrays.asList(1, 5, 2, 4));

    for(int note: contents){
        System.out.println(note);
    }
}

public static boolean add(int note){
    if(contents.size() >= MAX)
        return false; // The wallet is full, therefore we cannot add anything.

    contents.add(note);
    return true;
}

要转账,您只需执行以下操作:

public static void transfer(List<Integer> donor){ // You would change 'int[]' to Wallet, and the code below according to this change
    for(int i = 0; i < donor.size(); i++) // Going through all the values of the donor's wallet
        if(!add(donor.get(i))) // If there is no space left, we must stop the transfer
            return;
}

启动此类时,您的控制台应打印:

5
10
1
5
2
4

我希望这有帮助!

于 2016-11-14T01:41:48.617 回答