Results 1 to 5 of 5

Thread: Java Progammers, i need help

  1. #1
    Junior Member
    Join Date
    Jul 2009
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Java Progammers, i need help

    I need help with my code. Its basically supposed to be like vending machine. You input the value of any item from 25 cents upto 1 dollar. Then the program will tell you how much change you will recieve the coins you will recieve (least amount of coins).
    I made this program, but is there any way to make it shorter/better?

    import java.io.*;
    public class vending {
    public static void main(String[] args)throws IOException
    {
    double dollar = 100;
    double price;
    int change;
    int quarter, dime, nickel;
    String quar, dimes, nickels;
    BufferedReader in;

    in = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Please enter price of item (in cents): ");

    price = Double.parseDouble(in.readLine());
    if (price < 25)
    System.out.print("Sorry, we don't have any items for that price.");
    else {
    System.out.println("n" + "You bought and item for " + (int)price + " cents.n");
    change = (int)(dollar - price);
    System.out.println("After giving me $1, your change will be " + change + " cents.");
    System.out.println("It will be given to you as:n");

    quarter = change / 25;
    dime = (change - (quarter * 25)) / 10;
    nickel = (change - (quarter * 25) - (dime * 10)) / 5;

    if (quarter == 1)
    quar = "quarter";
    else
    quar = "quarters";
    if (dime == 1)
    dimes = "dime";
    else
    dimes = "dimes";
    if (nickel == 1)
    nickels = "nickel";
    else
    nickels = "nickels";

    if (quarter == 0)
    if (dime == 0)
    if (nickel == 0)
    System.out.println("no coins.");
    else
    System.out.println(nickel + " " + nickels);
    else
    if (nickel == 0)
    System.out.println(dime + " " + dimes);
    else
    System.out.println(dime + " " + dimes + "\n\n" + nickel + " " + nickels);
    else
    if (dime == 0)
    if (nickel == 0)
    System.out.println(quarter + " " + quar);
    else
    System.out.println(quarter + " " + quar + "\n\n" + nickel + " " + nickels);
    else
    if (nickel == 0)
    System.out.println(quarter + " " + quar + "\n\n" + dime + " " + dimes);
    else
    System.out.println(quarter + " " + quar + "\n\n" + dime + " " + dimes + "\n\n" + nickel + " " + nickels);
    }
    }
    }



    i apologize if its hard to read
    Last edited by babbalabba; 10-11-2009 at 04:49 AM. Reason: Rewrote

  2. #2
    Senior Member messie's Avatar
    Join Date
    Jun 2009
    Posts
    5,118
    Thanks
    68
    Thanked 7,150 Times in 1,065 Posts
    is this the kind "this is my first java program"?

  3. #3
    Junior Member
    Join Date
    Jul 2009
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Its not my 1st, its like my 3rd. But i actually want to improve this to show my teacher.

  4. #4
    Senior Member messie's Avatar
    Join Date
    Jun 2009
    Posts
    5,118
    Thanks
    68
    Thanked 7,150 Times in 1,065 Posts
    what more do you want? if its working then np. it is clear, you can understand what it is doing... what more?

  5. #5
    Junior Member
    Join Date
    Jul 2009
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just want to know if it can be made better

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •