我遇到了这个 java 问题,我想看看为它编写代码是多么容易。
编写一个 Java 程序,让两个人玩以下游戏。我们从一堆筹码开始。您的游戏将允许奇数。第一个玩家从筹码堆中取出 1 到(筹码数 - 1)/2。从那时起,玩家交替轮流,从堆中取出筹码,直到它为空。一旦堆空了,游戏就结束了。
一切都很顺利,直到我不得不交替转弯。我做对了,但是当玩家 1 输入一个值,而玩家 2 也这样做时,当 while 循环再次进行时,它从 0 开始并且不添加过去的输入。
这就是我的意思:
while(chips!=0){
if(counter%2==0){
System.out.println(fname + " has " + (one) + " chips" + "\n" + sname + " has " + (two) + " chips." + "\n" + "It is your turn, " + fname + "\n" + "There are " + chips + " remaining." + "\n" + "You may take any number of chips from 1 to " + (chips-1)/2 + ". How many will you take, " + fname + "?") ;
one +=input.nextInt();
chips -=one;
} else {
System.out.println(fname + " has " + (one) + " chips" + "\n" + sname + " has " + (two) + " chips." + "\n" + "It is your turn, " + sname + "\n" + "There are " + chips + " remaining." + "\n" + "You may take any number of chips from 1 to " + (chips-1)/2 + ". How many will you take, " + sname + "?") ;
two +=input.nextInt();
chips -=two;
}
counter++;
}
有没有人有解决方案?