Results 1 to 3 of 3
  1. #1
    andrewj24 is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Smile just wana see what u think

    Hi im new to this forum!
    Im taking intro to java.
    one of my assignments is to figure out how many quarters,nickles,dimes,pennies i am supposed to return for change.
    i got it working just wana get input on improvements or other ways that this should be done.

    Thanx for reading.
    Java Code:
    import java.util.Scanner;
    import java.util.*;
    public class J3 {
    
       public static void main(String[] args) 
       {
    while(true){
    float change3,total,bill=0;
    
    int change,change2,quarter,dime,nickel,pennie,doller;
    
    Scanner input = new Scanner(System.in);
    
    System.out.println("What is the total amount?\n");
    total=input.nextFloat();
    
         
    while (bill<total){
    System.out.printf("Please enter payment amount\n");
    bill=input.nextFloat();
    }
    change3=bill-total;
    System.out.printf("Your change is %.2f\n",change3);
    
    bill*=100;
    total*=100;
    int bill2 = (int) bill;
    int total2 = (int) total;
    
    change2=bill2-total2;
    change=change2;
    
    doller=change/100;
    change=change%100;
    quarter=change/25;
    change=change%25;
    dime=change/10;
    change=change%10;
    nickel=change/5;
    change=change%5;
    pennie=change/1;
    System.out.printf("%d doller(s)\n",doller);
    System.out.printf("%d quarter(s)\n",quarter);
    System.out.printf("%d dime(s)\n",dime);
    System.out.printf("%d nickel(s)\n",nickel);
    System.out.printf("%d pennie(s)\n",pennie);
    }
    }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Place the nominal amounts in an array and use a loop.
    Use if statement to only print amounts greater than 0 ie do not display "0 dimes"
    Use if statement to add s only when amount is greater than 1 ie "1 quarter 2 dimes". Penny/Pennies hmmmm!

  3. #3
    andrewj24 is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default

    thank you very much havent covered arrays yet.
    looks like i got some reading to do thank you. i will post revised code :)

Posting Permissions

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