(JAVA)以下代码无法调试:执行了哪些错误以及如何修复这些错误?

时间:2016-12-09 23:22:29

标签: java while-loop java.util.scanner

我遇到以下代码问题。场景如下:用户输入一个字符串(可以是一个句子)。对于每个单词,如果以元音开头,则代码将在末尾打印完整的单词+“ay”,并将单词的第一个字母放在单词的末尾(然后在末尾添加“ay” )。如果它不是一个开始单词的字母,那么只需打印它。但是,它应该通过输入“是”或“否”来确认用户是否想要继续,并使用正确的大小写。

我尝试调试它,并且无法按预期工作。您将看到while(scanWord.hasNext())中的所有内容(以及之前的所有内容)都可以使用。但是,在该循环之后,执行者将忽略其余部分(将被注释为这样)。你能帮助我得到一个答案,解释错误,从而让其他人和我自己更好的java开发人员吗?

//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();
    }
}

2 个答案:

答案 0 :(得分:0)

您需要在两个地方进行更正:

  • 首先,您需要将while (scanWord.hasNext()) {更改为if (scanWord.hasNext()) {,因为您希望控件在解析完句子后打印Yes/No问题。

  • 其次,您需要更正字符串比较,您需要使用response.compareTo("no") == 1response.equals("No")更改为response.equalsIgnoreCase("No"),甚至更好,以进行不区分大小写的比较。

答案 1 :(得分:-1)

  

//EVERYTHING UNDER HERE DOES NOT EXECUTE

你有一个无限的while循环。

//while the input still contains words
while (scanWord.hasNext()) {

所以,你需要break以某种方式。例如,不要使用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
}

Just be careful using next() and nextLine() together.

当对象相等时,compareTo的实现会返回0,而当一个对象大于时,1会执行== 0

使用if (response.equals("Yes")) { }或使用if (response.equalsIgnoreCase("yes")) { }

为什么资本化很重要?如果您甚至不关心案例,请使用

import multiprocessing
import time

class Counter(object):
  def __init__(self):
    self.value = 0

  def update(self):
    self.value += 1

def job(Counter):
  while True:
    counter.update()

if __name__ == '__main__':
  counter = Counter()
  p = multiprocessing.Process(target=job,args=(counter,)
  p.start()
  time.sleep(10)
  # I want to check the value of the counter object here
  p.terminate()

另外,请勿关闭循环内的扫描仪。而你只需要一个。

如果您关闭一台扫描仪,它会关闭后备流,因此您实际上正在关闭两者。