2016-04-29 9 views
0

私は自分のプログラムでニッケールの数を丸めるようにしています(これ以上ペニーはありません)。だから私は1.44を入力すると1.40にラウンドする必要があり、1.46の場合は1.50に丸めるはずです。助けてください!プログラムを変更する(丸めヘルプ)

import java.util.Scanner; 

public class MakingChange 
{ 

    private static Scanner scanner; 

    public static void main(String[] args) 

    { 
     scanner = new Scanner(System.in); 
     double amount = 0; 
     while (true) { 
      try { 
       amount = Double.parseDouble(scanner.nextLine()); 
       break; // will only get to here if input was a double 
      } catch (NumberFormatException ignore) { 
       System.out.println("INVALID\r\n$"); 
      } 
     } 

     //calculating amount of change in cents 
     int remainingAmount = (int)(amount * 100.00); 
     //toonies 
     int numberofToonies = (int) (remainingAmount/200.00); 
     remainingAmount = remainingAmount % 200; 
     //loonies 
     int numberofLoonies = (int) (remainingAmount/100.00); 
     remainingAmount = remainingAmount % 100; 
     //quarters 
     int numberofQuarters = (int)(remainingAmount/25.00); 
     remainingAmount = remainingAmount % 25; 
     //dimes  
     int numberofDimes = (int)(remainingAmount/10.00); 
     remainingAmount = remainingAmount % 10; 
     //nickels 
     int numberofNickels = (int)(remainingAmount/5.00); 
     remainingAmount = remainingAmount % 5; 
     //rounded value 
     numberofNickels=(int) (((amount -(numberofToonies * 2) - (numberofLoonies *1) - (numberofQuarters *0.25) - (numberofDimes * 0.10) - (numberofNickels * 0.05))+0.04)/0.05); 

System.out.println(".*toonies:" + numberofToonies + ";" + " loonies:" + numberofLoonies + ";" + " quarters:" + numberofQuarters + ";" + " dimes:" + numberofDimes + ";" + " nickels:" + numberofNickels +"$"); 


     } 

} 
+1

をユーザーに尋ねることを忘れてはいけないので、正確にどのようにこのコードが動作しませんか? –

+0

*あなたは1.44を持っていればUSスタイルにすることができますが、1を取得し、ヒントのjarファイルを取得します.44:p *おそらくあなたは['Math.round'](https://docs.oracle.com)を探しています。 /javase/8/docs/api/java/lang/Math.html#round-double-)。また、金銭的なものにはdoubleを使うべきではないことに注意してください(例えば 'BigDecimal'を使用してください) –

+0

2つのニッケルスを与える代わりに、私には1つしか与えません。また、2つのニッケルを取得した場合、2つのニッケルではなく、1つのダイムを表示するようにします。 –

答えて

2

私は、Math.round()でそれラウンド、10で数を掛け、その後10で戻ってそれを分割することをお勧め。

1.46 * 10 = 14.6 
Math.round(14.6) = 15 
15/10 = 1.5 

1.44 * 10 = 14.4 
Math.round(14.4) = 14 
14/10 = 1.4 

これは、次のラムダで実現することができます:Math.round()long値を返し、あなたは整数divisonが、フロート部門をしたくないので、10.0の代わり10を指定

d -> Math.round(d * 10)/10.0 

が重要です。

this ideone snippetで実際に動作しています。

+0

再利用可能なヘルパーメソッドにそのロジックを書き込むことは良い考えです。 'double roundToNickel(double amount)'。 – Andreas

+0

@Aaron、コードのスニペットを送ってもらえますか?どのように見えるかは分かりません。また、なぜ単純に0.05を掛けて0.05で除算しないのでしょうか? –

0

は、彼の入力

public class MakingChange 
{ 
    private static Scanner scanner; 

    public static void main(String[] args) { 

     scanner = new Scanner(System.in); 
     System.out.println("Insert amount:"); 
     double amount = 0; 
     while (true) { 
      try { 
       amount = Double.parseDouble(scanner.nextLine()); 
       break; // will only get to here if input was a double 
      } catch (NumberFormatException ignore) { 
       System.out.println("INVALID\r\n$"); 
      } 
     } 

     // calculating amount of change in cents 
     int remainingAmount = (int) (amount * 100.00); 
     // toonies 
     int numberofToonies = (int) (remainingAmount/200.00); 
     remainingAmount = remainingAmount % 200; 
     // loonies 
     int numberofLoonies = (int) (remainingAmount/100.00); 
     remainingAmount = remainingAmount % 100; 
     // quarters 
     int numberofQuarters = (int) (remainingAmount/25.00); 
     remainingAmount = remainingAmount % 25; 
     // dimes 
     int numberofDimes = (int) (remainingAmount/10.00); 
     remainingAmount = remainingAmount % 10; 

     // nickels 
     int numberofNickels = 0; 
     // if the remaining amount is exactly 5 cents 
     if (remainingAmount == 5) { 
      numberofNickels = 1; 
     } else if (remainingAmount > 5)// round to higher value if remaining 
             // value is greater than 5 cents e.g 
             // 20.68 
     { 
      numberofDimes += 1; 
     } 

     System.out.println(".*toonies:" + numberofToonies + ";" + " loonies:" + numberofLoonies + ";" + " quarters:" 
       + numberofQuarters + ";" + " dimes:" + numberofDimes + ";" + " nickels:" + numberofNickels + "$"); 

    } 
} 
関連する問題