我的java代码似乎在某个地方陷入了循环
这是我的nim类游戏我的java代码似乎在某个地方陷入了循环,java,Java,这是我的nim类游戏 import java.util.Random; import java.lang.Math; import java.lang.String; public class GameOfNim { private int min,max; private int turn; private int firstturn; private int stupid; private int smart; private int computer; priv
import java.util.Random;
import java.lang.Math;
import java.lang.String;
public class GameOfNim
{
private int min,max;
private int turn;
private int firstturn;
private int stupid;
private int smart;
private int computer;
private int user;
private int first;
private int pile;
public GameOfNim(int min, int max ){
pile=(int)(Math.random()*max+min);
smart=(int)(Math.random()*100);
stupid=(int)(Math.random()*100);
System.out.println("Pile size is "+pile);
firstturn=(int)(Math.random()*100 + 1);
computer=0;
user=1;
if(firstturn>50)
{
firstturn=user;
}
else
{
firstturn=computer;
}
firstturn=turn;
}
public void play()
{
if(smart>50)
{
System.out.println("Computer is playing smart");
}
else
{
System.out.println("Computer is playing stupid");
}
if(firstturn==user)
{
System.out.println("You go first");
}
else
{
System.out.println("Computer goes first");
}
turn = firstturn;
while(pile-1>0)
{
if(turn==user)
{
String take = "How many marbles do you want to take away?";
System.out.println(take);
int take2 = Integer.parseInt(take);
while(take2>(int)(pile/2))
{
System.out.println("Only take away half or less from the pile");
take = "How many marbles do you want to take away?";
take2= Integer.parseInt(take);
}
pile= pile-take2;
System.out.println("There are "+pile+" marbles left");
turn=computer;
}
else
{
if(smart>50)
{
pile -= smartTake();
}
else
{
pile -= stupidTake();
}
turn = user;
}
}
}
private int smartTake()
{
int x = (int)(Math.random())*2-1;
int sMarbles= (int)Math.pow(2,x);
while (sMarbles > (.5 * pile) || sMarbles==0)
{
x = (int)(Math.random())*2-1;
sMarbles = (int)Math.pow(2,x);
}
System.out.println("The computer took away " + sMarbles +" marbles");
return sMarbles;
}
private int stupidTake(){
int stMarbles = pile/2;
while (stMarbles > (.5*pile) || pile==0)
{
stMarbles = pile/2;
}
System.out.println("The Computer took away " + stMarbles +" marbles");
return stMarbles;
}
}
这是我的驾驶课
import java.util.Random;
import java.lang.Math;
import java.lang.String;
public class GameOfNim
{
private int min,max;
private int turn;
private int firstturn;
private int stupid;
private int smart;
private int computer;
private int user;
private int first;
private int pile;
public GameOfNim(int min, int max ){
pile=(int)(Math.random()*max+min);
smart=(int)(Math.random()*100);
stupid=(int)(Math.random()*100);
System.out.println("Pile size is "+pile);
firstturn=(int)(Math.random()*100 + 1);
computer=0;
user=1;
if(firstturn>50)
{
firstturn=user;
}
else
{
firstturn=computer;
}
firstturn=turn;
}
public void play()
{
if(smart>50)
{
System.out.println("Computer is playing smart");
}
else
{
System.out.println("Computer is playing stupid");
}
if(firstturn==user)
{
System.out.println("You go first");
}
else
{
System.out.println("Computer goes first");
}
turn = firstturn;
while(pile-1>0)
{
if(turn==user)
{
String take = "How many marbles do you want to take away?";
System.out.println(take);
int take2 = Integer.parseInt(take);
while(take2>(int)(pile/2))
{
System.out.println("Only take away half or less from the pile");
take = "How many marbles do you want to take away?";
take2= Integer.parseInt(take);
}
pile= pile-take2;
System.out.println("There are "+pile+" marbles left");
turn=computer;
}
else
{
if(smart>50)
{
pile -= smartTake();
}
else
{
pile -= stupidTake();
}
turn = user;
}
}
}
private int smartTake()
{
int x = (int)(Math.random())*2-1;
int sMarbles= (int)Math.pow(2,x);
while (sMarbles > (.5 * pile) || sMarbles==0)
{
x = (int)(Math.random())*2-1;
sMarbles = (int)Math.pow(2,x);
}
System.out.println("The computer took away " + sMarbles +" marbles");
return sMarbles;
}
private int stupidTake(){
int stMarbles = pile/2;
while (stMarbles > (.5*pile) || pile==0)
{
stMarbles = pile/2;
}
System.out.println("The Computer took away " + stMarbles +" marbles");
return stMarbles;
}
}
出于某种原因,每次我运行它时,它都会一直运行到“计算机优先”的地步,然后就什么也没发生。我假设它陷入了一个循环,但我似乎无法找到原因。任何帮助都将不胜感激我建议您学习使用调试器。简单地逐行检查代码,可能会发现比有人在身上更多的错误,所以试着找出错误;在这一行中,你能告诉我turnsorry的价值吗?抱歉,我花了一段时间才做出回应,但我通过用户7790438所说的话找出了问题所在。firstturn=turn是一个错误,因为不管怎样,它总是0。还修复了几个其他错误。谢谢我建议您学习使用调试器。简单地逐行检查代码,可能会发现比有人在身上更多的错误,所以试着找出错误;在这一行中,你能告诉我turnsorry的价值吗?抱歉,我花了一段时间才做出回应,但我通过用户7790438所说的话找出了问题所在。firstturn=turn是一个错误,因为不管怎样,它总是0。还修复了几个其他错误。谢谢