如何解析一个变量?
How to resolve a variable?
我正在制作 Nim 游戏,我正在添加单人游戏模式。为此,我创建了一个方法(不确定这是否是您所说的),并在用户轮到结束时调用它。弹出一条错误消息说: "variable not resolved." 是我代码的位置吗?请帮忙。
我尝试再次创建变量,然后调用变量。
目前的代码如下:
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!");
}
}
在方法 computeMove
中第 8->16 行的 if 语句中,您引用了未定义的变量 stones1, stones2, stones3
。您需要声明这些变量以引用它们,或者将它们作为参数传递(这可能是您想要的)。
public static void computerMove(int stones1, int stones2, int stones3){
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;
}
}
并将第 87 行的函数调用更改为:
computerMove(stones1, stones2, stones3);
变量stones1
、stones2
和stones3
仅在main
方法中有上下文,它们在computerMove
方法中未定义
您可以将它们定义为 static
class 变量,但这是坏习惯滑坡的开始,相反,修改 computerMove
方法以接受它需要的信息
public static void computerMove(int stone1, int stone2, int stone3) {
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;
}
}
现在调用 computeMove
做了一些事情,但它没有超出方法本身的上下文,因此您可能需要 return 值
public static int computerMove(int stone1, int stone2, int stone3) {
//...
return amtInPile;
}
然后您可以在调用它的同一上下文中对其进行维护...
int computerPile = 0;
//...
if (gameMode == 1) {
computerPile = computerMove(stones1, stones2, stones3);
}
我正在制作 Nim 游戏,我正在添加单人游戏模式。为此,我创建了一个方法(不确定这是否是您所说的),并在用户轮到结束时调用它。弹出一条错误消息说: "variable not resolved." 是我代码的位置吗?请帮忙。
我尝试再次创建变量,然后调用变量。
目前的代码如下:
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!");
}
}
在方法 computeMove
中第 8->16 行的 if 语句中,您引用了未定义的变量 stones1, stones2, stones3
。您需要声明这些变量以引用它们,或者将它们作为参数传递(这可能是您想要的)。
public static void computerMove(int stones1, int stones2, int stones3){
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;
}
}
并将第 87 行的函数调用更改为:
computerMove(stones1, stones2, stones3);
变量stones1
、stones2
和stones3
仅在main
方法中有上下文,它们在computerMove
方法中未定义
您可以将它们定义为 static
class 变量,但这是坏习惯滑坡的开始,相反,修改 computerMove
方法以接受它需要的信息
public static void computerMove(int stone1, int stone2, int stone3) {
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;
}
}
现在调用 computeMove
做了一些事情,但它没有超出方法本身的上下文,因此您可能需要 return 值
public static int computerMove(int stone1, int stone2, int stone3) {
//...
return amtInPile;
}
然后您可以在调用它的同一上下文中对其进行维护...
int computerPile = 0;
//...
if (gameMode == 1) {
computerPile = computerMove(stones1, stones2, stones3);
}