Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Nim游戏Java错误?_Java_Syntax Error - Fatal编程技术网

Nim游戏Java错误?

Nim游戏Java错误?,java,syntax-error,Java,Syntax Error,嗨,我正在为尼姆编写一个游戏 我已经完成了大部分代码,但我有两个主要问题 我找不到我的错误 我似乎无法让电脑轮到它 提前谢谢,这是我的代码 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.next

嗨,我正在为尼姆编写一个游戏

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

  • 我找不到我的错误

  • 我似乎无法让电脑轮到它

  • 提前谢谢,这是我的代码

    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中出现异常。错误:未解决的编译问题: 语法错误,请插入“;”以完成LocalVariableDeclarationStatement
    语法错误,请插入“;”以完成语句

    我无法重现您的编译错误


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

    您可以帮助我如何执行此操作吗?在您的主循环中,类似于:
    while(numStones>0){player();computerTurn();}
    。您不需要
    computerTurn()
    btw中的第一个参数。因为您无论如何都要覆盖它,所以只需在方法的开头声明它。
    Random rn=null;int compTake=rn.nextInt(3)
    我很确定这将始终导致
    NullPointerException
    ?类似于
    main
    方法中的随机变量。初始化它,而不是
    null
    <代码>随机rn=新随机()