划分欧元金额
Divide Euro Amount
我的代码有问题,我找不到答案。
我只能使用 if 和 else 而不能使用其他 类 作为示例 Math.
该代码保存一个值并尝试划分欧元硬币。
如果我输入 4,31,结果是 2x2e + 1x20c + 1x1c,这没问题,但如果我输入值 1,20,结果是 1e + 1x10c + 1x5c + 2x2c + 1x1c,但正确的结果是 1e + 1x20c。
我不得不在 1 美分硬币中添加 0.001,因为如果我不这样做,我就不会得到它的印刷品。添加这个也是错误的。
如果有人能帮助我,我将不胜感激。
问候。
代码:
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");
}
}
这些是 Java 的舍入误差,它们在使用浮点数时总是会发生。在你的情况下,你不断编辑相同的值,所以错误变得越来越大。
使用
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;
因为整数不会出现舍入错误。
首先:如果您将一个值存储在双变量中,请始终认为双变量有点不精确,我会改用美分(只需删除逗号并解析为 int)。
对于实施本身:使用数组优化整个事情。
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] + " + ");
}
在 Java 中始终使用 java.math.BigDecimal
作为货币金额。
您不会遇到无法控制的奇怪舍入行为。
我的代码有问题,我找不到答案。 我只能使用 if 和 else 而不能使用其他 类 作为示例 Math.
该代码保存一个值并尝试划分欧元硬币。 如果我输入 4,31,结果是 2x2e + 1x20c + 1x1c,这没问题,但如果我输入值 1,20,结果是 1e + 1x10c + 1x5c + 2x2c + 1x1c,但正确的结果是 1e + 1x20c。 我不得不在 1 美分硬币中添加 0.001,因为如果我不这样做,我就不会得到它的印刷品。添加这个也是错误的。
如果有人能帮助我,我将不胜感激。 问候。
代码:
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");
}
}
这些是 Java 的舍入误差,它们在使用浮点数时总是会发生。在你的情况下,你不断编辑相同的值,所以错误变得越来越大。
使用
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;
因为整数不会出现舍入错误。
首先:如果您将一个值存储在双变量中,请始终认为双变量有点不精确,我会改用美分(只需删除逗号并解析为 int)。
对于实施本身:使用数组优化整个事情。
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] + " + ");
}
在 Java 中始终使用 java.math.BigDecimal
作为货币金额。
您不会遇到无法控制的奇怪舍入行为。