(JAVA)以下代码无法调试:发生了哪些错误以及如何修复这些错误?
我对以下代码有问题。场景是这样的:用户输入一个字符串(可以是一个句子)。对于每个单词,如果以元音开头,代码将在末尾打印完整的单词+“ay”,并将单词的第一个字母放在单词的末尾(然后在末尾添加“ay”)。如果单词的开头不是字母,就打印出来。但是,它应该通过输入大写正确的“是”或“否”来询问用户是否希望继续 我试图调试它,但没有按预期工作。您将看到while(scanWord.hasNext())中的所有内容都正常工作(以及之前的所有内容)。但是,在该循环之后,其余部分将被执行器忽略(将被这样注释)。你能帮我找到答案,解释错误,从而让别人和我成为更好的java开发人员吗(JAVA)以下代码无法调试:发生了哪些错误以及如何修复这些错误?,java,while-loop,java.util.scanner,Java,While Loop,Java.util.scanner,我对以下代码有问题。场景是这样的:用户输入一个字符串(可以是一个句子)。对于每个单词,如果以元音开头,代码将在末尾打印完整的单词+“ay”,并将单词的第一个字母放在单词的末尾(然后在末尾添加“ay”)。如果单词的开头不是字母,就打印出来。但是,它应该通过输入大写正确的“是”或“否”来询问用户是否希望继续 我试图调试它,但没有按预期工作。您将看到while(scanWord.hasNext())中的所有内容都正常工作(以及之前的所有内容)。但是,在该循环之后,其余部分将被执行器忽略(将被这样注释)
//import Scanner
import java.util.Scanner;
public class Prog508a {
public static void main(String [] args) {
//Declare variables
String word;
String response;
boolean answer = true;
//Declare Scanner objects
Scanner scanWord = new Scanner(System.in);
Scanner answerQuestion = new Scanner(System.in);
//while user still wants to continue
while (answer == true) {
//ask user for input
System.out.print("Enter a sentence: ");
//while the input still contains words
while (scanWord.hasNext()) {
//input from user is word
word = scanWord.next();
//if the character is letter at 0
if (Character.isLetter(word.charAt(0))) {
//and if the character is not a vowel at index 0
if (!(word.charAt(0) == 'a')
&& !(word.charAt(0) == 'e')
&& !(word.charAt(0) == 'i')
&& !(word.charAt(0) == 'o')
&& !(word.charAt(0) == 'u')) {
//the output is added the substring of word from the second letter and added first letter at the end
word = word.substring(1,word.length())+ word.charAt(0);
}
//word is added "ay"
word+= "ay";
}
//print the word result
System.out.print(word + " ");
}
//EVERYTHING UNDER HERE DOES NOT EXECUTE
System.out.println("\n");
System.out.print("Do you wish to convert another sentence (Yes or No): ");
response = answerQuestion.next();
while (!(response.compareTo("No") == 1) || !(response.compareTo("Yes") == 1)) {
//if the answer is no or yes (no capitalization)
if (response.compareTo("no") == 1 || response.compareTo("yes") == 1) {
while (response.compareTo("no") == 1 || response.compareTo("yes") == 1) {
System.out.println("Capitalization is important! Input correctly: ");
response = answerQuestion.next();
}
} else if (!(response.compareTo("No") == 1) || !(response.compareTo("No") == 1)) {
while (!(response.compareTo("No") == 1)
|| !(response.compareTo("Yes") == 1)
|| !(response.compareTo("no") == 1)
|| !(response.compareTo("yes") == 1)) {
System.out.println("You must input either yes or no! Input correctly: ");
response = answerQuestion.next();
}
}
}
if (response.compareTo("No") == 1)
answer = false;
if (response.compareTo("Yes") == 1)
answer = true;
}
//close the scanners
answerQuestion.close();
scanWord.close();
}
}
//此处的所有内容都不会执行
你有一个无限的while循环
//while the input still contains words
while (scanWord.hasNext()) {
所以,你需要以某种方式打破这种局面。例如,不要使用while循环,一次输入一个完整的句子
//ask user for input
System.out.print("Enter a sentence: ");
String sentence = scanWord.nextLine();
for (String word : sentence.split("\\s+")) {
// sentence is split on spaces
}
当对象相等时,
compareTo
的实现返回0
,当一个对象大于另一个对象时返回1
使用==0
或者只使用if(response.equals(“Yes”){}
为什么资本化很重要?如果你根本不在乎这个案子,就用
if (response.equalsIgnoreCase("yes")) { }
此外,请勿关闭回路内的扫描仪。你只需要一个
如果关闭一个扫描仪,它将关闭备份流,因此实际上您正在同时关闭这两个扫描仪 您需要在两个位置进行校正:
- 首先,您需要将
更改为while(scanWord.hasNext()){
,因为您希望控件在解析句子后打印if(scanWord.hasNext()){
问题Yes/No
- 其次,您需要更正字符串比较,您需要将
更改为response.compareTo(“no”)==1
,或者更好地使用response.equals(“no”)
进行不区分大小写的比较response.equalsIgnoreCase(“no”)
compareTo
是如何工作的,它在第一次转换(第一句话)时工作良好,但不要求继续…当response=“No”
,然后!(response.compareTo(“No”)=1)
是真的,我想好了-谢谢!但是问题没有解决。我做了你建议我做的所有更改,但是它仍然没有编译//下面的所有内容都没有执行wait-没有编译,或者没有运行?如果有编译错误,那么你应该提到这是什么,你怎么能从中摆脱呢他在while循环中不使用break语句?我尽量避免在while循环中使用break语句。对不起,我的意思是运行…它可以编译,这里有一个输出:输入一句话:谢谢!hankTay ou!yay hello ellohayWell,将while更改为条件语句不会使我的代码按预期工作。该循环应该计算所有不同的在输入句子中插入单词。这是一个无限循环。它在处理完第一个句子后等待其他句子,而理想情况下,它应该询问用户是否想尝试另一个句子。在这里发布答案之前,我对其进行了调试。