我不断在这一行收到错误消息“identifyyer expected”:hk.guess(charToGuess); 我需要做些什么才能纠正这个问题?
我正在创建一个简单的Hangman游戏,它给了我很多麻烦。我是编程新手,我开始对这个项目感到非常沮丧。
import java.util.Random;
import java.util.Scanner;
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Hangman {
public static void main( String[] args ) {
HangmanSession hangmanSession = new HangmanSession();
hangmanSession.play();
}
private HiddenKeyword hiddenKeyword;
private int triesNumber = 7;
hiddenKeyword hk = new HiddenKeyword();
hk.guess(charToGuess);
private void printState() {
System.out.print( "Hidden word : " );
hiddenKeyword.print();
System.out.print( "Tries left: " + triesNumber + "\n<guess letter:>" );
}
public void play() {
boolean bool = true;
while( true ) {
bool = true;
printState();
System.out.println( "Try again, you've already used that letter " + char.class );
bool = false;
}
if( bool ) {
if( hiddenKeyword.guess( char.class ) ) {
System.out.println( "YAY! you have found letter! " + ch );
}
else {
triesNumber--;
}
if( triesNumber < 1 )
gameOver();
if( hiddenKeyword.found() )
congratulations();
}
} //end of bool
public void congratulations() {
System.out.println( "Congratulations " + player + ", you win!" );
System.exit( 0 );
}
public void gameOver() {
System.out.println( "Sorry " + player + ", you lost, but you can try again!" );
System.exit( 0 );
}
private String fValue;
private StringBuffer pValue;
private int lfoundNumber = 0;
public String words() {
Random r = new Random();
int random = r.nextInt(20)+1;
}
public boolean found() {
System.out.println( "Letters found:" + lfoundNumber + "/" + fValue.length() );
return ( lfoundNumber == fValue.length() );
}
public boolean guess( char c ) {
int index = fValue.indexOf( c );
if( index == -1 )
return false;
else {
lfoundNumber = lfoundNumber + findOccurances( c );
return true;
}
}
private int findOccurances( char c ) {
int idx = fValue.indexOf( c );
pValue.setCharAt( idx, fValue.charAt( idx ) );
int counter = 1;
while( idx != -1 ) {
int idxx = fValue.indexOf( c, idx + 1 );
idx = idxx;
if( idx != -1 ) {
counter++;
pValue.setCharAt( idx, fValue.charAt( idx ) );
}
}
return counter;
}
public void print() {
System.out.println( pValue );
}
private char[] lbox = new char[24];
private int counter = 0;
public boolean contains( char c ) {
for( int i = 0; i < counter; i++ ) {
if( lbox[i] == c )
return true;
}
lbox[counter] = c;
counter++;
return false;
}
public void print() {
System.out.print( "\nLetterBox:" );
for( int i = 0; i < counter; i++ ) {
System.out.print( lbox[i] );
}
System.out.println( "" );
}
public static void main(String[] args) {
int words = 20;String wordsString;
switch(random) {
case 1:
return "star";
case 2:
return "hooray";
case 3:
return "dog";
case 4:
return "improvise";
case 5:
return "teacher";
case 6:
return "college";
case 7:
return "seahawks";
case 8:
return "read";
case 9:
return "computer";
case 10:
return "program";
case 11:
return "holiday";
case 12:
return "film";
case 13:
return "music";
case 14:
return "learn";
case 15:
return "library";
case 16:
return "waterfall";
case 17:
return "book";
case 18:
return "semester";
case 19:
return "first";
case 20:
return "vacation";
default:
return"";
}
}
}
}
答案 0 :(得分:1)
请记住,作为Java的经验法则,类可能只包含字段和方法。语句(除了那些将字段初始化为默认值的语句)只能写入方法内。
考虑到上述情况,问题在于您只是在类中调用方法(hk.guess(charToGuess)
),而不是在任何方法内部,并且该语句不是字段初始值设定项。