| | |
need help with for loop
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2008
Posts: 7
Reputation:
Solved Threads: 0
import java.util.*;
public class changemakerj
{
static Scanner console=new Scanner (System.in);
static final double PRICE = 10.50;
public static void main (String[] Args)
{
double quantity,tprice,amtpaid,change1,chg$10,chg$5,chg$2,chg50,chg20,chg5,chg2,chg1;
System.out.println("Enter the quantity:");
quantity=console.nextInt();
System.out.printf("Total price
%.2f.%n",tprice=quantity*PRICE,quantity);
System.out.println();
System.out.println("Amount paid
");
amtpaid=console.nextDouble();
System.out.println();
;
System.out.printf("Change
%.2f.%n",change1=amtpaid-tprice,amtpaid);
System.out.println();
chg$10=(int)(change1/10);
change1=change1 - (chg$10*10);
System.out.println((int)(chg$10)+ "
10.00");
chg$5=(int)(change1/5);
System.out.println((int)(chg$5) + "
5.00");
change1=change1 - (chg$5*5);
chg$2=(int)(change1/2);
System.out.println((int)(chg$2) + "
2.00");
change1=change1 - (chg$2*2);
chg50=(int)(change1/0.5);
System.out.println((int)(chg50) + ":50cents");
change1=change1 - (chg50*0.5);
chg20=(int)(change1/0.2);
System.out.println((int)(chg20) + ":20cents");
change1=(int)change1 - (chg20*0.2);
chg5=(int)(change1/0.5);
System.out.println((int)(chg5) + ":5cents");
change1=change1 - (chg5*0.5);
chg2=(int)(change1/0.2);
}
}
public class changemakerj
{
static Scanner console=new Scanner (System.in);
static final double PRICE = 10.50;
public static void main (String[] Args)
{
double quantity,tprice,amtpaid,change1,chg$10,chg$5,chg$2,chg50,chg20,chg5,chg2,chg1;
System.out.println("Enter the quantity:");
quantity=console.nextInt();
System.out.printf("Total price
%.2f.%n",tprice=quantity*PRICE,quantity);System.out.println();
System.out.println("Amount paid
");amtpaid=console.nextDouble();
System.out.println();
;
System.out.printf("Change
%.2f.%n",change1=amtpaid-tprice,amtpaid);System.out.println();
chg$10=(int)(change1/10);
change1=change1 - (chg$10*10);
System.out.println((int)(chg$10)+ "
10.00");chg$5=(int)(change1/5);
System.out.println((int)(chg$5) + "
5.00");change1=change1 - (chg$5*5);
chg$2=(int)(change1/2);
System.out.println((int)(chg$2) + "
2.00");change1=change1 - (chg$2*2);
chg50=(int)(change1/0.5);
System.out.println((int)(chg50) + ":50cents");
change1=change1 - (chg50*0.5);
chg20=(int)(change1/0.2);
System.out.println((int)(chg20) + ":20cents");
change1=(int)change1 - (chg20*0.2);
chg5=(int)(change1/0.5);
System.out.println((int)(chg5) + ":5cents");
change1=change1 - (chg5*0.5);
chg2=(int)(change1/0.2);
}
}
Specifically tell us what problems you are having, what you have tried and any errors you get. Read the announcement at the top of the forum. We can't read your mind and know what you need help with.
Last edited by jasimp; May 5th, 2008 at 8:17 pm.
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
•
•
Join Date: May 2008
Posts: 6
Reputation:
Solved Threads: 0
Also try putting the post with the tags. It becomes more easy to read and identify problems
java Syntax (Toggle Plain Text)
int example
•
•
Join Date: May 2008
Posts: 1
Reputation:
Solved Threads: 0
import java.util.*;
public class ChangeMakerJ
{
static Scanner console = new Scanner(System.in);
static final double PRICE = 10.50;
public static void main(String[] Args)
{
boolean anotherBill = true;
double quantity, tprice, amtpaid, change1, chg$10, chg$5, chg$2, chg50, chg20, chg5, chg2, chg1;
for(;anotherBill
{
System.out.println("Enter the quantity:");
quantity = console.nextInt();
System.out.printf("Total price %.2f.%n", tprice = quantity * PRICE,
quantity);
System.out.println();
System.out.println("Amount paid");
amtpaid = console.nextDouble();
System.out.println();
;
System.out.printf("Change %.2f.%n", change1 = amtpaid - tprice, amtpaid);
System.out.println();
chg$10 = (int) (change1 / 10);
change1 = change1 - (chg$10 * 10);
System.out.println((int) (chg$10) + "10.00");
chg$5 = (int) (change1 / 5);
System.out.println((int) (chg$5) + "5.00");
change1 = change1 - (chg$5 * 5);
chg$2 = (int) (change1 / 2);
System.out.println((int) (chg$2) + "2.00");
change1 = change1 - (chg$2 * 2);
chg50 = (int) (change1 / 0.5);
System.out.println((int) (chg50) + ":50cents");
change1 = change1 - (chg50 * 0.5);
chg20 = (int) (change1 / 0.2);
System.out.println((int) (chg20) + ":20cents");
change1 = (int) change1 - (chg20 * 0.2);
chg5 = (int) (change1 / 0.5);
System.out.println((int) (chg5) + ":5cents");
change1 = change1 - (chg5 * 0.5);
chg2 = (int) (change1 / 0.2);
System.out.println("Want to add Another bill ? ");
console.nextBoolean();
}
}
}
public class ChangeMakerJ
{
static Scanner console = new Scanner(System.in);
static final double PRICE = 10.50;
public static void main(String[] Args)
{
boolean anotherBill = true;
double quantity, tprice, amtpaid, change1, chg$10, chg$5, chg$2, chg50, chg20, chg5, chg2, chg1;
for(;anotherBill

{
System.out.println("Enter the quantity:");
quantity = console.nextInt();
System.out.printf("Total price %.2f.%n", tprice = quantity * PRICE,
quantity);
System.out.println();
System.out.println("Amount paid");
amtpaid = console.nextDouble();
System.out.println();
;
System.out.printf("Change %.2f.%n", change1 = amtpaid - tprice, amtpaid);
System.out.println();
chg$10 = (int) (change1 / 10);
change1 = change1 - (chg$10 * 10);
System.out.println((int) (chg$10) + "10.00");
chg$5 = (int) (change1 / 5);
System.out.println((int) (chg$5) + "5.00");
change1 = change1 - (chg$5 * 5);
chg$2 = (int) (change1 / 2);
System.out.println((int) (chg$2) + "2.00");
change1 = change1 - (chg$2 * 2);
chg50 = (int) (change1 / 0.5);
System.out.println((int) (chg50) + ":50cents");
change1 = change1 - (chg50 * 0.5);
chg20 = (int) (change1 / 0.2);
System.out.println((int) (chg20) + ":20cents");
change1 = (int) change1 - (chg20 * 0.2);
chg5 = (int) (change1 / 0.5);
System.out.println((int) (chg5) + ":5cents");
change1 = change1 - (chg5 * 0.5);
chg2 = (int) (change1 / 0.2);
System.out.println("Want to add Another bill ? ");
console.nextBoolean();
}
}
}
![]() |
Similar Threads
- Help with gui loop. (C)
- Loop...without the loop (Java)
Other Threads in the Java Forum
- Previous Thread: JPanel's size and JButton
- Next Thread: Collection ? <?>
| Thread Tools | Search this Thread |
Tag cloud for Java
affinetransform android api append apple applet application arguments array arrays automation bi binary bluetooth businessintelligence busy_handler(null) chat class classes client code component database draw eclipse encryption equation error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer intersect j2me java javaexcel javaprojects jmf jni jpanel julia linked linux list loop main map method methods mobile netbeans newbie number open-source oracle oriented panel print problem program programming project qt recursion reference replaysolutions repositories return robot scanner screen scrollbar se server set singleton size sms socket sort sql string swing test threads time tree utility windows xor






