嗨,我正在编写Nim游戏,
我的大部分代码都已完成,但我遇到了两个主要问题。
我找不到我的错误
我似乎无法让计算机轮流使用
先谢谢,这是我的代码。
import java.util.*;
import java.util.Random;
public class Nim {
public static void main(String[] args) {
int banana = 0;
Random r = new Random();
intro();
banana = r.nextInt(16);
int numStones = (15 + banana);
yorner(numStones);
//kbinput.nextInt();
}
public static void intro () {
System.out.println("Welcome to the game of Nim!");
System.out.println("The Rules of the game are as follows: \n");
System.out.println("1. There are two players in this game; you and the computer.");
System.out.println("2. The game starts with a random number stones ranging from 15 to 30 stones.");
System.out.println("3. Every turn each player takes anywhere between 1 to 3 stones");
System.out.println("4. The player who takes the last stone loses. \n");
System.out.println("Would you like to start the game now? \nPlease enter 'Y' for yes and 'N' for no:");
}
public static void yorner (int numStones){
System.out.println("This game of nim will start with " + numStones + " stones.\n");
Scanner kbinput = new Scanner (System.in);
boolean vInput = false;
do
{
String yorn = kbinput.nextLine();
char input = yorn.charAt(0);
switch(input) {
case 'Y':
case 'y':
vInput = true;
yes(numStones);
break;
case 'N':
case 'n':
System.out.println("Thank you for your time.");
vInput = true;
break;
default:
System.out.println("Please only enter 'Y' for yes and 'N' for no, other entries will not be tolerated.");
}
}
while((vInput == false));
}
public static void yes (int numStones){
System.out.println("You selected 'Yes', thank you for choosing to play the game of Nim.\n");
System.out.println("It is your turn first.");
System.out.println("How many stones would you like to take? \n");
System.out.println("Enter a number from 1 to 3");
player(numStones);
}
public static int player(int numStones){
Scanner kbinput = new Scanner (System.in);
int numTake = kbinput.nextInt();
int numStone = 0;
boolean apple = false;
loop: while ( apple == false){
switch(numTake){
case 1:
apple = true;
numStone = numStones - numTake;
System.out.println("There are " + numStone + " stones left");
break;
case 2:
apple = true;
numStone = numStones - numTake;
System.out.println("There are " + numStone + " stones left");
break;
case 3:
apple = true;
numStone = numStones - numTake;
System.out.println("There are " + numStone + " stones left");
break;
default:
System.out.println("You can only takes anywhere between 1 and 3 stones from the pile");
}
}
return numStone;
}
public static boolean compWin (int numStone){
return false;
}
public static void computerTurn(int numStone1, int numStone) {
Random rn = null;
int compTake = rn.nextInt(3);
switch(compTake){
case 1:
System.out.println("Computer takes 1 stone.");
numStone1 = numStone - compTake;
System.out.println("There are " + numStone + " stones left");
break;
case 2:
System.out.println("Computer takes 2 stones");
numStone1 = numStone - compTake;
System.out.println("There are " + numStone + " stones left");
break;
case 3:
System.out.println("Computer takes 3 stones");
numStone1 = numStone - compTake;
System.out.println("There are " + numStone + " stones left");
break;
}
}
}
我得到的错误是这些
线程“main”中的异常java.lang.Error:未解决的编译问题: 语法错误,插入“;”完成LocalVariableDeclarationStatement 语法错误,插入“;”完成声明
答案 0 :(得分:1)
我无法重现您的编译错误。
然而,你没有游戏循环(也就是说,一个循环可以让游戏保持有效但仍然有效的动作)并且你永远不会调用computerTurn