Page 1 of 1

java loop confusion

Posted: Mon Oct 15, 2012 7:45 pm
by Minimur12
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 ");
		
		}
		
	}

}

Re: java loop confusion

Posted: Mon Oct 15, 2012 10:17 pm
by vaygr_nl
i dont know how its done in java but you should create a loop. now it just stops after the break;

Re: java loop confusion

Posted: Tue Oct 16, 2012 6:55 am
by Minimur12
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

Re: java loop confusion

Posted: Tue Oct 16, 2012 11:40 am
by Dallox
while (true){
loop stuff
}

that will loop the program until it exit.

Re: java loop confusion

Posted: Tue Oct 16, 2012 11:59 am
by vaygr_nl
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.

Re: java loop confusion

Posted: Wed Oct 24, 2012 8:21 am
by niksko12
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. ^^

Re: java loop confusion

Posted: Tue Oct 30, 2012 12:47 pm
by kk.xie
use while

Re: java loop confusion

Posted: Mon Sep 16, 2013 11:11 pm
by watswat5
I reccomend you visit this site (http://www.tutorialspoint.com/java/java ... ontrol.htm). It explains all of Java's available types of loops.