简体   繁体   中英

How do I ask the user when he/she wants to quit the program

Here's the program that I'm working on but I'm having problems with figuring out how to ask the user if they are done with it by saying a word or number.

import java.util.Scanner;

class paliindrome {
    public static void main(String args[]) {

        String isPalindrome, reverse = "";
        Scanner in = new Scanner(System.in);
        Scanner keyboard = new Scanner(System.in);
        PalindromeChecker aGame = new PalindromeChecker();

        System.out.println(" Please type a word and I'll figure out if it's a palindrome(The program is case sensitive).");
        isPalindrome = in.nextLine();


        int length = isPalindrome.length();

        for ( int i = length - 1; i >= 0; i-- )
            reverse = reverse + isPalindrome.charAt(i);

        if (isPalindrome.equals(reverse))
            System.out.println("The word that you have entered is a palindrome.");
        else
            System.out.println("The word that you have typed isn't a palindrome.");

        char answer;
        do {
            aGame.play ();

            System.out.print("\n Do you want to continue (y or n)?");
            answer = keyboard.next().charAt(0);
        } while (answer == 'y' || answer == 'Y');
    }
}

Put your entire application within the loop. One easy way would be to default answer to y (and you can use Character.toUpperCase(char) to eliminate the or) and something like

PalindromeChecker aGame = new PalindromeChecker();
char answer = 'y';
while (Character.toUpperCase(answer) == 'Y') {
    String isPalindrome, reverse = "";
    Scanner in = new Scanner(System.in);
    Scanner keyboard = new Scanner(System.in);
    // ... The rest of your code ...
    aGame.play();

    System.out.print("\n Do you want to continue (y or n)?");
    answer = keyboard.next().charAt(0);
}

There is one more way to solve your problem: Put your entire code in a infinite while loop like this while(true) , on the last line of loop just before exiting from the loop, u can ask user if he/she wants to continue. If answer is yes, then use the code System.exit(0) to exit from the program or if you want to just break from the loop use break; in the if condition.

You can use below code to ask user for quit.

boolean quit = false;
        Scanner scanner = null;
        try {
            do {
                System.out.println("Do you want to quit? (true/false)\n");
                scanner = new Scanner(System.in);
                String input = scanner.nextLine();
                quit = Boolean.parseBoolean(input);
            } while (!quit);
        } finally {
            scanner.close();
        }
import java.util.Scanner;

public class paliindrome {
    public static void main(String[] args) {

        String isPalindrome, reverse = "";
        Scanner in = new Scanner(System.in);
        Scanner keyboard = new Scanner(System.in);
        PalindromeChecker aGame = new PalindromeChecker();
        for (;;) {// or while(true)
            System.out.println(
                    " Please type a word and I'll figure out if it's a palindrome(The program is case sensitive).");
            isPalindrome = in.nextLine();
            int length = isPalindrome.length();

            for (int i = length - 1; i >= 0; i--) {
                reverse = reverse + isPalindrome.charAt(i);
            }

            if (isPalindrome.equals(reverse))
                System.out.println("The word that you have entered is a palindrome.");
            else
                System.out.println("The word that you have typed isn't a palindrome.");

            System.out.print("\n Do you want to continue (y or n)?");
            char answer = keyboard.next().charAt(0);
            if (answer != 'y' && answer != 'Y') {
                break;
            }else{
                aGame.play ();
            }

        }

    }
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM