我的程序应该收到大量的资金,并将其转换为更改,但是当它收到0
时,它应该停止要求输入并停止程序。
我的所有数学都是正确的,我只是在重复询问数字时遇到问题。这是我目前的情况:
public class Coins {
public static int quarters;
public static int dimes;
public static int nickels;
public static int pennies;
public static int Quarters(int TotalCoins) {
quarters = (int) (TotalCoins / 25);
TotalCoins %= 25;
return quarters;
}
public static int Dimes(int TotalCoins){
TotalCoins -= (quarters * 25);
dimes = (int) (TotalCoins / 10);
TotalCoins %= 10;
return dimes;
}
public static int Nickels(int TotalCoins){
TotalCoins -= (dimes * 10) + (quarters * 25);
nickels = (int) (TotalCoins / 5);
TotalCoins %= 5;
return nickels;
}
public static int Pennies(int TotalCoins){
TotalCoins -= (nickels * 5 ) + (dimes * 10 ) + (quarters * 25);
pennies = (int) (TotalCoins / 1);
TotalCoins %= 1;
return pennies;
}
}
这是我的测试人员课程(我非常确定我的主要问题是):
public class CoinsTester {
public static void main(String[] args) {
Scanner kybd = new Scanner(System.in);
if (kybd.nextInt() != 0){
int changeMoney = kybd.nextInt();
System.out.println("Quarters = " + Coins.Quarters(changeMoney) + "\nDimes = " + Coins.Dimes(changeMoney) + "\nNickels = " + Coins.Nickels(changeMoney) + "\nPennies = " + Coins.Pennies(changeMoney));
}
}
}
任何帮助都会很棒。
编辑:有人告诉我,在一个班级中完成整个任务可能更容易,如果是这样的话,任何以这种方式做的建议都会很好。三江源!答案 0 :(得分:3)
你想要这样的东西:
Scanner kybd = new Scanner(System.in);
while (true) {
int changeMoney = kybd.nextInt();
if (changeMoney == 0)
break;
// do stuff with changeMoney
}
它也可以表示为for
循环:
Scanner kybd = new Scanner(System.in);
for (int changeMoney = kybd.nextInt(); changeMoney != 0; changeMoney = kybd.nextInt()) {
// do stuff with changeMoney
}
虽然线条较少,但它使for循环线很长,但可以通过使用较短的变量名来缓解:
Scanner in = new Scanner(System.in);
for (int i = in.nextInt(); i != 0; i = in.nextInt()) {
// do stuff with i
}
答案 1 :(得分:2)
您要求用户输入两次号码
if
声明中如果您希望让用户再次写入另一个号码,那么您的if
似乎应该被循环替换。在这种情况下,您可以使用类似
Scanner kybd = new Scanner(System.in);
for (int changeMoney = -1; (changeMoney = kybd.nextInt()) != 0;){
System.out.println("Quarters = " + Coins.Quarters(changeMoney) + "\nDimes = " + Coins.Dimes(changeMoney) + "\nNickels = " + Coins.Nickels(changeMoney) + "\nPennies = " + Coins.Pennies(changeMoney));
}
这种方法的优点是它很短,changeMoney
只是局部变量,只能在循环中使用。
答案 2 :(得分:1)
首先,您的方法应该具有小写名称。其次,你需要一个循环 - 也许就像这样
public static void main(String[] args) {
Scanner kybd = new Scanner(System.in);
while (kybd.hasNextInt()){ // will stop if the next input is not an int.
int changeMoney = kybd.nextInt();
if (changeMoney == 0) {
break;
}
// Assuming you just rename them to "get" methods.
System.out.println("Quarters = "
+ Coins.getQuarters(changeMoney) + "\nDimes = "
+ Coins.getDimes(changeMoney) + "\nNickels = "
+ Coins.getNickels(changeMoney)
+ "\nPennies = "
+ Coins.getPennies(changeMoney));
}
}
答案 3 :(得分:0)
将其更改为此类内容。
int changeMoney=0;
while ( (changeMoney =kybd.nextInt()) != 0){
System.out.println("Quarters = " + Coins.Quarters(changeMoney) + "\nDimes = " + Coins.Dimes(changeMoney) + "\nNickels = " + Coins.Nickels(changeMoney) + "\nPennies = " + Coins.Pennies(changeMoney));
}
@Bohemian回答:很棒的代码。我的错,忘记提及(抱歉)我 应该能够一次输入所有输入数字,我该怎么做 你会说这个吗?
将输入存储在类似List
的集合中。
代码示例:
List<Integer> list = new ArrayList<>();
while ( (int i =kybd.nextInt()) != 0){
list.add(i);
}
// Then loop again
for(Integer changeMoney : list){
System.out.println("Quarters = " + Coins.Quarters(changeMoney) + "\nDimes = " + Coins.Dimes(changeMoney) + "\nNickels = " + Coins.Nickels(changeMoney) + "\nPennies = " + Coins.Pennies(changeMoney));
}
答案 4 :(得分:0)
看起来你只读了一次,你应该在一个while循环中包装scanner.nextInt()调用,如果值为0就会中断
while (kybd.hasNextInt()){
int changeMoney = kybd.nextInt();
if(changeMoney == 0)break;
System.out.println("Quarters = " + Coins.Quarters(changeMoney) + "\nDimes = " + Coins.Dimes(changeMoney) + "\nNickels = " + Coins.Nickels(changeMoney) + "\nPennies = " + Coins.Pennies(changeMoney));
}