|
||
|
|||
|
So i got this assignment in computer science that computes change from money given etc. etc.
Anyways what i need help with is at the end of the program it asks you if you want to run the program again. I got it to run it again one more time with an if statement but it only runs on more time and it runs again no matter what you type in when it asks you if you want to run it again. How would i put it in a loop and mke it run over and over and if they type no it ends the program? All help is appreciated very much Java Code
import java.io.*;
public class Temp2
{
public static void main(String[] args) throws IOException {
BufferedReader input= new BufferedReader(new InputStreamReader(System.in));
double prc;
double rcvd;
int dllrs,qrtrs,dms,nckls,pnns;
System.out.println("Enter Amount Recieved");
rcvd=Double.parseDouble(input.readLine());
System.out.println("Enter Price Of Item");
prc=Double.parseDouble(input.readLine());
double chngdue=rcvd-prc;
System.out.println("Change Due"+" "+chngdue);
double lftvr;
dllrs=(int)chngdue/1;
System.out.println("Dollars:"+" "+dllrs);
lftvr=chngdue-dllrs;
qrtrs=(int)(lftvr/.25);
System.out.println("Quarters:"+" "+qrtrs);
lftvr-=(qrtrs*.25);
dms=(int)(lftvr/.10);
System.out.println("Dimes:"+" "+dms);
lftvr-=(dms*.10);
nckls=(int)(lftvr/.05);
System.out.println("Nickels:"+" "+nckls);
lftvr-=(nckls*.05);
pnns=(int)(lftvr/.01);
System.out.println("Pennies:"+" "+pnns);
System.out.println("Would you like to run the program again");
String answer="yes";
answer=input.readLine();
if (answer=="yes")
System.out.println("OK");
System.out.println("Enter Amount Recieved");
rcvd=Double.parseDouble(input.readLine());
System.out.println("Enter Price Of Item");
prc=Double.parseDouble(input.readLine());
System.out.println("Change Due"+" "+chngdue);
System.out.println("Dollars:"+" "+dllrs);
System.out.println("Quarters:"+" "+qrtrs);
System.out.println("Dimes:"+" "+dms);
System.out.println("Nickels:"+" "+nckls);
System.out.println("Pennies:"+" "+pnns);
System.out.println("Would you like to run theprogram again");
answer=input.readLine();
}
}
|
|
|||
|
The easiest way I've found to loop a program is to use a do while loop around the entire thing. What you have is the original program, then a duplicate inside an if statement (which by the way needs {} if you want it to do more than the first line of code beneath it). This is just unnecessary code.
Without "doing it for you", try something like this: Java Code
do {
boolean repeat = false;
// Code for program here
// ...
System.out.println("Would you like to run the program again?");
System.out.println("Y or y for yes. Any other key for no");
char response = input.nextLine().charAt(0);
if ((response == 'Y') || (response == 'y'))
repeat = true;
} while (repeat == true);
|
|
|||
|
Here is the one I did.
Java Code
import javax.swing.*;
public class katzensteinHW3 {
public static void main( String[] args ) {
//...Varibles declared for whole program.
int amount, originalAmount, halfDollar, quarters, dimes, nickels, pennies, selection;
boolean runAgain = true;
//...Loop
do{
//...Enter the integer 1-100.
String amountString = JOptionPane.showInputDialog(
"Enter a whole number from 1 to 99.\n"
+ "I will output a combination of coins\n"
+ "that equals that amount of change.");
amount = Integer.parseInt(amountString);
originalAmount = amount;
halfDollar = amount/50;
amount = amount%50;
quarters = amount/25;
amount = amount%25;
dimes = amount/10;
amount = amount%10;
nickels = amount/5;
amount = amount%5;
pennies = amount;
//...Showes orginalAmount (Integer intered for amountString).
JOptionPane.showMessageDialog(null,
originalAmount
+ " cents in coins can be given as:\n"
+ halfDollar + " half dollar\n"
+ quarters + " quarters\n"
+ dimes + " dimes\n"
+ nickels + " nickels and\n"
+ pennies + " pennies");
//...Asks if you want to run anouther.
selection = JOptionPane.showConfirmDialog(null,
"Would you like to run another?","Confirmation",
JOptionPane.YES_NO_OPTION);
runAgain = (selection == JOptionPane.YES_OPTION);
//...Continues the loop.
}while (runAgain);
System.exit(0); //...Ends the window.
}
}
|
| The Following User Says Thank You to jeremykatz For This Useful Post: | ||
SwEeTAcTioN (25-10-2009) | ||
|
|||
|
It's usually best to let someone learn on their own and just guide them than give them the answer, jeremykatz.
But in response to your question, the only import you would need is for the Scanner class if you choose to use that. |
|
|||
|
So im still having problems it say "cannot find symbol" for respone or repeat
Java Code
import java.io.*;
import javax.swing.*;
public class Temp2
{
public static void main(String[] args) throws IOException {
BufferedReader input= new BufferedReader(new InputStreamReader(System.in));
do {
double prc;
double rcvd;
int dllrs,qrtrs,dms,nckls,pnns;
System.out.println("Enter Amount Recieved");
rcvd=Double.parseDouble(input.readLine());
System.out.println("Enter Price Of Item");
prc=Double.parseDouble(input.readLine());
double chngdue=rcvd-prc;
System.out.println("Change Due"+" "+chngdue);
double lftvr;
dllrs=(int)chngdue/1;
System.out.println("Dollars:"+" "+dllrs);
lftvr=chngdue-dllrs;
qrtrs=(int)(lftvr/.25);
System.out.println("Quarters:"+" "+qrtrs);
lftvr-=(qrtrs*.25);
dms=(int)(lftvr/.10);
System.out.println("Dimes:"+" "+dms);
lftvr-=(dms*.10);
nckls=(int)(lftvr/.05);
System.out.println("Nickels:"+" "+nckls);
lftvr-=(nckls*.05);
pnns=(int)(lftvr/.01);
System.out.println("Pennies:"+" "+pnns);
System.out.println("Would you like to run the program again");
System.out.println("Would you like to run the program again?");
System.out.println("Y or y for yes. Any other key for no");
boolean repeat= false;
char response= input.readLine().charAt(0);
if ((response == 'Y') || (response == 'y'))
repeat= true;
} while (repeat==true);
if (response=='Y')
System.out.println("Enter Amount Recieved");
double rcvd;
rcvd=Double.parseDouble(input.readLine());
System.out.println("Enter Price Of Item");
double prc;
prc=Double.parseDouble(input.readLine());
int dllrs,qrtrs,dms,nckls,pnns;
double chngdue=rcvd-prc;
System.out.println("Change Due"+" "+chngdue);
double lftvr;
dllrs=(int)chngdue/1;
System.out.println("Dollars:"+" "+dllrs);
lftvr=chngdue-dllrs;
qrtrs=(int)(lftvr/.25);
System.out.println("Quarters:"+" "+qrtrs);
lftvr-=(qrtrs*.25);
dms=(int)(lftvr/.10);
System.out.println("Dimes:"+" "+dms);
lftvr-=(dms*.10);
nckls=(int)(lftvr/.05);
System.out.println("Nickels:"+" "+nckls);
lftvr-=(nckls*.05);
pnns=(int)(lftvr/.01);
System.out.println("Pennies:"+" "+pnns);
System.out.println("Would you like to run the program again");
System.out.println("Would you like to run the program again?");
System.out.println("Y or y for yes. Any other key for no");
}
}
|
|
|||
|
Try defining them both right before the do while loop.
Java Code
boolean repeat = false; // A value is necessary char response |
| The Following User Says Thank You to Bill_H For This Useful Post: | ||
SwEeTAcTioN (25-10-2009) | ||
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Little Loop Problem | chronoz13 | Loops & Control Statements | 1 | 17-10-2009 09:40 AM |
| literal Loop | chronoz13 | Loops & Control Statements | 3 | 11-10-2009 03:03 AM |
| can any one do this in a loop? (for loop) | chronoz13 | Loops & Control Statements | 4 | 05-10-2009 01:31 AM |
| For loop tricks | helloworld922 | Java Code Snippets and Tutorials | 0 | 04-10-2009 06:09 AM |
| [SOLVED] for loop | lotus | Loops & Control Statements | 2 | 13-07-2009 04:47 AM |