gpt4 book ai didi

java - Nim 游戏 Java 错误?

转载 作者:行者123 更新时间:2023-12-01 13:17:00 24 4
gpt4 key购买 nike

嗨,我正在编写 Nim 游戏,

我已经完成了大部分代码,但我遇到了两个主要问题。

  1. 我找不到错误

  2. 我似乎无法让计算机轮流运行

提前致谢,这是我的代码。

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中出现异常: Unresolved 编译问题:语法错误,请插入“;”完成LocalVariableDeclarationStatement语法错误,请插入“;”完成声明

最佳答案

我无法重现您的编译错误。

但是,您没有游戏循环(即,在仍然存在有效移动的情况下保持游戏继续进行的循环),并且您从不调用 computerTurn

关于java - Nim 游戏 Java 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22391380/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com