所以我试图在java中调用一个方法displayBoard,一旦用户在main方法中输入一个数字,就会显示boardgame。我无法调用此方法。有谁知道我哪里出错了或者我该如何解决这个问题?感谢。
public static void displayBoard(int [] board, boolean showItem)
{
System.out.println();
System.out.print("_");
for (int val : board){
switch(codes){
case 2:
System.out.print("x");
break;
case 3:
System.out.print(" ");
break;
case 4:
System.out.print(showItem ? "Y" : " ");
break;
}
System.out.print("_");
} //for
System.out.println();
System.out.println();
}//display
public static void main (String [] args)
{
int guess = 0;
int userInput = promptForInt("Length Of board");
int [] numbers = new int[userInput];
int randomlocs = new Random().nextInt(userInput);
int val;
int display = displayBoard(board [], boolean showItem) // doesnt work?
boolean showItem = false;
while(! showItem)
{
val = promptForInt("Try Again!");
if(guess == randomlocation)
{
System.out.println("Found it!");
showItem = true;
}
else if(guess != randomlocs)
System.out.print(val);
}
}
答案 0 :(得分:1)
<强>问题强>
您必须将值传递给方法调用。现在,您正在将声明传递给该方法,这不是正确的Java语法
如何修复
首先,在调用方法之前声明您的showItem
布尔值,以便有boolean
传递给方法。它应该是这样的:
boolean showItem = false;
int display = displayBoard(numbers, showItem)
这将传递存储在numbers
和showItem
变量中的vakues。我们知道由于方法的参数名称,应该传入存储在这些特定变量(numbers
和showItem
)中的值。
导致该方法调用的语句应如下所示:
int userInput = promptForInt("Length Of board");
int [] numbers = new int[userInput];
boolean showItem = false;
int display = displayBoard(board [], boolean showItem);
int randomlocs = new Random().nextInt(userInput); //since this isn't used before the method call, it should be declared below it
int guess = 0; //same with this
int val; //and this