'How should I start my minimum coins project from the beginning, the user change input?
This program is supposed to find the minimum coins to use based off of the change the user inputs in the beginning. However I can't seem to get it to start completely over from the beginning. In it's current state it keeps adding the same numbers up every time it starts over, and it doesn't display the output for the user to read the second time. Edit: code format
import java.util.Scanner; // Import Scanner class
public class Main
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in); // Create a Scanner object
int change = 0;
int quarters = 0;
int dime = 0;
int nickels = 0;
int pennies = 0;
System.out.println("Please Enter Amount of Change (1-99) or ZERO to EXIT.");
while (change == 0) {
change = input.nextInt();
while (change <= 0) {
System.exit(0);
}
while (change >= 25){
change = (change - 25); quarters = (quarters + 1);
}
while (change >= 10) {
change = (change - 10); dime = (dime + 1);
}
while (change >= 5) {
change = (change - 5); nickels = (nickels + 1); // block of code to be executed if the condition is true
}
pennies = change; // block of code to be executed if the condition is false
System.out.println("You currently have: ");
System.out.println("Quarters:" + quarters);
System.out.println("Dimes:" + dime);
System.out.println("Nickels:" + nickels);
System.out.println("Pennies:" + pennies);
}
}
}
Solution 1:[1]
I believe I've figured it out. I reset all integers to 0 at end of code and added another print statement stating the same as the first bit. In case anyone sees this and wants to see the code i fixed it to, I'll post it below.
Solution:
import java.util.Scanner; // Import Scanner class
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); // Create a Scanner object
int change = 0;
int quarters = 0;
int dime = 0;
int nickels = 0;
int pennies = 0;
System.out.println("Please Enter Amount of Change (1-99) or ZERO to EXIT.");
while (change == 0) {
change = input.nextInt();
while (change <= 0) {
System.exit(0);
}
while (change >= 25){
change = (change - 25); quarters = (quarters + 1);
}
while (change >= 10) {
change = (change - 10); dime = (dime + 1);
}
while (change >= 5) {
change = (change - 5); nickels = (nickels + 1); // block of code to be executed if the condition is true
}
pennies = change; // block of code to be executed if the condition is false
System.out.println("You currently have: ");
System.out.println("Quarters:" + quarters);
System.out.println("Dimes:" + dime);
System.out.println("Nickels:" + nickels);
System.out.println("Pennies:" + pennies);
change = 0;
quarters = 0;
dime = 0;
nickels = 0;
pennies = 0;
System.out.println("Please Enter Amount of Change (1-99) or ZERO to EXIT.");
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Justin Smoak |
