gpt4 book ai didi

java - 关于 while 循环的问题以及如何重新提出 y/n 问题

转载 作者:行者123 更新时间:2023-11-30 01:54:30 25 4
gpt4 key购买 nike

我正在创建将一个单词(由用户获得)翻译成 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
}

}

最佳答案

好吧,您在这里要求输入:

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 链,以及相应的“ block ”。您需要完全在第一个“else” block 内另一个 if/else!

关于java - 关于 while 循环的问题以及如何重新提出 y/n 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54931958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com