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 4 of 4

Thread: Help with high school java problem

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with high school java problem

    Hi! I have to create a program where the user enters a certain amount of money and the program will tell them how many of each bill (5, 10, 20, 100) and coins (loonie, toonie, nickles, dimes, quarters) the amount they entered consists of, USING THE LEAST AMOUNT OF BILLS AND COINS. I have created a code already but for some reason it wont output the correct numbers!

    I am a beginner and need a bit of guidance and need to know why my code isnt working.

    Thanks for the help!

    import java.io.*;
    public class ReviewProblem1
    {
    public static void main (String [] args) throws IOException
    {
     
    BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));//BufferedReader reads input
     
    double c = 0;
    double f = 0;
    double i = 0;
    double l = 0;
    double o = 0;
    double r = 0;
    double u = 0;
    double x = 0;
    double intc = 0;
     
    System.out.println("Please enter your input amount:");//asks user to enter input amount
    String input = myInput.readLine();//assigns users input to inputA
    double amount = Double.parseDouble (input);//converts inputA into double
     
    if (amount == 100)
    { 
    System.out.println ("1 100 dollar bill");
    }
     
    if (amount < 100)
    {
    if (amount < 100 && amount >= 50)
    {
    double inta = amount / 50;
    double intb = Math.floor(inta);
     
    System.out.println (intb + " 50 dollar bills");
     
    intc = amount - 50;
    }
     
    else if (intc >= 20 && intc < 50)
    {
    double a = intc / 20;
    double b = Math.floor(a);
     
    System.out.println ( b + " 20 dollar bills");
    c = intc - (b*20);
    }
     
     
    else if (c >= 10 && c <20)
    {
    double d = c / 10;
    double e = Math.floor(d);
     
    System.out.println (e + " 10 dollar bills");
    f = c - (e*10);
    }
     
    else if (f >= 5 && f < 10)
    {
    double g = f / 5;
    double h = Math.floor(g);
     
    System.out.println (h + " 5 dollar bills");
    i = f - (h*5);
    }
     
    else if (i >= 2 && i<5)
    {
    double j = i / 2;
    double k = Math.floor(j);
     
    System.out.println (k + " toonies");
    l = i - (k*2);
    }
     
    else if (l >= 1 && l< 2)
    {
    double m = l / 1;
    double n = Math.floor(m);
     
    System.out.println (n + " loonies");
    o = l - (n*1);
    }
     
    else if (o >= 0.25 && o < 1)
    {
    double p = o / 0.25;
    double q = Math.floor(p);
     
    System.out.println (q + " quarters");
    r = o - (q*0.25);
    }
     
    else if (r >= 0.10 && r < 0.25)
    {
    double s = r / 0.10;
    double t = Math.floor(s);
     
    System.out.println (t + " dimes");
    u = r - (t*0.10);
    }
     
    else if (u >= 0.05 && u < 0.10);
    {
    double v = u / 0.05;
    double w = Math.floor(v);
     
    System.out.println ( w + " nickles");
    x = u - (w*0.05);
    }
     
    if (x >= 0.01 && x < 0.05)
    {
    double y = x / 0.01;
    double z = Math.floor(y);
     
    System.out.println (z + " pennies");
    }
    }
    }
    }


  2. #2
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: Help with high school java problem

    I got a little lost following the code structure. For example what happens if someone types 28.50? intc is initialized at the start of your code but if it doesn't go through the first code block (100 > value > 50) intc is never modified unless I am missing something.

    A few things to help you out a bit. You may want to look a modulus, name your variables in a more descriptive manner, and indentation of code blocks. Take a look at your if/else if statements. Also is there a need for all those variables because you will use them in the future or can you get away with a single variable that can be modified as you go.

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Help with high school java problem

    Quote Originally Posted by maddiexc View Post
    I am a beginner and need a bit of guidance and need to know why my code isnt working.
    What makes you say the code "isnt working"? Does it compile? Is there an error? Does it give any output?
    Explain what it does that you think it should not do, or what it you want it to do that it does not do.

  4. #4
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Help with high school java problem

    Hello.
    Before writing the code, first come up with an algorithm. Take same sample inputs such as 30, 52.76, etc and check if your algorithm works.
    Once algorithm is fine then do the coding.
    You have ERRORS in the algorithm itself you currently used.

    Syed.

Similar Threads

  1. Java assignment school
    By Ur Nerd in forum Paid Java Projects
    Replies: 3
    Last Post: September 7th, 2013, 03:22 AM
  2. Simple java beginners question (for school)
    By Jeesie in forum Java Theory & Questions
    Replies: 5
    Last Post: November 7th, 2012, 05:32 PM
  3. High School Java Programming Class
    By SDennis52 in forum Collections and Generics
    Replies: 1
    Last Post: May 7th, 2012, 12:38 AM
  4. Problem with School Assignment.
    By SaltSlasher in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 17th, 2012, 04:12 PM
  5. Problem with code for school assignment?
    By Mellisa315 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 16th, 2010, 08:36 PM

Tags for this Thread