3

我正在尝试创建一个程序,该程序将使用名为“防弹”的程序包从用户那里获取输入(完成,工作正常),但是当我找到匹配项时出现无限循环错误。我有点不知所措,我觉得我不太理解我的代码为什么不起作用。我不是在寻找直接的答案,因为我正在努力学习这一点,但任何帮助都将不胜感激。谢谢,我的代码如下。

--edit-- 我得到了程序的工作。谢谢大家的帮助,你们太棒了。

import bulletproof.*;

public class A26_1 {
public static void main(String[] args) {
    BPScanner kb = new BPScanner();
    String reservedWordToCheck = kb.getStringFromUser("Enter a word to see if it's reserved or enter leave: ");

    System.out.println(ReservedWordChecker(reservedWordToCheck));
}

public static String ReservedWordChecker(String reservedWordToCheck) {
    String[] table = {
            "abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const",
            "continue", "default", "do", "double", "enum", "extends", "final", "finally", "float",
            "for", "goto", "if", "implements", "import", "instanceof", "int", "interface", "long", "native",
            "new", "package", "private", "protected", "public", "return", "short", "static", "strictfp", "super",
            "switch", "synchronized", "this", "throw", "throws", "transient", "try", "void", "volatile", "while"
        };

    while (true) {                      
        if (reservedWordToCheck.equalsIgnoreCase("leave"))
            break;                
        boolean found = false;

        for (int i=0; i < table.length; i++) {
            if (reservedWordToCheck.equalsIgnoreCase(table[i])) {
                found = true;
                break;
            }               
            if (found)
                System.out.println("Yeah, " + reservedWordToCheck + ", is reserved as a Java identifier.");
            else
                System.out.println("Nope, the word " + reservedWordToCheck + ", isn't reserved. Go hog wild.");                      
        }      
        System.out.println("OK BYE");
    } return reservedWordToCheck;
}  

}

4

6 回答 6

3

我认为最简单的方法是 -

public static boolean ReservedWordChecker(String reservedWordToCheck) {
    String[] table = {
            "abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const",
            "continue", "default", "do", "double", "enum", "extends", "final", "finally", "float",
            "for", "goto", "if", "implements", "import", "instanceof", "int", "interface", "long", "native",
            "new", "package", "private", "protected", "public", "return", "short", "static", "strictfp", "super",
            "switch", "synchronized", "this", "throw", "throws", "transient", "try", "void", "volatile", "while"
        };

       for (String data : table) {
            if (reservedWordToCheck.equalsIgnoreCase(data)) {
                return true;
            }
        }
      return false;
}  
于 2012-10-17T06:38:41.453 回答
0

您需要在for循环之外编写以下代码,因为for当您获得匹配时您会中断但您的while循环继续运行

 if (found){
            System.out.println("Yeah, " + reservedWordToCheck + ", is reserved as a Java identifier.");
            break;
 }
 else{
            System.out.println("Nope, the word " + reservedWordToCheck + ", isn't reserved. Go hog wild.");
            break;
 }
于 2012-10-17T06:36:51.593 回答
0

找到匹配项后,您并没有退出 while 循环:-

您应该将您的if-else移出for-loop并添加 break 到您的if,这将破坏您while是否找到匹配项:-

for (int i=0; i < table.length; i++) {
    if (reservedWordToCheck.equalsIgnoreCase(table[i])) {
        found = true;
        break;
    }
} 
if (found) {
     System.out.println("Yeah, " + reservedWordToCheck + 
                        ", is reserved as a Java identifier.");
     break;
} else {
     System.out.println("Nope, the word " + reservedWordToCheck + 
                        ", isn't reserved);
     break;
}

但我不明白,为什么你需要一个while循环。它有点毫无意义。你for-loop足够做你想做的事。

于 2012-10-17T06:37:22.950 回答
0

内循环不打破循环break。因此,while 循环无限继续。forforwhile

查看更改,我已经完成了,它应该可以正常工作。

while (true) 
{
    .......
    .......

for (int i=0; i < table.length; i++) 
{                
    if (reservedWordToCheck.equalsIgnoreCase(table[i])) 
    {                    
        found = true; 
        break; // remove break from here          
    }

    if (found)                    
    {
        System.out.println("Yeah, " + reservedWordToCheck + ", is reserved as a Java identifier.");
        break;
    }                                  
} // end for
if(found)
{
    System.out.println("OK BYE");
}
else                    
{
    System.out.println("Nope, the word " + reservedWordToCheck + ", isn't reserved. Go hog wild.");
}
break;
} // end while

但是,您不需要while(true)构造来检查这一点。只有for意志就足够了。

于 2012-10-17T06:37:25.067 回答
0

那是因为您未标记的 break 仅完成 for 语句,但 while(true) 将继续迭代。

解决方案可以使用如下标记的中断:

  search: 
  while (true) {                      
        if (reservedWordToCheck.equalsIgnoreCase("leave"))           
           break;                
        boolean found = false;

        for (int i=0; i < table.length; i++) {
            if (reservedWordToCheck.equalsIgnoreCase(table[i])) {
                found = true;
                break search;
            }               
            if (found)
                System.out.println("Yeah, " + reservedWordToCheck + ", is reserved as a Java identifier.");
            else
                System.out.println("Nope, the word " + reservedWordToCheck + ", isn't reserved. Go hog wild.");                      
        }      
        System.out.println("OK BYE");
    } return reservedWordToCheck;

欲了解更多信息,请阅读:http ://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html

于 2012-10-17T06:48:22.490 回答
0

你没有在正确的位置打破:

for (int i=0; i < table.length; i++) {
    if (reservedWordToCheck.equalsIgnoreCase(table[i])) {
        found = true;
        break; <--
    }               
    if (found)
        System.out.println("Yeah, " + reservedWordToCheck + ", is reserved as a Java identifier.");
    else
        System.out.println("Nope, the word " + reservedWordToCheck + ", isn't reserved. Go hog wild.");                      
}

当找到这个词时,你跳出 for 循环,除了 . 之外什么都不显示OK BYE

此外,我不明白你为什么在那里放一个无限循环?

像这样的东西会起作用:

public static String ReservedWordChecker(String reservedWordToCheck) {
    String[] table = { ... };

    if (reservedWordToCheck.equalsIgnoreCase("leave")) {
        break;
    }

    boolean found = false;
    for (String entry : table) {
        if (entry.equalsIgnoreCase(reservedWordToCheck)) {
            found = true;
            break;
        }
    }

    if (found) {
        System.out.println("Yeah, " + reservedWordToCheck + ", is reserved as a Java identifier.");
    } else {
        System.out.println("Nope, the word " + reservedWordToCheck + ", isn't reserved. Go hog wild.");                      
    }
    System.out.println("OK BYE");
    return reservedWordToCheck;
}

}

于 2012-10-17T06:42:53.663 回答