Java 意见分歧

Java 意见分歧,java,loops,for-loop,Java,Loops,For Loop,今天我在物理课上写了两个小脚本,但现在它开始困扰我了 第一个脚本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

今天我在物理课上写了两个小脚本,但现在它开始困扰我了

第一个脚本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;
    }
}
}
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
公共阶级变革{
静态货币[]硬币;
静态int[]计数;
公共静态void main(字符串[]args)引发IOException{
硬币=新货币[11];
计数=新整数[11];
硬币[0]=新货币(100);
硬币[1]=新货币(50);
硬币[2]=新货币(20);
硬币[3]=新货币(10);
硬币[4]=新货币(5);
硬币[5]=新货币(2);
硬币[6]=新货币(1);
硬币[7]=新货币(25,正确);
硬币[8]=新货币(10,正确);
硬币[9]=新货币(5,正确);
硬币[10]=新货币(1,正确);
System.out.println(“请键入更改:\n”);
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
字符串值=br.readLine();
String[]split=values.split(“\\”);
System.out.println();
整数=整数.parseInt(拆分[0]);
int small=Integer.parseInt(拆分[1]);
对于(int i=0;i<7;i++){
while(整块>=coins[i].getValue()){
整件-=硬币[i].getValue();
计数[i]++;
}
}
对于(int i=7;i<11;i++){
而(小>=coins[i].getValue()){
small-=硬币[i].getValue();
计数[i]++;
}
}
对于(int i=0;i<11;i++){
如果(计数[i]>0)
系统输出
.println((硬币[i].getValue()==100?”:“”)
+(硬币[i].isDecimal()?(“0.”
+(硬币[i].getValue()<10?:“)+硬币[i]
.getValue())+“:”+计数[i]
:((硬币[i].getValue()=硬币[j].getValue()){
temp1-=coins[j].getValue();
计数[j]++;
}
}
对于(int k=7;k<11;k++){
而(temp2>=coins[k].getValue()){
temp2-=硬币[k].getValue();
计数[k]++;
}
}
整数和=0;
用于(整数p:计数){
sum+=p;
}
如果(总和>最大值){
最大值=总和;
nums[0]=i;
nums[1]=h;
}
}
}
System.out.println(“\n在:$”+nums[0]+“+”+(nums[1]>9?nums[1]:“0”+nums[1])+“:+max+”\n)处所需的最大硬币和纸币;
}
公共静态类货币{
int值;
布尔十进制;
货币(整数价值){
这个(值,假);
}
货币(整数值,布尔十进制){
这个值=值;
this.decimal=十进制;
}
布尔isDecimal(){
返回小数;
}
int getValue(){
返回值;
}
}
}
第二个脚本执行相同的操作,但运行$100以下的所有值

问题是,第二个脚本说最大金额是9美元,达到了0.94美元

第一个是脚本,当您键入类似$1.94的内容时,它不会记录10是新的最高数字,而不是9


有什么问题吗?

因为我不打算做你的家庭作业,所以我不会向你提供工作代码,但这两个脚本都可以很容易地改进

1) 您的money对象知道值(例如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
intstartamount=//分钱
整数余数=startAmount;
整数硬币计数器=新整数[硬币长度];
for(int i=0;i=currentCoin){
造币机[i]=造币机[i]+1;
余数=余数-当前硬币;
}
}
//通过循环计数器打印出来,
//并使用Money类中包含的信息

您是指所需的最小硬币数量?“最大”应该是94。你为什么在物理课上做CS作业?你们在CS课上做物理实验吗?我的学校不提供计算机科学课程@user1071777我希望能够计算出所需的纸币和硬币的最大数量。谢谢你的回答,但是第一个程序显示了很多错误。只要再加上1美元或3美元,就可以分别得到10美元和11美元。为什么这不能正常工作?您确定您了解最大值和最小值的含义吗?对于0.05美元,最低是使用一枚5美分硬币,最高是使用五枚1美分硬币。是的,我的意思是,我想获得所需的最大数量的硬币,同时使用尽可能少的硬币。因此,它将首先使用最大的值,然后使用较小的值,以避免只使用所有的便士。
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