-3

My assignment requires me to read a number entered by the user and output the coins that number makes. For example, if the user enters "37", the program should respond with (1 Quarter, 1 dime, and 2 pennies). The code I have most likely does not make any sense at all and I don't know what I need to do to fix it.

import java.util.Scanner;

public class Change
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner (System.in);
        Int n = sc.nextInt("Enter a positive integer" );
        int number1, number2; // Division operands
        int quotient;         // Result of division


            if (QtrCnt > 0)
                if (QtrCnt > 1)
                    System.out.println(QtrCnt + " quarters");
                else
                    System.out.println(QtrCnt + " quarter");
        }

        if (DimeCnt > 0)
        {
            if (DimeCnt > 1)
                System.out.println(DimeCnt + " dimes");
            else
                System.out.println(DimeCnt + " dime");
        }

        if (NicklCnt > 0)
        {
            if (NicklCnt > 1)
                System.out.println(NicklCnt + " nickles");
            else
                System.out.println(NicklCnt + " nickle");
        }

        if (PennyCnt > 0);
        {
            if (PennyCnt > 1);
                System.out.println(PennyCnt + " pennies");
            System.out.println(PennyCnt + " penny");
        }

        int q = 25;
        int d = 10;
        int n = 5;
        int p = 1;

        if (a < 0);
            System.out.println("ERROR");


            String (money >=25); { int numQuarters = money/ 25; }
            money -= numQuarters * 25;
            QtrCnt = (num1 - num1 % 25) / 25;
            num1 = num1 - QtrCnt * 25;

            String(money >=10); { int numDimes = money/ 10; }
            money -= numDimes * 10;
            DimeCnt = (num1 - num1 % 10) / 10;
            num1 = num1 - DimeCnt * 10;

            String (money >=5); { int numNickles = money/ 5; }
            money -= numNickles * 5;
            NicklCnt = (num1 - num1 % 5) / 5;
            num1 = num1 - NicklCnt * 5;

            String (money >=1); { int numPennies = money/ 1; }
            money -= numPennies * 1;
            PennyCnt = (num1 - num1 % 1) / 1;
            num1 = num1 - PennyCnt * 1;
        }
   }
 }
Peter O.
  • 32,158
  • 14
  • 82
  • 96
Brandon.O
  • 27
  • 5

1 Answers1

0

Divide and floor (you should probably start over)

public static int getQuarters(int cents) {
    return Math.floor(cents / 25.0);
}

Here's how you do it:

public static void main(String[] args) {
    int cents = 46; // 46 just for an example
    int left = cents; // a variable that represents how many cents are left
    int quarters = getQuarters(cents); // how many quarters fit into 46 (1)
    int left -= quarters * 25; // now we have 21 cents left we need to work out
    int dimes = getDimes(left); // you can implement getDimes yourself (look at getQuarters). This will now return 2, since 2 dimes can go into 21 cents.
    left -= dimes * 10; // we now have only 1 cent to account for
    int nickels = getNickels(left) // Returns 0 (no nickels can fit into 1 cent)
    left -= nickels * 5; // we still have 1 cent left
    int pennies = left; // how many pennies are left over (always < 5)
    System.out.println(cents + " cents = " + quarters + " Quarters, " + dimes + " Dimes, " + nickels + " Nickels, and " + pennies + " Pennies."); // print the output 
}

Remember to include the getQuarters method in your Java class

Examples:

  • getQuarters(25) -> 1
  • getQuarters(24) -> 0
  • getQuarters(49) -> 1
  • getQuarters(51) -> 2
MCMastery
  • 3,099
  • 2
  • 20
  • 43
  • How do I implement that. (I'm new and seriously don't understand this at all) – Brandon.O Mar 04 '16 at 23:11
  • I'm surprised an assignment would require you to do this without much understanding of the language - Whenever you use getQuarters(cents), it returns how many quarters can "fit" into those cents. For example, `System.out.println(getQuarters(100))` prints "4" to the output. I'll update the answer, just wait a minute – MCMastery Mar 04 '16 at 23:13
  • So by doing it the way you just did it, that is the math side of the code? The remainder is just the output? – Brandon.O Mar 04 '16 at 23:25
  • The output is shown in the last line of the code. The output variables are quarters, dimes, nickels, and pennies (those are the names) – MCMastery Mar 04 '16 at 23:26
  • How did yours come out to be only a couple of lines while mine is 65, is that completed? Also you have to put an example for it to work like you put 46. – Brandon.O Mar 04 '16 at 23:30
  • Mine is completed, but it outputs in a not very user-friendly way. For example, `25` cents outputs: `25 cents = 1 quarters, 0 dimes, 0 nickels, and 0 pennies`. I'm sure you could figure out a way to make it look nicer if you must (I'm pretty sure you did in your first attempt). Also, of course, you have to make your own `getDimes`, `getNickels`, and `getPennies`. But that is very straightforward - just copy, paste, and modify the `getQuarters` I showed you – MCMastery Mar 04 '16 at 23:32
  • Sorry I have so many questions, but also was the math part the only incorrect part? Is the scanner and if else statement fine? – Brandon.O Mar 04 '16 at 23:33
  • https://gyazo.com/fa8bc48bcf00b4b56b32c0e36c5f7dd3 – Brandon.O Mar 04 '16 at 23:34
  • There are several incorrect things in your code. (1 You never define or find QtrCnt, DimeCnt, NicklCnt or PennyCnt. (2 What is `a`? (3 Oh, you define the variables near the bottom (I think). You need to define variables before you are able to use them! (4 Use my math; your math may not always be correct (I'm not sure, but mine is more readable as to what it does) – MCMastery Mar 04 '16 at 23:36
  • this is how to put in the beginning correct? https://gyazo.com/fa8bc48bcf00b4b56b32c0e36c5f7dd3 – Brandon.O Mar 04 '16 at 23:37
  • This is wat Im supposed to put before right? int q = 25; int d = 10; int n = 5; int p = 1; if (a < 0); System.out.println("ERROR"); – Brandon.O Mar 04 '16 at 23:39
  • No, that code doesn't do anything (`a` is never defined - what is it?) – MCMastery Mar 04 '16 at 23:39
  • I put it so if one puts 0 when inputting the number it shows as error so if they put a negative they must choose another number – Brandon.O Mar 04 '16 at 23:42