Go Back   Java Programming Forums > Java Standard Edition Programming Help > Loops & Control Statements


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 25-10-2009, 01:04 AM
Junior Member
 

Join Date: Oct 2009
Posts: 13
Thanks: 3
Thanked 0 Times in 0 Posts
SwEeTAcTioN is on a distinguished road
Exclamation Need help with loop

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();
				    	
	        
	        	
	    		
		
		
		
		
		
		
		}
	}



Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 25-10-2009, 02:16 AM
Junior Member
 

Join Date: Oct 2009
Posts: 26
Thanks: 5
Thanked 2 Times in 2 Posts
Bill_H is on a distinguished road
Default Re: Need help with loop

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);
You can alter that to allow the user to input N or n for no, and also provide a catch if they enter something other than Y y N n. Hope this helps!
Reply With Quote
  #3 (permalink)  
Old 25-10-2009, 02:29 AM
Junior Member
 

Join Date: Oct 2009
Posts: 13
Thanks: 3
Thanked 0 Times in 0 Posts
SwEeTAcTioN is on a distinguished road
Default Re: Need help with loop

Do i need to import anything?
Reply With Quote
  #4 (permalink)  
Old 25-10-2009, 02:30 AM
Junior Member
 

Join Date: Oct 2009
Posts: 10
Thanks: 5
Thanked 1 Time in 1 Post
jeremykatz is on a distinguished road
Default Re: Need help with loop

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.  
   }
}
Reply With Quote
The Following User Says Thank You to jeremykatz For This Useful Post:
SwEeTAcTioN (25-10-2009)
  #5 (permalink)  
Old 25-10-2009, 02:58 AM
Junior Member
 

Join Date: Oct 2009
Posts: 26
Thanks: 5
Thanked 2 Times in 2 Posts
Bill_H is on a distinguished road
Default Re: Need help with loop

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.
Reply With Quote
  #6 (permalink)  
Old 25-10-2009, 03:00 AM
Junior Member
 

Join Date: Oct 2009
Posts: 13
Thanks: 3
Thanked 0 Times in 0 Posts
SwEeTAcTioN is on a distinguished road
Exclamation Re: Need help with loop

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");
				    	
	        
	        	
	    		
		
		
		
		
		
		
		}
	}
Reply With Quote
  #7 (permalink)  
Old 25-10-2009, 03:29 AM
Junior Member
 

Join Date: Oct 2009
Posts: 26
Thanks: 5
Thanked 2 Times in 2 Posts
Bill_H is on a distinguished road
Default Re: Need help with loop

Try defining them both right before the do while loop.

Java Code
boolean repeat = false; // A value is necessary
char response
Then remove the char in front of response within the loop.
Reply With Quote
The Following User Says Thank You to Bill_H For This Useful Post:
SwEeTAcTioN (25-10-2009)
  #8 (permalink)  
Old 25-10-2009, 07:38 PM
Junior Member
 

Join Date: Oct 2009
Posts: 13
Thanks: 3
Thanked 0 Times in 0 Posts
SwEeTAcTioN is on a distinguished road
Default Re: Need help with loop

Thanks man it runs like a charm now
Reply With Quote
  #9 (permalink)  
Old 25-10-2009, 09:59 PM
Junior Member
 

Join Date: Oct 2009
Posts: 26
Thanks: 5
Thanked 2 Times in 2 Posts
Bill_H is on a distinguished road
Default Re: Need help with loop

Quote:
Originally Posted by SwEeTAcTioN View Post
Thanks man it runs like a charm now
No problem, glad I could help!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



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


100 most searched terms
Search Cloud
2d arraylist java actionlistener actionlistener in java actionlistener java addactionlistener addactionlistener in java addactionlistener java applications of oops could not create java virtual machine xp double format java double to int java double to integer in java double to integer java eclipse shortcut keys eclipse tutorial for beginners exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double java get mouse position java http://www.javaprogrammingforums.com/object-oriented-programming/3713-limiting-decimal-places-double.html java 2d arraylist java actionlistener java addactionlistener java double format java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programmers forum java programming forum java programming forums java programming help java project ideas java sendkeys java two dimensional arraylist java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton actionlistener jtextarea font jtextarea font color jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream two dimensional arraylist java writing ipod apps

All times are GMT. The time now is 11:34 PM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.