嗨,我正在編程一個Nim遊戲,Nim Game Java錯誤?
我已經完成了大部分代碼,但我遇到了兩個主要問題。
我找不到我的錯誤
我似乎無法讓電腦纔會輪到
在此先感謝,這裏是我的代碼。
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;
}
}
}
,我得到的錯誤是在線程「主要」 java.lang.Error的這些
異常:未解決的編譯問題: 語法錯誤,插入「;」完成LocalVariableDeclarationStatement 語法錯誤,插入「;」完成聲明
'隨機RN = NULL; int compTake = rn.nextInt(3);' 我敢肯定,這總是會導致'NullPointerException'? – tredontho
我該如何解決這個問題? – user3417442
類似於你在'main'方法中使用隨機變量。初始化它而不是'null'。隨機rn =新隨機();' – tredontho