[英]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.