Java Scanner.next()或方法调用

Java Scanner.next()或方法调用,java,Java,我正在写一个程序,它接受输入并搜索单词列表中的任何字谜。为此,它调用一个void方法search,该方法打印出10个字谜。下面是给我带来麻烦的代码片段: public static void main(String[] args) throws Exception { Scanner input = new Scanner(System.in); System.out.println("Please enter a word: "); String wo

我正在写一个程序,它接受输入并搜索单词列表中的任何字谜。为此,它调用一个void方法
search
,该方法打印出10个字谜。下面是给我带来麻烦的代码片段:

public static void main(String[] args) throws Exception
   {
      Scanner input = new Scanner(System.in);
      System.out.println("Please enter a word: ");
      String word = input.next();
      System.out.println(word);
      search(word);
   } 
它总是打印出您输入的任何单词,但随后它就停止了。当我在jGRASP中使用调试器时,我注意到它甚至没有将任何值保存到
word
!它也从不调用
search
类。代码冻结了——它说它仍在运行,但它什么也不做。但是,如果我让用户在单词后输入一个整数值,如下所示:

Scanner input = new Scanner(System.in);
System.out.println("Please enter a word: ");
String word = input.next();
System.out.println(word);
System.out.println("Enter a number: ");
int example = input.nextInt();
search(word);
这会将一个值保存到
word
,但不会保存到
example
,代码会像以前一样冻结

编辑:这里是
搜索
方法。words.txt是同一文件夹中的单词列表

public static void search(String input) throws Exception
   {
      Scanner words = new Scanner(new File("words.txt"));
      char[] alphabet = new char[] {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
      int[] inputLetters = new int[25];
      int[] wordLetters = new int[25];
      String[] anagrams = new String[10];
      int entries = 0;
      for(int a = 0; a < input.length(); a++)
      {
         for(int b = 0; b <= 25; b++)
         {
            if(input.charAt(a) == alphabet[b])
            {
               inputLetters[b]++;
            }
         }
      }
      while(entries < anagrams.length)
      {
         while(words.hasNext())
         {
            String comparison = words.next();
            for(int a = 0; a < comparison.length(); a++)
            {
               for(int b = 0; b < 25; b++)
               {
                  if(comparison.charAt(a) == alphabet[b])
                  {
                     wordLetters[b]++;
                  }
               }
            }
            if(inputLetters == wordLetters)
            {
               anagrams[entries] = comparison;
               entries++;
            }
         }
      }
     System.out.println(Arrays.toString(anagrams));
   }
公共静态无效搜索(字符串输入)引发异常
{
扫描器单词=新扫描器(新文件(“words.txt”);
字符[]字母表=新字符[]{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
int[]inputLetters=新int[25];
int[]wordLetters=新int[25];
字符串[]字谜=新字符串[10];
整数项=0;
对于(int a=0;a实际上,对于(intb=0;b来说,这就像白天一样清晰。您不需要调试器来查找无休止的循环

以下是关键的代码位:

  int[] inputLetters = new int[25];
  int[] wordLetters = new int[25];

  while(entries < anagrams.length)
  {
        ....

        if(inputLetters == wordLetters)
        {
           ......
           entries++;
        }
     }
  }
int[]inputLetters=newint[25];
int[]wordLetters=新int[25];
while(条目
分析:

  • 只要
    条目
    (最初为0)小于
    字谜,就可以循环
  • 如果
    inputLetters==wordLetters
    ,则增加
    条目的数量
  • 但是很明显,
    inputLetters
    引用永远不能等于
    wordLetters
    引用
  • 因此,您无法增加
    条目
  • 因此你有一个无止境的循环

  • 怎么可能?如果您输入的单词被打印出来,它应该存储在
    word
    中。显示搜索的作用。我打赌单词和示例不会被存储,因为它们在编译器的优化路径中被剥离。示例应该是确定的,因为没有引用它。是否
    search
    以无限循环结束?是否命中输入单词后输入?您的search()方法定义在哪里?我只是复制了您的代码(主方法),没有search method,它只是打印出我键入的单词,然后结束(它不会冻结)