Advertising (This ad goes away for registered users. You can Login or Register)

java loop confusion

Programming on your favorite platform, for your favorite platform? Post here
Post Reply
User avatar
Minimur12
Posts: 1102
Joined: Sat Sep 01, 2012 2:22 pm

java loop confusion

Post by Minimur12 » Mon Oct 15, 2012 7:45 pm

im doing a little calculator and its working fine, but my only problem is with the 'default' i was wondering is there a way to return it to the top, so the
user can input 1,2 or 3 again??

Code: Select all

import java.util.Scanner ;
public class Main {

	
	public static void main(String[] args) {
		Scanner Aidan = new Scanner (System.in);
		
		int choice ;
		
		System.out.println("Press 1 for Addition and 2 for Multiplication and 3 for Division");
		
		choice = Aidan.nextInt();
		
		switch (choice){
		
	case 1:	
			
		Addition AddingObject = new Addition();
		AddingObject.Adding();
		
		break;		
		
	case 2:
			
		Multiply TimesObject = new Multiply();
		TimesObject.Times();
		break;
	
	case 3:
		Divide DivideObject = new Divide() ;
		DivideObject.Division();
		break;
		
	default : 
		System.out.println("choose 1 , 2 or 3 ");
		
		}
		
	}

}
Advertising
I'm not on the forum much, if you want to let me know about something, shoot me a PM!
ImageImage

User avatar
vaygr_nl
Posts: 40
Joined: Mon Oct 08, 2012 12:20 pm

Re: java loop confusion

Post by vaygr_nl » Mon Oct 15, 2012 10:17 pm

i dont know how its done in java but you should create a loop. now it just stops after the break;
Advertising

User avatar
Minimur12
Posts: 1102
Joined: Sat Sep 01, 2012 2:22 pm

Re: java loop confusion

Post by Minimur12 » Tue Oct 16, 2012 6:55 am

vaygr_nl wrote:i dont know how its done in java but you should create a loop. now it just stops after the break;

thats what im trying to find out......not to be rude but im sick of non useful replies
I'm not on the forum much, if you want to let me know about something, shoot me a PM!
ImageImage

User avatar
Dallox
Posts: 74
Joined: Tue Sep 28, 2010 1:56 pm

Re: java loop confusion

Post by Dallox » Tue Oct 16, 2012 11:40 am

while (true){
loop stuff
}

that will loop the program until it exit.
E-Yup.

User avatar
vaygr_nl
Posts: 40
Joined: Mon Oct 08, 2012 12:20 pm

Re: java loop confusion

Post by vaygr_nl » Tue Oct 16, 2012 11:59 am

Minimur12 wrote:
vaygr_nl wrote:i dont know how its done in java but you should create a loop. now it just stops after the break;

thats what im trying to find out......not to be rude but im sick of non useful replies
i only knew how to do it in c#, but its the same i see.

niksko12
Posts: 97
Joined: Wed Oct 10, 2012 6:55 am
Location: Philippines

Re: java loop confusion

Post by niksko12 » Wed Oct 24, 2012 8:21 am

Minimur12 wrote:im doing a little calculator and its working fine, but my only problem is with the 'default' i was wondering is there a way to return it to the top, so the
user can input 1,2 or 3 again??

Code: Select all

import java.util.Scanner ;
public class Main {

	
	public static void main(String[] args) {
		Scanner Aidan = new Scanner (System.in);
		
		int choice ;
		
		System.out.println("Press 1 for Addition and 2 for Multiplication and 3 for Division");
		
		choice = Aidan.nextInt();
		
		switch (choice){
		
	case 1:	
			
		Addition AddingObject = new Addition();
		AddingObject.Adding();
		
		break;		
		
	case 2:
			
		Multiply TimesObject = new Multiply();
		TimesObject.Times();
		break;
	
	case 3:
		Divide DivideObject = new Divide() ;
		DivideObject.Division();
		break;
		
	default : 
		System.out.println("choose 1 , 2 or 3 ");
		
		}
		
	}

}
try this

Code: Select all

[code]
import java.util.Scanner ;
public class Main {

	
	public static void main(String[] args) {
		Scanner Aidan = new Scanner (System.in);
		
		int choice ;
		int cont;
		System.out.println("Press 1 for Addition and 2 for Multiplication and 3 for Division");
		
		choice = Aidan.nextInt();

		do{
		switch (choice){
		
	case 1:	
			
		Addition AddingObject = new Addition();
		AddingObject.Adding();
		
		break;		
		
	case 2:
			
		Multiply TimesObject = new Multiply();
		TimesObject.Times();
		break;
	
	case 3:
		Divide DivideObject = new Divide() ;
		DivideObject.Division();
		break;
		
	default : 
		System.out.println("choose 1 , 2 or 3 ");
		
		}
		
	}
System.out.println("Press 1 if you want to continue, Press 2 if you want to exit!");
cont = Aidan.nextInt();
}while(cont==1);

}
I added a do while statement and some codes to ask the user if he wants to continue or not.
That should do the trick. ^^
You hate me?
Cool.
I don't wake up everyday just to please you..

kk.xie
Posts: 37
Joined: Fri May 11, 2012 4:49 am

Re: java loop confusion

Post by kk.xie » Tue Oct 30, 2012 12:47 pm

use while

User avatar
watswat5
Posts: 72
Joined: Thu Jan 10, 2013 11:43 pm

Re: java loop confusion

Post by watswat5 » Mon Sep 16, 2013 11:11 pm

I reccomend you visit this site (http://www.tutorialspoint.com/java/java ... ontrol.htm). It explains all of Java's available types of loops.
"In theory, there is no difference between theory and practice. But, in practice, there is." - Jan L. A. van de Snepscheut
"If Java had true garbage collection, most programs would delete themselves upon execution." - Robert Sewell

Post Reply

Return to “Programming”