hope i can found help here , in fact im design program to make user enter string values contains 0s or 1s and then menu appear to help user to chose from 4 option put my problem i cant return the menu to display after first use because the program cannot run any choices after fist select ... Thank you again
Scanner input = new Scanner(System.in);
System.out.print("Enter 0s or 1s Numbers ");
String binaryString = input.nextLine();// user must enter string value contains 0 or 1
convert.displayMenu();// call the display menu which contains 4 choices
Scanner input2 = new Scanner(System.in);// the user select the 1 option from 4
int select = input2.nextInt(); // to save what user enter it
// here is the switch statement im use it
switch (select) {
case 1:
input2.equals(1);
convert.getBinary(binaryString);
convert.displayMenu();
break;
case 2:
input2.equals(2);
convert.convertBtD(binaryString);
convert.displayMenu();
break;
case 3:
input2.equals(2);
convert.convertBtO(binaryString);
convert.displayMenu();
break ;
case 4:
break;
}
while(select != 4);
}
This loop restarts the switch/case statement:
inputloop: while(true) {
int select = input2.nextInt();
switch (select) {
case 1:
input2.equals(1);
convert.getBinary(binaryString);
break;
case 2:
input2.equals(2);
convert.convertBtD(binaryString);
break;
case 3:
input2.equals(2);
convert.convertBtO(binaryString);
break ;
case 4:
break inputloop;
}
convert.displayMenu();
}
This code could be rewritten in a way that the loop condition is select != 4
but this one is more concise (from my point of view)
boolean startLoop = true;
While(startLoop) {
switch(choice) {
case 1:
break;
case 2:
break;
case 3://exitChoice
startLoop = false;
break;
}
}
You should call this method by itself like:
public void menu(){
Scanner input = new Scanner(System.in);
System.out.print("Enter 0s or 1s Numbers ");
String binaryString = input.nextLine();// user must enter string value contains 0 or 1
convert.displayMenu();// call the display menu which contains 4 choices
Scanner input2 = new Scanner(System.in);// the user select the 1 option from 4
int select = input2.nextInt(); // to save what user enter it
// here is the switch statement im use it switch (select) {
case 1:
input2.equals(1);
convert.getBinary(binaryString);
convert.displayMenu();
break;
case 2:
input2.equals(2);
convert.convertBtD(binaryString);
convert.displayMenu();
break;
case 3:
input2.equals(2);
convert.convertBtO(binaryString);
convert.displayMenu();
break ;
case 4:
break;
case 5:
System.out.println("Exit Point");
System.exit(0);
}
menu();
}
I just add this code into a method and add a call menu() at the end of the method. I also create case 5 in order to exit the application. Its your logic how to do it right but you got main outline to achieve this :)
A simple solution can be implemented like this:
while(1){
Scanner input2 = new Scanner(System.in);// the user select the 1 option from 4
int select = input2.nextInt(); // to save what user enter it
switch(select)
{
case 1:
input2.equals(1);
convert.getBinary(binaryString);
convert.displayMenu();
break;
case 2:
input2.equals(2);
convert.convertBtD(binaryString);
convert.displayMenu();
break;
case 3:
input2.equals(2);
convert.convertBtO(binaryString);
convert.displayMenu();
break ;
case 4:
break;
}
if(select == 4)
break;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.