目前我正在开发一个程序,用户输入的分数为美分(因此1.25美元为125),该程序确定以最少的金额给出的硬币数量。我有这个工作。
令我不安的是,我的教授希望程序不断循环,直到用户输入的值小于0.我不知道该怎么做,因为每次尝试时,它都只会循环一次并且没有显示适当数量的硬币。
请帮忙。
这是我的代码:
import java.util.Scanner;
public class MakingChange {
public static void main(String[] args) {
//Prompts user to input the change they recieved.
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the amount of change you recieved in coin format. Example: $1.25 would be entered as 125.");
int Change = sc.nextInt(); //The value is then stored as an integer named Change.
int Pennies = 0;
int Nickels = 0;
int Dimes = 0;
int Quarters = 0;
while (Change < 1){
System.out.println("Error: Cannot enter a value less than 1!");
//System.exit(0); //found at http://codingforums.com/java-jsp/69296-%5Bjava%5D-how-end-program.html
}
while (Change > 0){ //Runs a loop which determines how many of each coin is used by subtracting the values of the largest first and continuing until 0.
if (Change >= 25){
Change -= 25;
Quarters++;
}
else if (Change >= 10){
Change -= 10;
Dimes++;
}
else if (Change >= 5){
Change -=5;
Dimes++;
}
else if (Change >= 1){
Change -= 1;
Pennies++;
}
}
System.out.println("In total, you should have recieved:");
System.out.printf("Number of Quarters: %3d %n", Quarters);
System.out.printf("Number of Dimes: %6d %n", Dimes);
System.out.printf("Number of Nickels: %4d %n", Nickels);
System.out.printf("Number of Pennies: %4d %n", Pennies);
//Prints out final number of coins used by type of coin.
}
}
答案 0 :(得分:2)
废弃你的第一个while循环。你不希望两个循环处理相同的事情。接下来,初始化您的“更改”变量,但将sc.nextInt();
移到内部,以便每次迭代都获得输入。最后,使循环成为do-while循环,使其至少运行一次(或者你可以将更改初始化为1)。
int Change = 0;
do{
Change = sc.nextInt();
// ...
}
while(Change > 0);
答案 1 :(得分:1)
您需要询问用户他们在循环中获得了多少变化。以下是一些代码:
import java.util.scanner;
public void MakingChange {
public static void main(String[] args) {
int change = 0;
int currentchange = 0;
Scanner in = new Scanner(System.in);
do {
change += currentchange;
System.out.println("Enter an amount of change.");
currentchange = in.nextInt();
} while(currentchange > 0);
}
}
System.out.println("Total change: " + change);
+1给Gary做循环的想法。 你明白了。
答案 2 :(得分:1)
我在你的代码中看到了一些问题,Java命名约定的变量以小写字母开头。接下来,您可以在Scanner.hasNextInt()
时循环。然后,如果你计算自己的循环中的四分之一,硬币和镍(你的镍计数器算作硬币),你的代码似乎会更简单。所以,像 -
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String msg = "Please enter the amount of change "
+ "you received in coin format. Example: "
+ "$1.25 would be entered as 125.";
System.out.println(msg);
while (sc.hasNextInt()) {
int change = sc.nextInt();
int pennies = 0;
int nickels = 0;
int dimes = 0;
int quarters = 0;
if (change < 1) {
System.out.println("Error: Cannot enter a value less than 1!");
} else {
while (change >= 25) {
change -= 25;
quarters++;
}
while (change >= 10) {
change -= 10;
dimes++;
}
while (change >= 5) {
change -= 5;
nickels++;
}
pennies = change;
}
System.out.println("In total, you should have recieved:");
System.out.printf("Number of Quarters: %3d %n", quarters);
System.out.printf("Number of Dimes: %6d %n", dimes);
System.out.printf("Number of Nickels: %4d %n", nickels);
System.out.printf("Number of Pennies: %4d %n", pennies);
System.out.println(msg);
}
}
答案 3 :(得分:1)
我会这样简化你的循环:
int change;
while ((change = sc.nextInt()) >= 0) {
//do code
}
由于Java命名约定,请注意小写change
。
您的代码的主要问题是您有两个循环,如果您的数字不小于1,您将立即退出第一个循环(如果数量不足则循环无限循环)。你做了一次验证,然后做了你的程序逻辑。尝试使用单个循环。
答案 4 :(得分:0)
你需要循环一切。循环的顶部将提示并读取值,循环的底部将打印输出。但因为它是一个循环,它会返回并再次提示。您在代码中没有在该级别获得循环。
答案 5 :(得分:0)
import java.util.Scanner;
public class MakingChange {
public static void main(String[] args) {
//Prompts user to input the change they recieved.
Scanner sc = new Scanner(System.in);
int input = 0;
do{
System.out.println("Please enter the amount of change you recieved in coin format. Example: $1.25 would be entered as 125.");
input= sc.nextInt(); //The value is then stored as an integer named Change.
if(input<=0) {
System.out.println("Good Bye");
break; breaks the loop
}
else{
Change=input;
int Pennies = 0;
int Nickels = 0;
int Dimes = 0;
int Quarters = 0;
while (Change > 0){ //Runs a loop which determines how many of each coin is used by subtracting the values of the largest first and continuing until 0.
if (Change >= 25){
Change -= 25;
Quarters++;
}
else if (Change >= 10){
Change -= 10;
Dimes++;
}
else if (Change >= 5){
Change -=5;
Dimes++;
}
else if (Change >= 1){
Change -= 1;
Pennies++;
}
}
System.out.println("In total, you should have recieved:");
System.out.printf("Number of Quarters: %3d %n", Quarters);
System.out.printf("Number of Dimes: %6d %n", Dimes);
System.out.printf("Number of Nickels: %4d %n", Nickels);
System.out.printf("Number of Pennies: %4d %n", Pennies);
//Prints out final number of coins used by type of coin.
}//end of else
}while(input>0);//end of do while
}
答案 6 :(得分:0)
只需进行一次while循环并继续引用它直到输入&lt;从技术上讲,你所有的计算都应该在这个庞大的循环中进行。