【问题标题】:Wallet Transfer Method in JavaJava中的钱包转账方法
【发布时间】:2016-11-14 01:19:32
【问题描述】:

我正在学习 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;
   }

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

【问题讨论】:

    标签: java arrays stringbuilder wallet


    【解决方案1】:

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

    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;
    }
    

    【讨论】:

      【解决方案2】:

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

      首先,您不需要在单独的变量中跟踪数组中有多少“钞票”:只需调用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
      

      我希望这会有所帮助!

      【讨论】:

      • contents[contents.length] = note; 总是会抛出索引错误!
      • 确实如此。我会建议他使用列表,这将简化他的生活。
      • @schwobaseggl 我相应地编辑了代码,展示了这将如何大大减少他必须做的工作量。但是,如果他想坚持使用数组,您的答案仍然在这里。
      • 还有几个问题。 donor 应该是 Wallet 实例。这看起来非常像一个训练练习,它应该处理在填充接收器时清空捐赠者钱包。此外,数组的使用似乎是教义诱导的;)
      • 是的。我假设他能够将代码正确地放在他的不同类中。
      猜你喜欢
      • 2018-01-28
      • 2023-01-27
      • 2019-12-02
      • 2021-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 2019-01-13
      相关资源
      最近更新 更多