简体   繁体   中英

How to ask the user to input whether they want to continue or not in the last part of a program?

I am making a random code just to help me on learning to write japanese from romaji. I am stuck on finding a solution on how to ask the user whether they want to continue practicing or not.

I am a beginner on coding so if there's any tips on what I should and should not do on my codes and ways to make my code more efficient, it would mean a lot to me. Thanks for the help in advance :D

edit : I made this for myself because I couldn't find a word randomizer that would track the history of the random words because I need it to check if I wrote the characters correctly or not. I am just asking this because I wanted to make this code as if someone else is using it.

public static void main(String[] args) 
{
    String character;
    String cont;
    int quantity;
    int loops = 0;
    int lines;
    int terminate = 0;
    
    Scanner scanner = new Scanner(System.in);
    
    while (terminate == 0) 
    {
        System.out.print("What do you want to practice? (K/H) ");
        character = scanner.nextLine();
        
        while(!character.equals("K") && !character.equals("H")) 
        {
            System.out.println("\nInput is invalid");
            System.out.print("What do you want to practice? (only type K or H) ");
            character = scanner.nextLine();
        }
        
        
        System.out.print("\nHow many lines do you want to generate? ");
        lines = scanner.nextInt();
        quantity = lines * 11;
        System.out.println("");
        
        String[] hiragana ={"a", "i", "u", "e", "o", "n", 
                "ka", "ki", "ku", "ke", "ko", "ga", "gi", "gu", "ge", "go",
                "sa", "shi", "su", "se", "so", "za", "ji", "zu", "ze", "zo",
                "ta", "chi", "tsu", "te", "to", "da", "dji", "dzu", "de", "do",
                "na", "ni", "nu", "ne", "no",
                "ha", "hi", "fu", "he", "ho", "ba", "bi", "bu", "be", "bo", "pa", "pi", "pu", "pe", "po",
                "ma", "mi", "mu", "me", "me",
                "ya", "yu", "yo",
                "ra", "ri", "ru", "re", "ro",
                "wa", "wo"};
        String[] katakana = {"a", "i", "e", "o", "u"};
        
        Random r=new Random();
        
        if (character.equals("H")) 
        {
            while (quantity > 0) 
            {
                if (quantity > 1) 
                {
                    loops++;
                    int randomNumber=r.nextInt(hiragana.length);
                    System.out.print(hiragana[randomNumber] + ", ");
                    quantity = quantity - 1;
                    if (loops > 10) 
                    {
                        loops = 0;
                        System.out.println("\n");
                    }
                }
                else if (quantity > 0)
                {
                    loops++;
                    int randomNumber=r.nextInt(hiragana.length);
                    System.out.print(hiragana[randomNumber]);
                    quantity = quantity - 1;
                    if (loops > 10) 
                    {
                        loops = 0;
                        System.out.println("\n");
                    }
                }
            }
        }
        else if (character.equals("K")) 
        {
            while (quantity > 0) 
            {
                if (quantity > 1) 
                {
                    loops++;
                    int randomNumber=r.nextInt(katakana.length);
                    System.out.print(katakana[randomNumber] + ", ");
                    quantity = quantity - 1;
                    if (loops > 10) 
                    {
                        loops = 0;
                        System.out.println("\n");
                    }
                }
                else if (quantity > 0)
                {
                    loops++;
                    int randomNumber=r.nextInt(katakana.length);
                    System.out.print(katakana[randomNumber]);
                    quantity = quantity - 1;
                    if (loops > 10) 
                    {
                        loops = 0;
                        System.out.println("\n");
                    }
                }
            }
        }
        
        System.out.print("Do you want to continue practicing? (Y/N) ");
        cont = scanner.nextLine();
        
        if (cont.equals("Y")) 
        {
            terminate = 0;
            System.out.print("test");
        }
        else if (cont.equals("N")) 
        {
            terminate = 1;
            System.out.print("Have a nice day! :D");
        }
    }
        
    scanner.close();
    
}

You can swap your while with a do-while loop. In the end of your program when you are asking the user if he wants to continue the program should take the user's answer and then use it in the do-while loop condition.

do{ \\\\ DO STUFF HERE }while(con.equals("Y"))

You can try something like this

boolean isContinue = true;
Scanner in = new Scanner(System.in);

do{
    System.out.print("Do you want to continue practicing? (Y/N)");
    String ans = in.nextLine();

    switch(ans) 
    {
        case "Y" -> {//Put your code here}
        case "N" -> 
             {
               isContinue = false;
               System.out.println("Bye");
             }
while(isContinue);

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