我今天在物理学上制作了2个小脚本,但现在它开始让我烦恼了。
第一个脚本是100%准确:它用于计算所需现金数量所需的账单和硬币数量。
第一个脚本:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Change {
static Money[] coins;
static int[] counts;
public static void main(String[] args) throws IOException {
coins = new Money[11];
counts = new int[11];
coins[0] = new Money(100);
coins[1] = new Money(50);
coins[2] = new Money(20);
coins[3] = new Money(10);
coins[4] = new Money(5);
coins[5] = new Money(2);
coins[6] = new Money(1);
coins[7] = new Money(25, true);
coins[8] = new Money(10, true);
coins[9] = new Money(5, true);
coins[10] = new Money(1, true);
System.out.println("Please type the change:\n");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String values = br.readLine();
String[] split = values.split("\\.");
System.out.println();
int whole = Integer.parseInt(split[0]);
int small = Integer.parseInt(split[1]);
for (int i = 0; i < 7; i++) {
while (whole >= coins[i].getValue()) {
whole -= coins[i].getValue();
counts[i]++;
}
}
for (int i = 7; i < 11; i++) {
while (small >= coins[i].getValue()) {
small -= coins[i].getValue();
counts[i]++;
}
}
for (int i = 0; i < 11; i++) {
if (counts[i] > 0)
System.out
.println((coins[i].getValue() == 100 ? "" : " ")
+ (coins[i].isDecimal() ? (" 0."
+ (coins[i].getValue() < 10 ? "0" : "") + coins[i]
.getValue()) + ": " + counts[i]
: ((coins[i].getValue() <= 5 ? " " : "") + coins[i]
.getValue())
+ ".00: "
+ counts[i]));
}
}
public static class Money {
int value;
boolean decimal;
Money(int value) {
this(value, false);
}
Money(int value, boolean decimal) {
this.value = value;
this.decimal = decimal;
}
boolean isDecimal() {
return decimal;
}
int getValue() {
return value;
}
}
}
第二个脚本:
import java.io.IOException;
public class ChangeMax {
static Money[] coins;
static int[] nums = new int[2];
static int max = -2147483648;
public static void main(String[] args) throws IOException{
coins = new Money[11];
coins[0] = new Money(100);
coins[1] = new Money(50);
coins[2] = new Money(20);
coins[3] = new Money(10);
coins[4] = new Money(5);
coins[5] = new Money(2);
coins[6] = new Money(1);
coins[7] = new Money(25, true);
coins[8] = new Money(10, true);
coins[9] = new Money(5, true);
coins[10] = new Money(1, true);
for(int i = 0; i < 100; i++){
int temp1 = i;
for(int h = 1; h < 100; h++){
int temp2 = h;
int[] counts = new int[100];
for (int j = 0; j < 7; j++) {
while (temp1 >= coins[j].getValue()) {
temp1 -= coins[j].getValue();
counts[j]++;
}
}
for (int k = 7; k < 11; k++) {
while (temp2 >= coins[k].getValue()) {
temp2 -= coins[k].getValue();
counts[k]++;
}
}
int sum = 0;
for(int p : counts){
sum += p;
}
if(sum > max){
max = sum;
nums[0] = i;
nums[1] = h;
}
}
}
System.out.println("\nMax coins and bills required at: $"+nums[0]+"."+(nums[1] > 9 ? nums[1] : "0" + nums[1]) + ": "+max+"\n");
}
public static class Money {
int value;
boolean decimal;
Money(int value) {
this(value, false);
}
Money(int value, boolean decimal) {
this.value = value;
this.decimal = decimal;
}
boolean isDecimal() {
return decimal;
}
int getValue() {
return value;
}
}
}
第二个脚本执行相同的操作,但会运行$ 100以下的所有值。
问题是,第二个脚本是否说最大金额是9,并且达到了0.94美元。
第一个脚本,当你输入类似1.94美元的东西时,并没有注册10是新的最高数字,而不是9。
什么似乎是问题?
答案 0 :(得分:0)
由于我不打算做你的作业,我不打算为你提供工作代码,但两个脚本都可以很容易地改进
1)您的货币对象知道是否有价值10代表10美元,或10美分(或者你在美国使用的任何东西,我会使用欧元和美分)。但是你仍然使用你的阵列的硬编码索引,你从美元切换到美分
2)当有人使用漂亮的舍入数字作为输入时,第一个脚本将失败,没有小数部分
3)如果您将输入首先转换为美分,以及您的硬币阵列中的所有值,您的代码将更清晰,更容易理解。
形式的东西int startAmount = ... ;//in cents
int remainder = startAmount;
int coinCounter = new int[coins.length];
for ( int i = 0; i < coins.length; i++ ){
int currentCoin = coins[i];//in cents
coinCointer[i] = 0;
while( remainder >= currentCoin ){
coinCointer[i] = coinCointer[i] + 1;
remainder = remainder - currentCoin;
}
}
//print out by looping over coinCounter,
//and use the info contained in the Money class