2013-04-11 63 views
0

我目前正在尝试编写一个基于文本的hang子手。如果你选择猜测一封信,程序会在执行第53行后突然结束。请告诉我为什么会发生这种情况,因为我无法弄清楚。Java程序突然在NetBeans中结束

这是我的一些Java代码:

package hangman; 

import java.util.Random; 
import java.util.Scanner; 
import java.lang.String; 

public class Hangman { 

    public static void main(String[] args) { 
     // TODO code application logic here 
     Scanner chez = new Scanner(System.in); 
     Scanner waffle = new Scanner(System.in); 
     Random randomGenerator = new Random(); 
     StringBuilder displayWord = new StringBuilder("______________________________"); 
     String theWord = null; 
     String preLetter; 
     //String sbDisplayWord; 
     char letter = 0; 
     int randomNumber; 
     int lengthOfWord; 
     int numberOfLetterInWord = 0; 
     int gDisplay; 
     int guessWordNumber = 0; 
     String guessWord; 
     RandomWord troll = new RandomWord();  
     randomNumber = randomGenerator.nextInt(12); 
     //Fill var with the word. 
     theWord = troll.wordDecide(randomNumber); 

     System.out.println ("Welcome to Hangman!"); 

     lengthOfWord=theWord.length(); 
     System.out.println("This word has " + lengthOfWord + " letters."); 

     System.out.println("You have 20 guesses."); 

     for (int g =19; g >= 0; g--) {  

      System.out.println("If you want to guess the word, type 0. If you want to guess a letter, type 1."); 
      guessWordNumber=chez.nextInt(); 

      if (guessWordNumber==0) { 
       System.out.println("Enter the word now. Remember, don't capitalize it."); 
       guessWord=waffle.nextLine(); 
       if (guessWord.equals(theWord)) { 
        System.out.println("YOU WIN"); 
        System.exit(0); 
       } else { 
        System.out.println("Sorry, this wasn't the correct word."); 
       } 
      } else if (guessWordNumber==1) { 

       System.out.println("Please enter the letter you wish to guess with."); 
       //System.out.println("It will tell you if you have guessed right for any of the letters. If it is blank, that means none of the letters match."); 
       preLetter=chez.nextLine(); 

       if (preLetter.length()>0) { 
        letter=preLetter.charAt(0); 
       } else { 
        System.exit(0); 
       } 


       System.out.println(""); 
       for(int i = 0; i <= lengthOfWord -1; i++) {  //-Eshan 

        if (letter == theWord.charAt(i)) { 

         numberOfLetterInWord=i+1; 
         System.out.println("This letter matches with letter number " + numberOfLetterInWord + " in the word."); 
         displayWord.setCharAt(i, letter); 
        } else { 

         numberOfLetterInWord=i+1; 
         System.out.println("This letter doesn't match with letter number " + numberOfLetterInWord + " in the word."); 

        }  


       } 

       System.out.println(""); 
       System.out.println("The word so far is " + displayWord); 
       System.out.println(""); 
       gDisplay = g + 1; 
       System.out.println("You have " + gDisplay + " guesses left."); 

      } else { 
       System.exit(0); 
      } 

     } 

     System.out.println("GAME OVER"); 
     System.exit(0); 

    } 
} 

这是我的Java代码的另一部分:

package hangman; 

public class RandomWord { 
    private static String[] wordArray = { 
     "psychology", 
     "keratin", 
     "nostalgia", 
     "pyromaniac", 
     "chlorophyl", 
     "derivative", 
     "unitard", 
     "pterodactyl", 
     "xylophone", 
     "excommunicate", 
     "obituary", 
     "infinitesimal", 
     "concupiscent", 
    }; 

    public String wordDecide(int randomNumber) { 
     String theWord; 
     theWord = wordArray[randomNumber]; 
     return theWord; 
    } 

} 
+0

上码偶联的评述。 RandomWord不应该需要随机数参数。单词数组的大小仅在“RandomWord”内部是已知的,所以随机数字应该在'RandomWord'内部生成,如'randomNumber = Random.nextInt(wordArray.size)'中所示,并且不作为参数传递。 – 2013-04-11 03:03:07

回答

0

因为Scanner#nextInt不消耗换行符,此行

guessWordNumber = chez.nextInt(); 

传递一个换行符到

preLetter = chez.nextLine(); 

Scanner现在不阻止,因为它已经收到输入。 StringpreLetter现在是空的,因此导致程序退出。

尝试使用Scanner#nextLine消耗换行符:

guessWordNumber = Integer.parseInt(chez.nextLine());