gpt4 book ai didi

java - Nim 游戏再次出错

转载 作者:太空宇宙 更新时间:2023-11-04 15:21:37 24 4
gpt4 key购买 nike

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

当计算机处于哑模式时,每当我接近赢得比赛时(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/

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