/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package test1s19coinspuzzle; import java.util.Scanner; /** * * @author jcmtiernan */ public class Test1S19CoinsPuzzle { /** * @param args the command line arguments * You will ask the user to tell you how much total change (less than * $1.00) they have that they have with them and how many coins * (.01, .05, .10, and .25 US coins only) they have to make up that * amount of change. Your program will then calculate a set of coins * that could make up that amount of change. The user can then input * whether the program guessed right or wrong. If the program guessed * correctly, then the program ends. If the program guessed incorrectly, * then the program should go back and calculate a different set of * coins that would make that same amount of change. Continue until * either the program guesses correctly or the program cannot calculate * a new set of coins. */ public static void main(String[] args) { double totalChange = 0; int pennyCnt = 0; int nickelCnt = 0; int dimeCnt = 0; int quarterCnt = 0; double currentChange = -1; boolean badGuess = true; int fndPennyCnt = 0; int fndNickelCnt = 0; int fndDimeCnt = 0; int fndQuarterCnt = 0; int numCoins = 0; int fndCoins = 0; Scanner userIn = new Scanner(System.in); /* int count = 0; String response = ""; //System.out.println("How much change is in your pocket? \n"+ // "Enter as .XX and we'll predict which coins."); System.out.println("How much change is in your pocket? "+ "Enter as .XX :"); totalChange = userIn.nextDouble(); System.out.println("How many total coins do you have? "); numCoins = userIn.nextInt(); userIn.nextLine(); System.out.println("\nWe'll guess what coins you are carrying.\n"); System.out.println("Here are the current guesses:"); System.out.printf("%9s%10s%10s%10s%10s%10s\n", "Change:","Quarters", "Dimes","Nickels","Pennies", "# Coins"); for (pennyCnt = 0; (pennyCnt < 100) && badGuess; pennyCnt++) for (nickelCnt = 0; (nickelCnt < 20) && badGuess; nickelCnt++) for (dimeCnt = 0; (dimeCnt < 10) && badGuess; dimeCnt++) for (quarterCnt = 0; (quarterCnt < 4) && badGuess; quarterCnt++) { */ /* System.out.println("Q: "+quarterCnt+" D: "+dimeCnt +" N: "+nickelCnt+" P: "+pennyCnt +" Total: "+(quarterCnt*.25+dimeCnt*.10+nickelCnt*.05+pennyCnt*.01)); */ /* if (totalChange == (quarterCnt*.25+dimeCnt*.10+nickelCnt*.05+pennyCnt*.01)) { fndPennyCnt = pennyCnt; fndNickelCnt = nickelCnt; fndDimeCnt = dimeCnt; fndQuarterCnt = quarterCnt; fndCoins = fndPennyCnt + fndNickelCnt + fndDimeCnt + fndQuarterCnt; //System.out.println("Here is the current guess:"); //System.out.println("Guess - Q: "+quarterCnt+" D: "+dimeCnt // +" N: "+nickelCnt+" P: "+pennyCnt); if (fndCoins == numCoins) { count++; System.out.printf("%2d%7s%10d%10d%10d%10d%10d\t", count,". Guess", fndQuarterCnt, fndDimeCnt,fndNickelCnt,fndPennyCnt,fndCoins); //System.out.println("Is this the correct combination of coins? (Y)es or (N)o"); System.out.print("Correct? (Y)es or (N)o "); response = userIn.nextLine(); //System.out.println(""); if ((response.charAt(0) == 'Y') || (response.charAt(0) == 'y')) { System.out.println("\nFound it! \nThank you for playing.\n\n"); badGuess = false; } } } } if (badGuess == true) { System.out.println("\nWe could not determine the coins you have. Bummer.\n\n"); } */ int count = 0; String response = ""; // Ask for user input of change amount and total of coins System.out.println("How much change is in your pocket? "+ "Enter as .XX :"); totalChange = userIn.nextDouble(); System.out.println("How many total coins do you have? "); numCoins = userIn.nextInt(); userIn.nextLine(); System.out.println("\nWe'll guess what coins you are carrying.\n"); System.out.printf("%9s%10s%10s%10s%10s%10s\n", "Change:","Quarters", "Dimes","Nickels","Pennies", "# Coins"); // For each type of coin, loop up to 100 "cents", i.e. loop for 100 pennies but only 4 quarters for (pennyCnt = 0; (pennyCnt < 100) && badGuess; pennyCnt++) for (nickelCnt = 0; (nickelCnt < 20) && badGuess; nickelCnt++) for (dimeCnt = 0; (dimeCnt < 10) && badGuess; dimeCnt++) for (quarterCnt = 0; (quarterCnt < 4) && badGuess; quarterCnt++) if ((totalChange == (quarterCnt*.25+dimeCnt*.10+nickelCnt*.05+pennyCnt*.01)) && (numCoins == (pennyCnt +nickelCnt + dimeCnt + quarterCnt))) { count++; System.out.printf("%2d%7s%10d%10d%10d%10d%10d\t", count,". Guess", quarterCnt, dimeCnt,nickelCnt,pennyCnt,fndCoins); System.out.print("Correct? (Y)es or (N)o "); response = userIn.nextLine(); if ((response.charAt(0) == 'Y') || (response.charAt(0) == 'y')) { System.out.println("\nFound it! \nThank you for playing.\n\n"); badGuess = false; } } if (badGuess == true) { System.out.println("\nWe could not determine the coins you have. Bummer.\n\n"); } } }