Java: How to Ask User If He/She Wants to Continue Program

Java - Do you want to continue?(Y/N)

String c = "";
do{
System.out.println("How much money do you want to have? ");
double money = input.nextDouble();

System.out.println("Ok, here is yours $" + money);

System.out.println("Do you want to continue y or n");
c = input.nextLine();

}while(c.equalsIgnoreCase("Y"));

How to ask user if he/she wants to quit the program and print out the thank you message in Java

In the code you've posted, your loop is being controlled by the condition !input.equalsIgnoreCase("quit"). That is, if input is "quit", the loop is terminated.

But the following block is executed only if input is "quit":

if (input.equals("quit"))
{
// See if user wants to continue
System.out.println("Do you wish to leave the Underground (Y/N)? >");
choice = scan.nextLine();
System.out.println();
}

So if this block is executed, !input.equalsIgnoreCase("quit") evaluates to false and the loop is terminated. And that's not what you want.

Now that you know what's wrong, fixing it is easy. Check the value of choice in the above if block: if choice is not yes, don't quit i.e. reset input to a default value.

I've pasted the working code here on pastebin.

enter image description here

Java: Ask user if they want to play again?

your whole logic is within the main()-method. I suggest you move the while{}-part into a new method like play().

In your main() implement a new while() with prompting your message and reading the input from the keyboard:

public static void main(String args[]) {
Scanner inputScanner = new Scanner(System.in);
String userInput = "";
do {
play();
System.out.print("Do you want to play again ([Y] / n)? ");
userInput = inputScanner.nextLine();
System.out.println(userInput);
} while (userInput.equals("Y") || userInput.equals(""));
}

HTH, SiS

I have a program where I need to user will be asked if they want to continue and add to the sum each time

The OP's problem is this. It is located below the code. The question needs formatting since the OP's problem description is included in the code snippet.

Need to be able to repeat the questions add to get that total of 55.

String userInput = null;
int term = 0;
int lastTerm = 0;
int sum = 0;
System.out.println("It is a simple series sum problem. Prepared by -------");
System.out.println("Enter first term between 1 to 100:");
term = input.nextInt();
while (term < 0 || term > 100) {
System.err.print("Error: Enter the first term between 1 to 100");
term = input.nextInt();


}
System.out.println("Enter last term:");
lastTerm = input.nextInt();
while (lastTerm <= term) {
System.err.print("Error: Enter an integer greater than " + term);
lastTerm = input.nextInt();
}
// Edit start
for (int i = term; i<=lastTerm; i++) {
sum += i;
}
System.out.println("Sum:" + sum);
// Edit end

Put the above code inside this while loop.

        while (true) {
// Place it here
System.out.println("Do you like to do again");
Boolean willContiue = false;
while (true){
userInput = input.next();
if (userInput.equalsIgnoreCase("Y"));
willContiue = true;
break;
else if (userInput.equalsIgnoreCase("N"))
{
break;
}
}
if (!willContinue) {
break;
}
}

By doing the above modifications you will now get the user to input his choice again. Since in your code you are just asking the user if he/she would like to do it again, and if the user answers "Y" your code does not do anything. That is why the question does not repeat.

How to continue loop based on user input in java program

You can change your code like this:

System.out.println("Enter yes if you would like to cash out and use remaining balance to play and no if you would like to stop");
String decision = input.next();
if ("Yes".equalsIgnoreCase(decision)) { // Cashing Out
budget = budget - 5000;
System.out.println("Budget After play " + budget);
continue;
}
break; // breaking the loop (if user enters anything other than yes)

Here I have used String (to store User's response as a word). If he enters Yes, yes or YES (ignoring the case, it will be considered that user wants to cash out) the loop will not break.



Related Topics



Leave a reply



Submit