Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Change Maker

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Change Maker

    package change.maker;
    import java.util.Scanner;
    public class ChangeMaker 
    {
        public static void main(String[] args)
        {
            Scanner input = new Scanner(System.in);
            System.out.println("Please insert a set amount of money");
            double money;
            money = input.nextDouble();
            int dollars = 10, quarters = 10, dimes = 10, nickels = 10, pennies = 10;
            dollars = (int)(money%1);
            money = money - dollars;
            quarters = (int)(money%.25);
            money = money - quarters;
            dimes = (int)(money%.1);
            money = money - dimes;
            nickels = (int)(money%.25);
            money = money - nickels;
            pennies = (int)(money%.01);
            System.out.println(dollars + " dollars " + quarters + " quarters " + dimes + " dimes " + nickels + " nickels " + pennies + " pennies.");
        }
    }


    What is wrong with this?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Change Maker

    You tell us. What does this code do?

    What happened when you stepped through it with a debugger, or at least added some print statements, to figure out when the program's execution differs from what you expected?

    It looks like you wrote the whole thing before really testing it, which is definitely not the way to go. Start over, and test one tiny piece at a time. Can you write a program that outputs the correct amount of dollars, ignoring any change? If you can't get that working, then use a debugger or print statements to figure out what you're doing wrong. When you do get that working, then move on to including quarters, ignoring the rest of the change. Repeat that process until you're done.

    If you get stuck, post that tiny program (also known as an MCVE), and tell us exactly what it's doing wrong, and we'll go from there.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Change Maker

    package changemaker;
    import java.util.Scanner;
     
    public class ChangeMaker 
    {
        public static void main(String[] args) 
        {
            Scanner scanner = new Scanner(System.in);
            int amount, originalAmount, quarters, dimes, nickels, pennies;
     
            System.out.println("Enter the amount of change");
            amount = scanner.nextInt();
            originalAmount = amount;
            amount = amount.replace('X','');
     
            quarters = amount / 25;
            amount = amount % 25;
            dimes = amount / 10;
            amount = amount %10;
            nickels = amount / 5;
            amount = amount %5;
            pennies = amount;
     
            System.out.println(originalAmount + " cents in coins can be given as:\n" + quarters + " quarters\n" + dimes + " dimes\n" + nickels + " nickels\n" + pennies + " pennies");
        }
     
    }

    I think i figured it out. Thanks!

Similar Threads

  1. QUIZ MAKER CODES IN JAVA
    By Mikonkidial in forum Member Introductions
    Replies: 1
    Last Post: November 13th, 2013, 01:19 PM
  2. QUIZ MAKER CODES IN JAVA
    By Mikonkidial in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 13th, 2013, 01:11 PM
  3. how to change DB design for a change in java
    By harry7ster in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 7th, 2013, 12:57 PM
  4. Java Change Maker need help with rounding up the pennies
    By sxl4 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 3rd, 2013, 07:19 AM
  5. Game Maker Language and Game Maker and Zelda Classic thread
    By Fira in forum Other Programming Languages
    Replies: 3
    Last Post: April 17th, 2012, 08:59 AM

Tags for this Thread