【问题标题】:How to output the coins that were repeated如何输出重复的硬币
【发布时间】:2017-02-08 02:04:54
【问题描述】:

我被要求创建一个数组,其中包含 5 个用户输入的硬币(一分钱、五分钱、一角钱或四分之一)。一旦用户以十进制形式输入他的 5 个硬币数量中的任何一个,我应该输出他们输入的硬币(以十进制形式),以及他们输入的最大数量的硬币(最有价值的硬币可以高达季度)和输入的最小数量的硬币(最小价值的硬币可以低至一美分)。 我还应该输出重复的硬币,但我不知道该怎么做。由于用户输入了 5 个硬币,这意味着至少一个硬币将被多次使用(假设我只向用户询问这 4 个硬币)。有什么建议么?例如:“重复的硬币是:四分之一:2,一角钱:5 .etc

附:我为我草率的编码提前道歉,我确信有更简单的方法可以做到这一点,第一次学习。

enter code here

package HwMoney;
import java.util.Arrays;
import java.util.Scanner;
public class Coins2 {
public static void main(String[] args){
     Scanner kb=new Scanner(System.in);

        double coin[]=new double[5];
        int[] num={1,2,3,4,5};

        System.out.println("enter coin values");
        System.out.println("Enter the 5 coins values in decimal format,");
        for(int i=0;i<=4;i++){
        System.out.print("coin " + num[i] +":\t");
            coin[i] = kb.nextDouble();

        }

        System.out.println("your coins that you put in are: ");
        for(int i=0;i<=4;i++){
        System.out.print(coin[i]+"\t");


        }

        System.out.println();
        Arrays.sort(coin);

        double max = coin[4];
        if(max == .25)
        System.out.println("Your greatest coin is: quarter");
        else if(max == .10)
        System.out.println("Your greatest coin is: dime");
        else if(max == .05)
        System.out.println("Your greatest coin is: nickle");
        else if(max == .01)
        System.out.println("Yout greatest coin is: penny");
        else 
        System.out.println("try again you gave me the wrong numbers");


        double min = coin[0];
        if(min == .25)
        System.out.println("Your smallest coin is: quarter");
        else if(min == .10)
        System.out.println("Your smallest coin is: dime");
        else if(min == .05)
        System.out.println("Your smallest coin is: nickle");
        else if(min == .01)
        System.out.println("Your smallest coin is: penny");

}
/*enter coin values
Enter the 5 coins values in decimal format,
coin 1: .05
coin 2: .10
coin 3: .25
coin 4: .01
coin 5: .10
your coins that you put in are: 
0.05    0.1 0.25    0.01    0.1 
Your greatest coin is: quarter
Your smallest coin is: penny

*/

}

【问题讨论】:

  • 好的...那么您实际上要问的问题是什么?我收到了问题描述,但没有看到您提出的问题。
  • @Hamster 我已经发布了我的答案,它会处理最大、最小和重复计数...希望它对您也有帮助,如有问题请发表评论。 (我用我的手机发布了一个答案,并尽力让它没有错误:),但如果出现问题,请告诉)..stackoverflow.com/a/42103844/504133

标签: java arrays


【解决方案1】:

只需循环并与下一个元素进行比较

    double lastVal = -1.0;
    for(int i=0;i<4;i++){
        if (coin[i] == coin [i+1] && lastVal != coin[i]) {  // prevent reading same value
            lastVal = coin[i];
            if(coin[i] == .25) 
                System.out.println("quarter is repeated");
            if(coin[i] == .10) 
                System.out.println("dime is repeated");
            // etc

            i++;
        }
    }

【讨论】:

  • 哇,太棒了。看到这段代码真的泪流满面。我花了太多时间浏览我的教科书和笔记。谢谢你这么非常
【解决方案2】:

我能想到的最简单的方法就是为每个硬币设置一个累加器。例如,当第一个硬币输入为 0.10 时,您将在硬币累加器上加 1。

例子:

    // define accumulator

    int num_dimes = 0;
    System.out.println("your coins that you put in are: ");
    for(int i=0;i<=4;i++){
    System.out.print(coin[i]+"\t");
       if (coin[i] = 0.10)
           num_dimes = num_dimes + 1; //I think you can say num_dimes += 1

希望这会有所帮助。

【讨论】:

    【解决方案3】:

    您可以为此使用SetMap。你可以使用Map如果你的要求是(或者可能是明天可能)每个硬币的数量(假设用户可以选择超过2个硬币,就像所有5个硬币一样或3个硬币一样。

    您需要导入java.util.HashMap

    public static void main(String[] args){
       Scanner kb=new Scanner(System.in);
       double max = Integer.MIN_VALUE;
       double min = Integer.MAX_VALUE;
    
       Map < Double, Integer > countMap = new HashMap < >();
       Map <Double,String > valueNameMap = new HashMap < > ();
        // mapping penny, nickel, dime, or quarter
        valueNameMap.put (0.01, "penny");
        valueNameMap.put (0.05, "nickel");
        valueNameMap.put (0.10, "dime");
        valueNameMap.put (0.25, "quater");
    
        System.out.println("enter coin values");
        System.out.println("Enter the 5 coins values in decimal   ");
    
      for(int i=0;i<=4;i++){
        System.out.print("coin " + (i + 1) +":\t");
            double input = kb.nextDouble();
           if( input < min ){min = input};
           if( input > max ){max = input};
           Integer count = countMap.get(input);
           if(count != null ){
               //input is duplicate
               countMap.put(input,count+1);
           }else {
               countMap.put(input,0);
          }
       }
        if(valueName.get(max) ! = null ){
               System.out.println("Your greatest coin is: "+valueNameMap.get(max));
         }else {  
             System.out.println(" wrong greatest coin inputted ");
        }
        if(valueName.get(min) ! = null ){
               System.out.println("Your smallest coin is: "+valueNameMap.get(min));
         }else {  
             System.out.println(" wrong minimum coin inputted ");
        }
    
      for(Double coin : countMap.keySet()){
           Integer count = countMap.get(coin);
          if (count ! = null && count > 1) {
              System.out.println(" we have a duplicate coin :" + valueNameMap.get(coin) +" its entered " + count + " times " );
         }
      }
    }
    

    【讨论】:

      【解决方案4】:

      第 1 课:数组适用于专家。其他人,使用集合。

      在这种情况下,Map 是您的朋友:

      TreeMap<Double, Integer> coins = new TreeMap<>();
      
      for (int i = 0; i < 5; i++) { 
          double coin = kb.nextDouble();
          // check input is valid if you like
          coins.merge(coin, 1, (a, b) -> a + b);
      }
      
      System.out.println("You have:");
      coins.forEach((c, n) -> System.out.println(n + " of " + c));
      
      System.out.println("Your greatest coin is " + coins.lastKey());
      System.out.println("Your least coin is " + coins.firstKey());
      

      使用 TreeMap 使您的工作比 HashMap 更容易,因为它会按照您希望输出的键顺序进行迭代,并免费为您提供最高和最低键。

      如果您将此代码作为作业提交,请确保您理解它!

      【讨论】:

      • 非常感谢!代码肯定看起来更加翻新了!虽然我还没有学过TreeMap,但我一定会研究它的!我总是喜欢新材料!谢谢你。我认为自己是个菜鸟 XD。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-28
      • 1970-01-01
      • 2015-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-22
      相关资源
      最近更新 更多