2017-02-08 5 views
0

コイン(ペニー、ニッケル、ダム、またはクォーター)を5人入力したアレイを作成するように求められました。ユーザーが小数点5桁の5桁のコインを入力すると、入力したコイン(小数点以下の桁)と入力されたコインの最大額(最高額のコインは(最小金額のコインはペニーのように低くなります)。 私はまた、どのコインが繰り返されたかを出力することになっていますが、それを行う手掛かりはありません。ユーザーは5枚のコインを入力しているので、少なくとも1枚のコインが2回以上使用されることになります(4枚のコインのみを頼むと仮定します)。助言がありますか?例は次のようになります:「コインは繰り返します:四半期:2、ダイム:5 .etc繰り返されたコインを出力する方法

PS事前に虚偽のコーディングをお詫び申し上げます。これを行うためのより簡単な方法が最初にあります。

私はこれを行うと考えることができ
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 

*/ 

}

+1

をインポートすることがわかりました...ので、何あなたが*実際に*私は、問題の説明を取得しますが、私はあなたからの質問が表示されないのですか?求めている質問です。私は私を掲載している – Makoto

+0

@Hamsterあなたが問題になった場合には、コメントしてください。(私は私のモバイルを使って答えを投稿し、それを持っているのが最善であるエラーフリーです:)しかし、問題の場合教えてください)..http://stackoverflow.com/a/42103844/504133 –

答えて

0

の周りだけでループして次の要素

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

うわー、絶対に素晴らしい。このコードを見た後、文字通り涙が浮かびます。私は教科書やメモを見て時間を過ごしました。ありがとうございました** so ** much – Hamster

0

最も簡単な方法は、単に各コインのためのアキュムレータを持っている。例えば、最初のコイン入力が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 

これが役に立ちます。

0

レッスン1と比較:配列は、専門家のためのものです。他の人は、コレクションを使用してください。この場合

Mapはあなたの友達です:TreeMapのを使用して

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()); 

HashMapのよりもあなたの仕事がさらに容易になりますでしょう、それはあなたがあなたの出力のためにしたいキーの順序で反復して、あなたを与えるので、最高と最低の鍵は無料です。

このコードを宿題として提出する場合は、理解してください。

+0

ありがとうございます!コードは間違いなくもっと多く見直されています!私はまだTreeMapを学んでいませんが、私は間違いなくそれを調べます!私はいつも新しい素材を楽しんでいます!ありがとうございました。私は自分自身をノブXDと考えています。 – Hamster

0

おそらくSetまたはMapを使用できます。 Mapを使用することができます(各コインの数を同じにするか、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 "); 
    } 
    } 
} 
関連する問題