gpt4 book ai didi

java - 交替播放器并添加用户输入

转载 作者:行者123 更新时间:2023-11-30 08:46:19 25 4
gpt4 key购买 nike

我遇到了这个 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++;
}

有人对此有解决方案吗?

最佳答案

您需要将玩家的筹码增加一个值而不是分配它。

int amount = input.nextInt();
one += amount;
chips -= amount;

关于java - 交替播放器并添加用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32911084/

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