java - Nim 游戏再次出错

标签 java runtime runtime-error

我不确定是否应该为此创建一个新问题......但现在出现了一个新问题。 当计算机在所谓的“智能模式”下执行时,程序运行得很好,但当程序在“哑模式”下运行时,就会出现错误

当计算机处于哑模式时,每当我接近赢得比赛时(Snickerdoodles,我刚刚输掉比赛),我都会收到错误...错误如下:

java.lang.IllegalArgumentException: n must be positive
at java.util.Random.nextInt(Random.java:294)
at GameOfNim.main(GameOfNim.java:62)

我知道在这种情况下 n 必须是正数,但我什至有一个 if 语句告诉代码如果实际发生了该怎么办...

你们有什么解决办法吗?

哦,这里又是代码(我没有包括智能模式方法,因为这与它无关):

import java.util.*;
public class GameOfNim
{
    public static void main (String [] args)
    {
        Scanner in = new Scanner (System.in);
        Random num = new Random ();
        int numberLeft = num.nextInt(101-10) + 10;
        int computerMode = num.nextInt(2);
        int subtraction = numberLeft;
        boolean turn = num.nextBoolean();
        String computer = "";

        System.out.println ("The number you start out with is: " + numberLeft);
        System.out.println ("-------------------");

        if (computerMode == 0)
        {
            System.out.println ("The computer is in smart mode");
            System.out.println ("-------------------");
            computer = "Smart";
        }
        if (computerMode == 1)
        {
            System.out.println ("The computer is in dumb mode");
            System.out.println ("-------------------");
            computer = "Dumb";
        }

        while (numberLeft > 1)
        {
            if (turn == true)
            {
                System.out.println ("-------------------");
                System.out.println ("It is your turn...");
                System.out.printf ("Please enter the number you wish to take from the pile (Remember it has to be less than " + ((numberLeft/2) +1) + "): ");
                subtraction = in.nextInt();
                numberLeft -=subtraction;
                System.out.println ("The number left is " + numberLeft);
                turn = false;

            }
            if (turn ==false)
            {
                System.out.println ("-------------------");
                System.out.println ("It is the computer's turn...");
                if (computer.equals("Smart"))
                {
                    numberLeft = smartComputer(numberLeft);
                    System.out.println ("The number left is " + numberLeft);

                }

                if (computer.equals("Dumb"))
                {
                    if (numberLeft - num.nextInt(numberLeft/2) <= 1)
                    {
                        return;
                    }
                    else
                    {
                        numberLeft -= (num.nextInt(numberLeft/2 - 1) + 1);
                    }
                    System.out.println ("The number left is " + numberLeft);
                }
                turn = true;
            }
        }

        if (numberLeft <= 1)
        {
            if (turn = false)
            {
                System.out.println ("~~~~~~~~~~~~~~~~~~~~~~~~");
                System.out.println ("You Win!");
            }
            else
            {
                System.out.println ("~~~~~~~~~~~~~~~~~~~~~~~~");
                System.out.println ("You're horrible...you lost to a computer.");
            }
        }
    }

最佳答案

if (numberLeft <= 1)
    {
        if (turn = false)
        {
            System.out.println ("~~~~~~~~~~~~~~~~~~~~~~~~");
            System.out.println ("You Win!");
        }
    }

这里有一个赋值而不是相等检查。虽然我不认为这是整个问题的原因

关于java - Nim 游戏再次出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20340516/

相关文章:

Stm32F103 Arm 运行时系统,GNAT Ada 编译器

java - 在 SQLite 中运行随机查询时出错

go - panic : runtime error: slice bounds out of range when concurrently running as goroutine

c++ - 在 SPOJ 上提交代码会导致运行时错误 (SIGABRT)

java - 在C++中,静态变量、动态变量和局部变量存储在哪里?在 C 和 Java 中怎么样?

java - JDBC Statement.setFetchsize 是如何工作的

java - 为什么我的进程终止?

java - View 中的 setBackground 不能应用于 (int)

c++ - C++中std::map的运行时复杂度是多少?

java - 执行ExecutorService时出现NullPointerException