import java.util.Scanner;
public class MoneyChange {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Input an amount of money: ");
double value = reader.nextDouble();
int money = (int) value;
int thousand = money / 1000;
int fivehundred = money / 500;
int hundred = money / 100;
int fifty = (money % 100) / 50;
int twenty = ((money % 100) % 50) / 20;
int ten = (((money % 100) % 50) % 20) / 10;
int five = ((((money % 100) % 50) % 20) % 10) / 5;
int one = (((((money % 100) % 50) % 20) % 10) % 5) / 1;
System.out.println("Number of 1000-Bhat Banknote(s) is " + thousand);
System.out.println("Number of 500-Bhat Banknote(s) is " + fivehundred);
System.out.println("Number of 100-Bhat Banknote(s) is " + hundred);
System.out.println("Number of 50-Bhat Banknote(s) is " + fifty);
System.out.println("Number of 20-Bhat Banknote(s) is " + twenty);
System.out.println("Number of 10-Bhat Coin(s) is " + ten);
System.out.println("Number of 5-Bhat Coin(s) is " + five);
System.out.println("Number of 1-Bhat Coin(s) is " + one);
reader.close();
}
}
我想知道如何将第一个变量的值延续到另一个变量而不是重新开始。帮帮我吧!
最佳答案
在此代码中
int fifty = (money % 100) / 50;
您可以计算金钱% 100
并将其存储在可以重复使用的变量中
int hun = money % 100;
int fifty = (hun) / 50;
另请注意Integer division
关于java - 如何继续使用第一个变量中获得的值而不重新开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59657939/