-1

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).

This is the code I've gotten so far I would appreciate it if someone could help me finish it as well as see if my current code has any errors

import java.util.Scanner;
public class Coin
{
    Scanner sc = new Scanner (System.in);
    Int n = sc.nextInt("Enter a positive integer" );
    int number1, number2; // Division operands
    int quotient;         // Result of division

    public static int getQuarters(int cents) {
        return Math.floor(cents / 25.0);
    }
    public static int getDimes(int cents) {
        return Math.floor(cents / 10.0);
    }
    public static int getNickels(int cents) {
        return Math.floor(cents / 5.0);
    }
    public static int getPennies(int cents) {
        return Math.floor(cents / 1.0);
    }
    public static void main(String[] args) {
        int cents = 46;
        int left = cents;
        int quarters = getQuarters(cents);
        int left -= quarters * 25;
        int dimes = getDimes(left);
        left -= dimes * 10;
        int nickels = getNickels(left);
        left -= nickels * 5;
        int pennies = left;
        System.out.println(cents + " cents = " + quarters + " Quarters, " + dimes + " Dimes, " + nickels + " Nickels, and " + pennies + " Pennies."); // print the output
    }
}
Brandon.O
  • 27
  • 5

2 Answers2

1

First, "if my current code has any errors" - Yes, it does, and your development environment should point them out. Or if you're using a command-line build, again, it would point them out. The one that jumps out at me: you have a second definition of "left". But the point is, let your IDE or build-script tell you if you have any syntax errors.

Then, to address "could help me finish it" I ask: have you even tried running it? (Well no, it won't compile with the second "int left" definition). But clean up the errors, run it and see what happens...You just might be surprised.

Of course, then you need to activate the input scanner instead of hard-coding the amount to calculate...

Tom Joyal
  • 91
  • 5
0

The follwing can be used ,but I don't quite understand your variables.

import java.util.Scanner;

public class Test{

public static int getQuarters(int cents) {
              return (int) Math.floor(cents / 25.0);
}
public static int getDimes(int cents) {
              return (int) Math.floor(cents / 10.0);
}
public static int getNickels(int cents) {
              return (int) Math.floor(cents / 5.0);
}
public static int getPennies(int cents) {
              return (int) Math.floor(cents / 1.0);
}
public static void main(String[] args) {
    Scanner sc = new Scanner (System.in);
    System.out.println("Enter a positive integer");
    int n = sc.nextInt();
   // int number1, number2; // Division operands
   // int quotient;         // Result of division
    int cents = n;
    int left = cents;
    int quarters = getQuarters(cents);
    left -= quarters * 25;
    int dimes = getDimes(left);
    left -= dimes * 10;
    int nickels = getNickels(left);
    left -= nickels * 5;
    int pennies = left;
    System.out.println(cents + " cents = " + quarters + " Quarters, " + dimes + " Dimes, " + nickels + " Nickels, and " + pennies + " Pennies."); // print the output
}
}
TanLingxiao
  • 402
  • 5
  • 7
  • Thanks alot if compiled but how do I make it so the results dont have 0 when the coin isint used aswell as plural and singular. I came up with this, is it correct? https://gyazo.com/19927151498f27706a538afbb3e1a448 – Brandon.O Mar 05 '16 at 01:58
  • I think a better way is add all a judgment at last.Just like if(quarters!=0) Systen.out.println(quarters); – TanLingxiao Mar 05 '16 at 04:50