简体   繁体   English

除欧元金额

[英]Divide Euro Amount

I have a problem in my code and i can't find the answer for it. 我的代码有问题,我找不到答案。 I only can use if and else and can't use other classes for an example Math. 我只能使用if和else,不能使用其他类作为示例Math。

The code save a value and try to divide in euro coins. 该代码保存一个值,然后尝试分割成欧元硬币。 If i enter 4,31 the result is 2x2e + 1x20c + 1x1c and this is ok but if i enter the value 1,20 the result is 1e + 1x10c + 1x5c + 2x2c + 1x1c but the right result is 1e + 1x20c. 如果我输入4,31,则结果为2x2e + 1x20c + 1x1c,这是可以的,但如果我输入值1,20,则结果为1e + 1x10c + 1x5c + 2x2c + 1x1c,但正确的结果是1e + 1x20c。 I had to add 0.001 in the 1cent coin because if i don't i'll not get a print for it. 我必须在1cent硬币中添加0.001,因为如果不这样做,我将无法获得打印结果。 Adding this is wrong too. 添加这个也是错误的。

If somebody could help me i would be very grateful. 如果有人可以帮助我,我将不胜感激。 Regards. 问候。

Code: 码:

import java.util.Scanner;

public class Coins {

    public static void main(String[] args){
        Scanner in= new Scanner(System.in);

        int e2 = 0, e1 = 0,c50 = 0,  c20=0,c10 = 0,c5 = 0,c2 = 0,c1;
        double v;

        System.out.println("Quantia em euros: ");
        v = in.nextDouble();

        e2 = (int)v/2;
        v=v-e2*2;

        e1=(int)v;
        v=(v-e1)*100;

        c50=(int)v/50;
        v=v-c50*50;

        c20=(int)v/20;
        v=v-c20*20;

        c10=(int)v/10;
        v=v-c10*10;

        c5=(int)v/5;
        v=v-c5*5;

        c2=(int)v/2;
        v=v-c2*2;

        c1=(int)(v+0.001);

        if(e2!=0)System.out.print(e2+"X2Eur ");
        if(e2!=0&&!(e1==0&&c50==0&&c20==0&&c10==0&&c5==0&&c2==0&&c1==0))System.out.print("+ ");
        if(e1!=0)System.out.print(e1+"X1Eur ");   
        if(e1!=0&&!(c50==0&&c20==0&&c10==0&&c5==0&&c2==0&&c1==0))System.out.print("+ ");
        if(c50!=0)System.out.print(c50+"X50c ");  
        if(c50!=0&&!(c20==0&&c10==0&&c5==0&&c2==0&&c1==0))System.out.print("+ ");
        if(c20!=0)System.out.print(c20+"X20c ");  
        if(c20!=0&&!(c10==0&&c5==0&&c2==0&&c1==0))System.out.print("+ ");
        if(c10!=0)System.out.print(c10+"X10c ");  
        if(c10!=0&&!((c5==0&&c2==0&&c1==0)))System.out.print("+ ");
        if(c5!=0)System.out.print(c5+"X5c ");     
        if(c5!=0&&!(c2==0&&c1==0))System.out.print("+ ");
        if(c2!=0)System.out.print(c2+"X2c ");    
        if(c2!=0&&!(c1==0))System.out.print("+ ");
        if(c1!=0)System.out.print(c1+"X1c");
    }
}

These are rounding errors by Java, they can always occur when using floats. 这些是Java的舍入错误,在使用浮点数时总是会发生。 In your case you keep editting the same value so the error gets bigger and bigger. 在您的情况下,您将继续编辑相同的值,因此错误会越来越大。

Use 采用

System.out.println("Quantia em euros: ");
v = 1.20;
int cents = (int)(v*100);
e2 = cents/200;
cents = cents%200;

e1=cents / 100;
cents = cents % 100;

c50=cents/50;
cents = cents%50;

c20=(int)cents/20;
cents =  cents %20;

c10=(int)cents/10;
cents = cents%10;

c5=(int)cents/5;
cents = cents % 5;

c2=(int)cents/2;


c1=cents%2;

Because rounding errors don't occur on ints. 因为整数不会发生舍入错误。

first of all: if you store a value in a double-variable, always consider that double is a bit imprecise, i'd use cents instead (simply remove the comma and parse to int). 首先:如果将值存储在双精度变量中,请始终认为double有点不精确,我会改用cents(只需删除逗号并将其解析为int)。

for the implementation itself: optimize the whole thing using arrays. 对于实现本身:使用数组优化整个过程。

final int[] coin_values = new int[]{
     200 , 100 , 50 , 20 , 10 , 5 , 2 , 1};
final String[] coin_names = new String[]{
     "2€" , "1€" , "50ct" , "20ct" , "10ct" , "5ct" , "2ct" , "1ct"};

String input = in.next();
String[] temp = input.split(".");
input = temp[0] + temp[1];
int value = Integer.parseInt(input);

int[] coins = new int[coin_values.length];

for(int i = 0 ; i < coins.length ; i++){
     coins[i] = value / coin_values[i];
     value %= coin_values[i];

     if(coins[i] != 0)
          System.out.print(coins[i] + " " + coin_names[i] + " + ");
}

In Java always use java.math.BigDecimal for monetary amounts. 在Java中,始终将java.math.BigDecimal用作货币金额。 You won't get strange rounding behaviour that you can't control. 您不会得到无法控制的奇怪舍入行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM