JAVA小程序错误。。。。纠结
import java.util.Scanner;import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
public class HangmanCL {
public static final int MAX_TRIES=6;
int letterFill (char,String, String&);
public static void main(String[] args) {
String name;
char letter;
int num_of_wrong_guesses=0;
String word;
String words[] = new String[]{
"Borrowing",
"SPOKESWOMAN",
"swifter",
"mimetic",
"boring",
"MULTIlateral",
"inexpressible",
"learning",
"acidify",
"blue",
"antidisestablishmentarianism",
"upstaged",
"immodestly",
"trader",
"victoriously"
};
//choose and copy a word from array of words randomly
srand(time(null));
int n=rand()% 15;
word=words[n];
char miss[7]={null,null,null,null,null,null,null};
int j=0,i=0;
// Initialize the secret word with the * character.
String unknown(word.length(),'*');
// welcome the user
System.out.print("\n\nWelcome to hangman...");
System.out.print("\n\nYou have " + MAX_TRIES + "tries to try and guess the word");
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
// Loop until the guesses are used up
while (num_of_wrong_guesses < MAX_TRIES)
{
j=0;
System.out.println("Word:"+unknown);
System.out.println("Misses:");
while(miss[j]!=NULL)
{
System.out.print(miss[j]+" ");
j+=1;
}
System.out.println("\nEnter Guess: ");
cin >> letter;
{
// Fill secret word with letter if the guess is correct,
// otherwise increment the number of wrong guesses.
if (letterFill(letter, word, unknown)==0)
{
System.out.println("Whoops! That letter isn't in there!");
num_of_wrong_guesses++;
miss[i]=letter;
i+=1;
}
else
{
System.out.println( "You found a letter! Isn't that exciting!" );
System.out.println();
}
// Tell user how many guesses has left.
System.out.print("You have " +( MAX_TRIES - num_of_wrong_guesses));
System.out.print( " guesses left.");
// Check if user guessed the word.
if (word==unknown)
{
System.out.print( word);
System.out.println();
System.out.print("Yeah! You got it!");
break;
}
}
}
if(num_of_wrong_guesses == MAX_TRIES)
{
System.out.print("\nSorry, you lose...you've been hanged.");
System.out.print( "The word was : "+word);
}
cin.ignore();
cin.get();
return 0;
}
/* Take a one character guess and the secret word, and fill in the
unfinished guess word. Returns number of characters matched.
Also, returns zero if the character is already guessed. */
int letterFill (char guess, String secretword, String &guessword)
{
int i;
int matches=0;
int len=secretword.length();
for (i = 0; i< len; i++)
{
// Did we already match this letter in a previous guess?
if (guess == guessword[i])
return 0;
// Is the guess in the secret word?
if (guess == secretword[i])
{
guessword[i] = guess;
matches++;
}
}
return matches;
} --------------------编程问答--------------------
import java.util.Scanner;--------------------编程问答-------------------- 好,看过了,顶一个 --------------------编程问答-------------------- 不知道怎么办 --------------------编程问答-------------------- 长了一点知识
public class Main {
private static final int MAX_TRIES = 6;
private char []guessWord;
private String secretWord = "";
private int numOfWrongGuesses = 0;
private final String []words = { "Borrowing", "SPOKESWOMAN", "swifter", "mimetic", "boring",
"MULTIlateral", "inexpressible", "learning", "acidify", "blue",
"antidisestablishmentarianism", "upstaged", "immodestly", "trader", "victoriously"};
private char []miss;
public boolean letterFill(char guess){
int len = secretWord.length();
int matches = 0;
for (int i = 0; i< len; i++){
if (guess == guessWord[i])
return false;
if (guess == secretWord.charAt(i)){
guessWord[i] = guess;
matches++;
}
}
return ((matches != 0) ? true : false);
}
Main(){
secretWord = words[(int)(Math.random() * words.length)];
guessWord = new char[secretWord.length()];
for(int i=0; i< secretWord.length(); i++){
guessWord[i] = '*';
}
miss = new char[secretWord.length()+1];
for(int i=0; i<= secretWord.length(); i++){
miss[i] = '\0';
}
}
public static void main(String[] args){
Scanner input = new Scanner(System.in);
Main m = new Main();
char letter;
int j = 0, i =0;
System.out.print("\n\nWelcome to hangman...");
System.out.print("\n\nYou have " + Main.MAX_TRIES + "tries to try and guess the word");
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
while (m.numOfWrongGuesses < Main.MAX_TRIES){
System.out.println("Word:" + new String(m.guessWord));
System.out.print("Misses:");
j = 0;
while(m.miss[j] != '\0'){
System.out.print(m.miss[j++] + " ");
}
System.out.println("\nEnter Guess: ");
letter = input.nextLine().charAt(0);
if (!m.letterFill(letter)){
System.out.println("Whoops! That letter isn't in there!");
m.numOfWrongGuesses++;
m.miss[i++]=letter;
} else {
System.out.println( "You found a letter! Isn't that exciting!\n" );
}
System.out.print("You have " +( Main.MAX_TRIES - m.numOfWrongGuesses));
System.out.print( " guesses left.");
if (m.secretWord.equals(new String(m.guessWord))){
System.out.print(m.secretWord);
System.out.println("\nYeah! You got it!");
break;
}
if(m.numOfWrongGuesses == Main.MAX_TRIES){
System.out.print("\nSorry, you lose...you've been hanged.");
System.out.println( "The word was : " + m.secretWord);
}
}
}
}
补充:Java , Eclipse