繁体   English   中英

如何让用户在程序的最后部分输入是否要继续?

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

我正在制作一个随机代码只是为了帮助我学习从罗马字写日语。 我一直在寻找如何询问用户是否要继续练习的解决方案。

我是编码的初学者,所以如果有关于我应该和不应该在我的代码上做什么以及如何使我的代码更高效的任何提示,这对我来说意义重大。 提前感谢您的帮助:D

编辑:我为自己做了这个,因为我找不到可以跟踪随机单词历史的单词随机生成器,因为我需要它来检查我是否正确地写了字符。 我只是问这个,因为我想制作这个代码,就好像其他人正在使用它一样。

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();
    
}

你可以用 do-while 循环交换你的 while。 在程序结束时,当您询问用户是否要继续时,程序应该接受用户的回答,然后在 do-while 循环条件中使用它。

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

你可以试试这样的

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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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