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

标签 java

我对编程非常陌生,我在使用这个程序时遇到了麻烦,该程序希望用户输入一些称为“novas”的“外星硬币”,然后确定代表该金额所需的每种不同外星硬币的最少数量,从最高的开始。以下是硬币及其值(value):

  • 1 极光 = 60 颗 pulsar
  • 1 pulsar = 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 

我不知道我做错了什么。我知道我肯定必须使用模运算符 % 来获取余数,但我不知道之后该怎么做。我将非常感谢任何人的帮助。谢谢。

最佳答案

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

所以: 极光> pulsar >引力子>新星

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

关于java - 如何确定代表输入金额所需的每种不同硬币的最少数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39522464/

相关文章:

java - 在android studio中使用AsyncTask从socket读取数据

java - 将 JSON 字符串转换为具有自定义类型的 Map

Eclipse : Where do I put files on the filesystem that I want to load using getResource? 中的 Java(例如 ImageIcon 的图像)

java - mouseEntered 不起作用

java - 实现与有理类类似的接口(interface)(java)

java - @override 在 android 中使用 onDraw 导致错误

java - 我应该使用DocumentSnapshot,QuerySnapshot还是QueryDocumentSnapshot?

java - 由于静态初始化而调用的构造函数

java - 如何正确解析 RFC 日期?

java - LogBack RollingFileAppender 不写入日志文件(尽管 FileAppender 有效)