【问题标题】:Adding ArrayList<String> inputs which are each referencing a double through enums添加 ArrayList<String> 输入,每个输入都通过枚举引用双精度
【发布时间】:2015-09-05 13:52:48
【问题描述】:

在这个程序中,我需要创建一个名为 purse 的 ArrayList,它将美国硬币名称作为字符串输入其中。然后我们被要求将每个输入值分配给它对应的双精度值。因此,当用户键入“penny”时,程序应该识别出字符串“penny”指的是双倍 0.01。我在 Coin 课程中通过枚举完成了这一点。现在我的任务是在我的 Purse 类中创建一个将双精度值相加的新方法。我几乎已经完成了,但我已将代码执行任务放在 AddCoin 方法中。因为我需要为此任务创建一个新方法,所以我想知道是否有办法用我现在拥有的方法来完成它。

package purse;
import java.util.Scanner;
import java.util.ArrayList;




/**
 * The Purse program creates an ArrayList called purse that gets printed out,
  reversed, and transfered into another ArrayList called purse2. 
 *     
 *               - ArrayList purse 
 *               - ArrayList purse2
 *               - Scanner coin - the Scanner that is used to type the contents of ArrayList purse
 *               - Scanner coin2- the Scanner that is used to type the contents of ArrayList purse2
 *               - String input - contains Scanner coin and is used to fill ArrayList purse
 *               - String input2- contains Scanner coin2 and is used to fill ArrayList purse2
 *               - String end - sentinel for ending the process inputting strings into Scanner coin and Scanner coin2
 *      
 */
public class Purse  
{ 
  ArrayList<String> purse = new ArrayList<>(); 



   /**
    *  fills ArrayList purse and purse2 with U.S coin names 
    *  purse gets printed out and then again is printed in reverse
    *  purse2 is printed

    */






  public void addCoin()
  { 
      double sum = 0.0;
      String end = "done";

      Scanner coin = new Scanner (System.in); 
       String input = " ";

      System.out.println("Please put as many coins of U.S currency as you like into the purse, hit the ENTER button after each coin and, type 'done' when finished: ");

      while (!input.equalsIgnoreCase ("done"))

      {  
          input =  coin.nextLine();
          Coin c = new Coin(Coin.Value.valueOf(input));
            for(int i =0; i< c.getValue();i++)
                sum += c.getValue();
            System.out.println(sum);

       if (input.equalsIgnoreCase("penny")||input.equalsIgnoreCase("nickel")||input.equalsIgnoreCase("dime")||input.equalsIgnoreCase("quarter")||input.equalsIgnoreCase(end))
        {
            purse.add(input);
            purse.remove(end);

        }



       else{
           System.out.println("Please input a coin of U.S currency.");
           }
      }


  }

  /**
  @return ArrayList purse
  */
  public ArrayList<String> printPurseContents()
  { 
      System.out.println("Contents of the purse: " + purse);
      return purse;  
  }

  /** checks whether purse2 has the same coins in the same order as purse
     * @return 
     * @param purse2
   */



  public boolean sameContents(Purse purse2)
  {

      if (purse2.purse.equals(purse)) 
      {
          return true; 
      } 
      else
      {
          return false;
      }   

   }
  /**
   * checks whether purse2 has the same coins as in purse, disregarding the order
   * @param purse2
   * @return 
   */

  public boolean sameCoins(Purse purse2) 
  {

    if( purse2.purse.containsAll(purse))
    {
        return true; 
    }    
    else
    {
        return false;
    }
  } 


   /**
    * adds contents of purse into purse2 and clears purse of its contents
     * @param purse2    
    */
    public void transfer(Purse purse2)  
    {
               purse2.purse.addAll(purse);
                purse.clear();

     System.out.println("The second purse now has: " + purse2.purse );
     System.out.println("and the first purse has: " + purse);


    }



}

---------硬币类

package purse;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/** private string name;
 * private double value;
 * sum up value method
 *
 * @author Thomas
 */
public class Coin 
{
  public static enum Value
  {
      penny(0.01), nickel(0.05), dime(0.10), quarter(0.25), done(0);

      double change;

      Value(double value)
      {
          this.change = value;
      }
  }
    private Value type;

    public Coin(Value type)
    {
        this.type = type;
    }
    public double getValue()
    {
        return type.change;
    }
    public String getName()
    {
        return type.name();
    }


   } 

【问题讨论】:

    标签: java arraylist enums add


    【解决方案1】:

    您应该使用 ArrayList&lt;Coin&gt; 而不是 ArrayList&lt;String&gt;,然后添加 sum() 方法,例如:

    public double sum() {
        double sum = 0d;
        for (Coin c : coins) {
            sum+=c.getValue();
        }
    }
    

    【讨论】:

    • 当我尝试使用 ArrayList 时,我的回报不起作用,我无法将字符串输入添加到我的数组中或使用 purse.remove(end)。我该如何解决这个问题以配合你所说的?
    • 另外,当我把它放到我的程序中时,我被告知要初始化硬币并且程序将它设置为 Iterator 硬币。然后它抱怨它没有初始化,所以它设置为空。这真的行不通。还有什么可以设置的吗?
    猜你喜欢
    • 2021-07-13
    • 1970-01-01
    • 1970-01-01
    • 2017-02-26
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-09
    相关资源
    最近更新 更多