How to drop out of a programme in java? - java

I have made a main menu in my code. When the user types q I want the code to stop running. Instead it just presents the menu again, what code should i use to stop this. If you press any other letter it works.Thank you
import java.util.Random;
public class Aaa {
AQAConsole2016 console = new AQAConsole2016();
Random random = new Random();
int boardSize;
boolean moveIsValid;
char [][] board;
int move;
char choice;
String playerName="Human";
String player2="Computer";
public Aaa() {
boardSize = 6;
do {
displayMenu();
choice = getMenuChoice(playerName);
switch (choice) {
case 'p' : playGame(playerName, boardSize);
break;
case 'e' : playerName = getPlayersName();
break;
case 'c' : boardSize = changeBoardSize();
break;
case 'm' : Multiplayer( boardSize,playerName,player2);
break;
case 'r' :readBoard(board,boardSize);
break;
case 'q' : quit();
}
}while (choice!='p'||choice!='e'||choice!='c'||choice!='m'||choice!='r'||choice!='q');
}
void quit(){
}

this is not going to work just because
case 'q' : quit(); is just a call to a method, i.e. when the method is done it will return to the do while...
you need instead to add some boolean condition that can be modified if the option menu is selected...
doing something like
while (!exit);
where exit is a boolean variable...
and you can in the void method:
void quit(){
//Do your stuff before exit
exit=true;
}

Related

How to choose and write a loop for this specific task?

I am pretty new to Java. Sorry if this is a lame question. I have this chunk of code. It is not the whole thing obviously.
char option = scan.next().charAt(0);
for (option !='a'||option !='b'||option !='c'||option !='d'||option !='e'||option !='f'||option !='q') {
System.out.println("Please pick an option from the menu above");
}
int lengthOne = stringOne.length(); //Getting the lengths for each string
int lengthTwo = stringTwo.length();
if (option == 'a'|| option == 'A') { //If the user inputs a
if (lengthOne == lengthTwo) { //If both lengths are equal
System.out.println("The strings are the same length");
}
Looking for some advice on which loop i should use for this code. The options will be A-F and then Q to quit.
The while loop can seem messy for what you are trying to accomplish. I would use a Switch statement inside a 'do while' loop.
If the user input doesn't match a 'case' then it will go to the default.
When a user enters 'q' to quit then boolean validSelection turns to true and you will exit the 'do while' loop.
public static void main( String[] args )
{
Scanner scan = new Scanner( System.in );
boolean validSelection = false;
do
{
System.out.println( "Please pick an option from the menu above" );
char option = scan.next().charAt( 0 );
switch( option )
{
case 'a':
break;
case 'b':
break;
case 'c':
break;
case 'd':
break;
case 'e':
break;
case 'f':
break;
case 'q':
validSelection = true;
break;
default:
System.out.println( "Choice invalid." );
break;
}
}
while( validSelection == false );
}
}
Add a scan inside the loop.
char option = scan.next().charAt(0);
while (option !='a'||option !='b'||option !='c'||option !='d'||option !='e'||option !='f'||option !='q') {
System.out.println("Please pick an option from the menu above");
option = scan.next().charAt(0);
}
Try this
Scanner scan = new Scanner(System.in);
char option = scan.next().charAt(0);
while (option != 'a' && option !='b' && option != 'c'&& option !='d'&& option !='e'&& option !='f'&& option !='q') {
System.out.println("Please pick an option from the menu above");
option = scan.next().charAt(0);
}
You will need the ANDs instead of the ORs or it wont work

infinite loop unable to close loop in Switch Statements

import java.util.Scanner;
class MenuFastFood {
public static void main(String[] args) {
String s;
char order;
Scanner keyboard = new Scanner(System.in);
s = keyboard.next();
s = s.toUpperCase();
order = s.charAt(0);
do {
switch(order) {
case 'A':
System.out.println("CheeseBurger");
System.out.println("Onion Rings");
System.out.println("Soda");
break;
case 'B':
System.out.println("Hot dog");
System.out.println("Fries");
System.out.println("Milk Shake");
break;
default:
System.out.println("error");
return;
case 'X':
System.out.println("EXIT");
break;
}
}while(order != 'X');
}
my program is suppose to pick an item based on the character enter in to keybaord and then loops back if another item is selected. when i run this and pick an item. it loops that item for ever. How do i get that to stop and makes it able for me to select another item?
Move your code for reading input to inside do..while
s = keyboard.next();
s = s.toUpperCase();
order = s.charAt(0);

Nested do while loop jump from one to another

I am having some logic difficulties when trying to use a do-while loop. In my main() method. I am trying to prompt user again and again if they entered anything larger than 6:
do{
System.out.println("select your option: ");
System.out.println("1.option1");
System.out.println("2.option2");
System.out.println("3.option3");
System.out.println("4.option4");
System.out.println("5.option5");
System.out.println("6.Quit");
optionChoice = sc.nextInt();
switch (optionChoice) {
case 1:
option1Method();
break;
}
} while (optionChoice > 6);
Then inside my option1Method(), I have another do while loop:
do {
System.out.println("select your option: ");
System.out.println("1.opt1 method1");
System.out.println("2.opt2 method2");
System.out.println("3.opt3 method3");
System.out.println("4.Back");
optOption = sc.nextInt();
switch (optOption ) {
case 1: //do stuffs, same for case 2 and 3
break;
case 4: return;
default: break;
}
} while (optOption > 4);
For this method, I am trying to prompt user the choice again and again as long as they entered anything larger than 4. Then, when they entered 4, it should go back to the do while loop in main() method.
However, for the second do-while loop, when I entered 4, the program itself is just terminated. Any ideas?
Thanks in advance.
In the main method set the condition as:
optionChoice != 6
I am not sure if this is what you want, but I have written the following for you:
import java.util.Scanner;
public class Answer {
static Scanner sc = new Scanner(System.in);
static int optionChoice;
public static void main(String[] args) {
do{
System.out.println("select your option: ");
System.out.println("1.option1");
System.out.println("2.option2");
System.out.println("3.option3");
System.out.println("4.option4");
System.out.println("5.option5");
System.out.println("6.Quit");
optionChoice = sc.nextInt();
switch (optionChoice) {
case 1:
option1Method();
break;
}
} while (optionChoice > 6);
}
public static void option1Method() {
int optOption;
do {
System.out.println("select your option: ");
System.out.println("1.opt1 method1");
System.out.println("2.opt2 method2");
System.out.println("3.opt3 method3");
System.out.println("4.Back");
optOption = sc.nextInt();
switch (optOption ) {
case 1: //do stuffs, same for case 2 and 3
break;
case 4:
optionChoice = 7; // you have to make this value greater than 6 if you want to continue in the loop
return;
default: break;
}
} while (optOption > 4);
}
}
The problem when you enter 4 is that you go back to the main method, and the value you entered for optionChoice is 1 which makes false the condition of the while loop.
EDIT:
In response to #Timeout who is totally right by claiming I am assuming that optionChoice is a "global variable".
To keep your functionality I guess you should just have the following condition in the do-while loop of the main() method:
optionChoice > 6 || optionChoice == 1
EDIT:
what if you add as a condition in the second while loop
optOption != 4
so that you will remain in that loop until the user enters 4
EDIT TO HANDLE optionXMethod where X is a number:
do{
System.out.println("select your option: ");
System.out.println("1.option1");
System.out.println("2.option2");
System.out.println("3.option3");
System.out.println("4.option4");
System.out.println("5.option5");
System.out.println("6.Quit")
optionChoice = sc.nextInt();
switch (optionChoice) {
case 1:
option1Method();
break;
case 2:
option2Method();
break;
case X:
optionXMethod();
break;
}
} while (optionChoice != 6);
void option1Method() {
int optOption;
do {
System.out.println("select your option: ");
System.out.println("1.opt1 method1");
System.out.println("2.opt2 method2");
System.out.println("3.opt3 method3");
System.out.println("4.Back");
optOption = sc.nextInt();
switch (optOption ) {
case 1: //do stuffs, same for case 2 and 3
break;
// you do not need the case 4: because when optOptiontakes the value of 4 it leaves the loop
default: break;
}
} while (optOption != 4);
}
....
General case:
void optionXMethod() {
int optOption;
do {
System.out.println("select your option: ");
System.out.println("1.opt1 method1");
System.out.println("2.opt2 method2");
System.out.println("3.opt3 method3");
System.out.println("4.opt4 method4");
// more options
System.out.println("X.Back"); // where X is the number option of Back
optOption = sc.nextInt();
switch (optOption ) {
case 1: //do stuffs, same for case 2 and 3
break;
// you do not need the case 4: because when optOptiontakes the value of 4 it leaves the loop
default: break;
}
} while (optOption != X); // whatever the value of X is should be consider for this condition
}

On Switch How to use logic operator on case JAVA

i have a problem i dont know what to put on case section, when ever the user input their grades from 0-100 there are output corresponds to their grades failed,good,verygood,excellent.
import java.util.Scanner;
public class ProgTestI {
public static void main (String args[]){
Scanner pao = new Scanner(System.in);
System.out.print("Grades: ");
String grades = pao.next();
int grado = Integer.parseInt(grades);
switch (grado){
case =<74: /* iwant to put 0 to 74*/
System.out.println("Failed");
case : /* 75-80*/
System.out.println("bellow average");
case : /*81-85*/
System.out.println("average");
case : /*86-90*/
System.out.println("Good");
case : /*91-96*/
System.out.println("VeryGood");
default:
}
}
}
You cannot use switch for ranges, you need to replace this chunk of code with proper if/else blocks.
Switch works only on numeric values, but it works like
if(numericVal == 40)
So writing it for ranges is... waste of code, and not readable.
You need to rewrite it:
if( g <= 74){
...
}else if( g > 74 && g <= 80 ){
...
Your case code is incorrect, you can do as Beri mentioned.
If you want to implement switch statement in your application, then you can do as follows:
public static void main(String[] args) {
Scanner pao = new Scanner(System.in);
System.out.print("Grades: ");
String grades = pao.next();
int grado = Integer.parseInt(grades);
int checkedCase=0;
if(grado<=74){
checkedCase=1;
}
else if(grado>=75&&grado<=80){
checkedCase=2;
}
else if(grado>=81&&grado<=85){
checkedCase=3;
}
else if(grado>=86&&grado<=90){
checkedCase=4;
}
else if(grado>=91&&grado<=96){
checkedCase=5;
}
switch (checkedCase){
case 1: /* iwant to put 0 to 74*/
System.out.println("Failed");
break;
case 2: /* 75-80*/
System.out.println("bellow average");
break;
case 3: /*81-85*/
System.out.println("average");
break;
case 4: /*86-90*/
System.out.println("Good");
break;
case 5: /*91-96*/
System.out.println("VeryGood");
break;
default: System.out.println("Please enter a value in range 0-96");
break;
}
}

My char switching software returns the same answer?

I want to make a program that takes a string and encrypts it.
During execution of program it is supposed to convert a string to char array. Then, a switch statement runs through the array to replace a with b and vice versa.
However, the programm just returns the same as at the start! here is the code
import java.lang.*;
import java.util.Scanner;
public class Program
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
String pw = input.next();
char pwa[] = pw.toCharArray();
for(char c : pwa ){
switch(c){
case 'a':
c = 'b';
break;
case 'b':
c ='a';
break;
}
}
String convpw = new String(pwa);
System.out.println(convpw);
}
}
You're just changing the variable c, not pwa, and c is local to your loop.
You can do this :
for (int i=0; i<pwa.length; i++) {
switch(pwa[i]){
case 'a':
pwa[i] = 'b';
break;
case 'b':
pwa[i] ='a';
break;
}
}

Categories