有关while循环的问题以及如何重新询问是/否问题

时间:2019-02-28 18:22:38

标签: java while-loop

我正在创建将单词(由用户获得)翻译成Pig Latin的代码。除了一件事,我的代码工作得非常好。我希望用户键入“ y”或“ n”,以确定是否运行代码。我正在使用while循环来确定要执行的操作。如果用户键入了上面列出的两个以外的任何内容,我希望它再次询问。目前,我有一个占位符,它愚蠢地调用用户并重新启动代码。我该怎么做?谢谢大家!

public static void main(String[] args) 
{

    Scanner stdIn = new Scanner(System.in);
    String playGame;
    String word;

    // Explains what the program does \\
    System.out.println("Welcome to Coulter's Pig Latin Translator!");
    System.out.println("If you choose to play, you will be asked to type a word which will be translated.");
    System.out.println();

    // Asks the user if they would like to play and checks 'y' or 'n' using a while statement \\
    System.out.println("Would you like to play? [y/n]: ");
    playGame = stdIn.next();

    while(playGame.equals("y") || playGame.equals("n")) // While expression that will check if the user enters a 'y' or 'n'
    {
        if (playGame.equals("y")) // Executes if the user entered 'y'
        {
            System.out.println("Please enter the word that you would like to translate: ");
            word = stdIn.next(); // Receives the word the user wishes to translate

            System.out.println("_______________________________________________________");

            System.out.println();
            System.out.println("You entered the word: " + word); // Displays what the user entered

            System.out.println();
            System.out.println("Translation: " + solve(word)); // Displays the solved word

            System.out.println();
            System.out.println("Thanks for playing!"); // 
            return; // Ends the code

        }

        else if(playGame.contentEquals("n")) // Executes if the user entered 'n'
        {
            System.out.println("That's okay! Come back when you want to.");
            return; // Ends the code
        }   
    }

    System.out.println("_______________________________________________________");
    System.out.println("Don't be silly. Restart and type either 'y' or 'n'"); // Tells the user to restart if they entered anything but 'y' or 'n'

}

// Word translator code using a new static\\
public static String solve (String word) 
{
    String temp = word.toLowerCase();
    char[] vowels = {'a', 'e', 'i', 'o', 'u'}; // Stores vowels in an array
    char first = temp.charAt(0); // Defines first character for later use

    for (int i = 0; i < vowels.length; i++) // Looks for first vowel to replace it
    {
        if (first == vowels[i]) 
        {
            return word + "way"; // Replaces checked vowel
        }
    }

    word = word.substring(1); // Returns the string to the end of the word
    word += first + "ay";


    return word; // Returns the translated word to the program above
}

}

3 个答案:

答案 0 :(得分:1)

好吧,您要在这里输入:

playGame = stdIn.next();

那只需要进入您的循环即可:

playOn = true;
while(playOn) {
  playGame = stdIn.next();
  if ( y ) { ... play 
  } else {
    if ( n ) { ... dont play, set playOn = false
    } else {
      ... ask again
    } 
  }

以上内容仅是作为启发,并指出:在这里真正重要的是,您获得了if / else链,并获得了相应的“块”。您需要在第一个“ else”块中完全包含另一个if / else!

答案 1 :(得分:1)

由于您要重复的代码部分仅是最初的问题,因此所有内容都应在循环中:

boolean askAgain = true;
while(askAgain) {
    // print your prompt;
    playGame = stdIn.next();
    if (playGame.equals("y") || playGame.equals("n")) {
        askAgain = false;
    }
}

// Now do your if/elseif for which letter was retrieved.

优良作法是仅循环遍历实际上可能重复的部分代码。

答案 2 :(得分:0)

您将希望拥有playGame = stdIn.next();行,并提示您在while循环的开始而不是之前运行。然后,您可以检查它是yn还是之后的其他内容。