我正在制作一个Nim游戏,并且我添加了一个单人游戏模式。为此,我创建了一个方法(不确定这是你所称的方法),并在用户的回合结束时调用它。弹出错误消息说:“变量未解决”。它是我的代码的位置?请帮忙。如何解决变量?
我试着再次创建变量,只是调用变量。
这是迄今为止代码:
import java.util.Scanner;
import java.util.Random;
public class nim {
public static void computerMove() {
Random randA = new Random();
int pcPilePick = randA.nextInt(10);
Object amtInPile;
if (pcPilePick == 1) {
amtInPile = stones1;
}
if (pcPilePick == 2) {
amtInPile = stones2;
}
if (pcPilePick == 3) {
amtInPile = stones3;
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Welcome!");
System.out.println("This is the game of Nim.");
System.out.println("[1]Single player or [2]multiplayer?");
String choice = input.nextLine();
int gameMode = Integer.parseInt(choice);
int stones1 = 9;// These variables will be used
int stones2 = 9;// in order to subtract a number
int stones3 = 9;// of stones from the pile.
int tStones = stones1 + stones2 + stones3;
while (tStones > 0) {
System.out.println("[1] Pile 1: " + stones1); //This stack will display
System.out.println("[2] Pile 2: " + stones2); //the stone count for
System.out.println("[3] Pile 3: " + stones3); //each pile after the first move.
System.out.println("From which pile would you like to take?");
String aMove = input.nextLine();
System.out.println("How many stones would you like to take?");
String bMove = input.nextLine();
int pilePick = Integer.parseInt(aMove);
int amtInPile = 0;
if (pilePick == 1) {
amtInPile = stones1;
}
if (pilePick == 2) {
amtInPile = stones2;
}
if (pilePick == 3) {
amtInPile = stones3;
}
int stonePick = Integer.parseInt(bMove);
/*All the while loops after this comment are
*used in order to prevent stupid answers.
*/
//legal move
//illegal move
if (pilePick > 3 || stonePick > amtInPile) {
System.out.println("But nothing happened!");
System.out.println("That move is invalid");
pilePick = 0;
stonePick = 0; // ensure that for an illegal move, nothing will change.
}
if (pilePick == 1) { //This stack of code will
stones1 = stones1 - stonePick;
}
if (pilePick == 2) { // subtract stones based on the
stones2 = stones2 - stonePick;
}
if (pilePick == 3) { // pile input and stone input.
stones3 = stones3 - stonePick;
}
tStones = stones1 + stones2 + stones3;
if (stones1 + stones2 + stones3 > 0) {
System.out.println("Taking " + stonePick + " stones from stack " + pilePick);
}
if (gameMode == 1) {
computerMove();
}
}
System.out.println("You lose!");
}
}
确切的错误是什么? –
它看起来好像您试图引用仅在您的'computerMove'方法中的'main'方法中定义的变量。将这些变量设置为“private static”并将它们放入你的班级中。 –
什么是确切的错误和*哪里*是确切的错误? –