gpt4 book ai didi

java - 如何在 pig 拉丁语中设置循环?

转载 作者:行者123 更新时间:2023-12-01 10:51:23 25 4
gpt4 key购买 nike

我编写了一个简单的游戏 Pig Latin,一切正常,但我想在用户输入他的单词或短语并且应用程序向他显示答案时设置一个循环,问题应该再次重复

我用 while 写了一个循环

while (true) {
if (input.equals("Exit")){
break;
} else {
// some actions

但是无论我将这段代码放在哪里,答案都会永远循环

我做错了什么?

这是我的代码:

import java.io.IOException;
import java.util.Scanner;

public class piglang {

public static Scanner scanner = new Scanner(System.in);
public static String input, output, line;
public static char firstLetter;
public static String vowels = "aeiouy";
public static boolean capital = false;

public static void main(String[] args) throws IOException{


System.out.println("enter something: ");

line = scanner.nextLine(); // scanner checks and returns the input that was skipped
scanner = new Scanner(line);

while (scanner.hasNext()) { // check for other words in line

input = scanner.next(); // finds and returns next complete token form this scanner

firstLetter = input.charAt(0);

/*first if*/ if ('A' <= firstLetter && firstLetter <= 'Z') { // checking first letter
firstLetter = Character.toLowerCase(firstLetter);
capital = true;
} else
capital = false;

/*second if*/ if (vowels != null) { // checking if first letter is a vowelFGriopt;khtyl mhp
output = (input + "ay");
}
else {
if (capital) { // setting first letter to upperCase
output = "" + Character.toUpperCase(input.charAt(1)); // char to String conversion
output = output + input.substring(2) + firstLetter + "ay";
} else {
output = input.substring(1) + firstLetter + "ay";
}
}
if (line.isEmpty()) {
output.isEmpty();
}

System.out.print(output + " ");
}
}
}

最佳答案

如果您只是将当前主方法中的所有内容放入 while(true) 循环 block 内,则必须进行的一项修改是在打印“enter某物”。由于您重复使用相同的 Scanner 对象来解析该行,因此它不会在下一次迭代中引用 System.in。您还需要检查是否是 line.equals("Exit") 而不是 input.equals("Exit"),因为它实际上是 line code> 代表您的整个输入。我还建议使用 equalsIgnoreCase 而不仅仅是 equals:

while (true) {
scanner = new Scanner(System.in);
System.out.println("enter something: ");

line = scanner.nextLine(); // scanner checks and returns the input that was skipped
if (line.equalsIgnoreCase("Exit")) {
break;
} else {
scanner = new Scanner(line);

while (scanner.hasNext()) { // check for other words in line
input = scanner.next(); // finds and returns next complete token form this scanner

firstLetter = input.charAt(0);

/*first if*/ if ('A' <= firstLetter && firstLetter <= 'Z') { // checking first letter
firstLetter = Character.toLowerCase(firstLetter);
capital = true;
} else {
capital = false;
}

/*second if*/ if (vowels != null) { // checking if first letter is a vowelFGriopt;khtyl mhp
output = (input + "ay");
} else {
if (capital) { // setting first letter to upperCase
output = "" + Character.toUpperCase(input.charAt(1)); // char to String conversion
output = output + input.substring(2) + firstLetter + "ay";
} else {
output = input.substring(1) + firstLetter + "ay";
}
}
if (line.isEmpty()) {
output.isEmpty();
}

System.out.print(output + " ");
}
}

}

关于java - 如何在 pig 拉丁语中设置循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33864642/

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