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??
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!
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??
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 ");
}
}
}
[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..
"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