Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何确定表示输入金额所需的每种不同硬币的最少数量?_Java - Fatal编程技术网

Java 如何确定表示输入金额所需的每种不同硬币的最少数量?

Java 如何确定表示输入金额所需的每种不同硬币的最少数量?,java,Java,我对编程非常陌生,我对这个程序有点麻烦,这个程序希望用户输入一些名为novas的“外星硬币”,然后确定每个不同的外星硬币所需的最少数量来表示这个数量,从最高的开始。以下是硬币及其价值: 1极光=60脉冲星 1颗脉冲星=70个引力子 1引力子=6新星 下面是一个示例输入和输出,介绍了它的外观: Enter number of novas: 64197 [Enter] That number of novas is equal to: 2 auroras 32 pulsars 59 gra

我对编程非常陌生,我对这个程序有点麻烦,这个程序希望用户输入一些名为novas的“外星硬币”,然后确定每个不同的外星硬币所需的最少数量来表示这个数量,从最高的开始。以下是硬币及其价值:

  • 1极光=60脉冲星
  • 1颗脉冲星=70个引力子
  • 1引力子=6新星
下面是一个示例输入和输出,介绍了它的外观:

Enter number of novas: 64197 [Enter]
That number of novas is equal to: 
2 auroras 
32 pulsars 
59 gravitons 
3 novas 
这是我的代码:

import java.util.Scanner; 

public class AlienMoney {
    public static void main(String[] args){
        int startAmount, amount, auroras, pulsars, gravitons, novas; 
        Scanner input = new Scanner(System.in);
        System.out.print("Enter number of novas: ");
        amount = input.nextInt();
        System.out.println("That number of novas is equal to: ");

        startAmount = amount;
        gravitons = amount / 6;
        amount = amount % 6;
        pulsars = amount / 70;
        amount = amount % 70;
        auroras = amount / 60;
        amount = amount % 60;
        novas = amount;

        System.out.println(auroras + " auroras");
        System.out.println(pulsars + " pulsars");
        System.out.println(gravitons + " gravitons");
        System.out.println(novas + " novas");


    }
}
这是我的输出:

Enter number of novas: 64197 [Enter]
That number of novas is equal to: 
0 auroras 
0 pulsars 
10699 gravitons 
3 novas 

我不确定我做错了什么。我知道我一定要使用模运算符
%
来得到余数,但我不确定之后该怎么做。我将非常感谢任何人的帮助。多谢各位

您需要按照正确的顺序计算您的货币,从最大的开始

因此: 极光>脉冲星>引力子>新星

而不是:
引力子>脉冲星>极光>新星

你需要按照正确的顺序计算货币,从最大的开始

因此: 极光>脉冲星>引力子>新星

而不是:
引力子>脉冲星>极光>新星

就在这里,评论中指出了问题线:

startAmount = amount;
gravitons = amount / 6;
amount = amount % 6;
pulsars = amount / 70; // <-- starts here
amount = amount % 70;
auroras = amount / 60;
amount = amount % 60;
novas = amount;

还可以使用
%=
运算符。很酷

就在这里,注释中指出了问题行:

startAmount = amount;
gravitons = amount / 6;
amount = amount % 6;
pulsars = amount / 70; // <-- starts here
amount = amount % 70;
auroras = amount / 60;
amount = amount % 60;
novas = amount;

还可以使用
%=
运算符。很酷

下面代码片段中的方法是计算三枚硬币中每枚硬币的新星数量,然后计算每枚硬币中用于表示输入金额的新星数量

为了使用最少数量的硬币,我们应该从使用尽可能多的极光开始,剩下的是脉冲星,最后是引力子

int auroras = amount / (6 * 70 * 60);
int remainder = amount % (6 * 70 * 60);
int pulsars = remainder / (6 * 70);
remainder = remainder %  (6 * 70);
int graviton = remainder / 6;
int novas = remainder % 6;

下面代码片段中的方法是计算三枚硬币中每枚硬币的新星数量,然后计算每枚硬币中用于表示输入金额的新星数量

为了使用最少数量的硬币,我们应该从使用尽可能多的极光开始,剩下的是脉冲星,最后是引力子

int auroras = amount / (6 * 70 * 60);
int remainder = amount % (6 * 70 * 60);
int pulsars = remainder / (6 * 70);
remainder = remainder %  (6 * 70);
int graviton = remainder / 6;
int novas = remainder % 6;

这是不正确的,因为它首先分配较小面额的硬币,这将导致使用最多的nova硬币。它确实首先分配这些硬币,但它也会在以后将它们转换为较高面额的硬币,因此它仍然有效。这是不正确的,因为它首先分配较小面额的硬币,这将导致使用最多的nova硬币。它确实会首先分配这些硬币,但也会在以后将它们转换成更高的面额,因此它仍然有效。这一解释是正确的。下次包含代码以获得更好的结果。此解释是正确的。下次包含代码以获得更好的结果。