gpt4 book ai didi

java - 如何创建一个循环,当与该循环关联的变量达到特定数字时更改 boolean 值

转载 作者:行者123 更新时间:2023-11-30 05:35:58 24 4
gpt4 key购买 nike

我正在编写一个纸牌游戏,并且我有一个特定的 boolean 值,我已将其设置为 false,citizenLimiter。当我的游戏中的公民卡总共玩了 4 次时,我想将此 boolean 值设置为 true。这应该允许它阻止您打牌并打出另一张牌。我的问题是,cardLimiter(我用来计算游戏获胜次数的变量)在第一次使用该卡后自动将 CitizenLimiter 设置为 true。我需要做什么来解决这个问题?

我尝试过使用嵌套循环,还尝试设置将 boolean 值更改为不同数字所需的变量。

public static void emperorsTurn() {
if (cardLimiter <= 4){
citizenLimiter = true;
}

Random cards = new Random();
int computerinput = 0;
int numberx = 5;

for (int counter = 1; counter <= 3; counter++) {
computerinput = 1 + cards.nextInt(2);
}


Scanner sc = new Scanner(System.in);
System.out.println("Please pick the card you are playing. \n if you are playing the Emperor press 1, if you are playing the citizen press 2 ");
int userinput = sc.nextInt();

if (userinput == 1 && computerinput == 1 && emperorLimiter == false) {
System.out.println("you have played the emperor! \n the emperor is defeated by the slave");
emperorLimiter =true;
System.out.println("you can no longer use the emperor");
} else if ((userinput == 1 && computerinput == 1 && emperorLimiter == true)) {
System.out.println("you cannot play the emperor this turn \n you have played the citizen instead. The citizen is defeated by the slave");

//make it so that the win/ lose is shown here


} else if (userinput == 1 && computerinput == 2 && emperorLimiter == false) {
System.out.println("you have played the emperor the emperor defeats the citizen");

winOrLose();

numberx--;
} else if (userinput == 2 ) { //when the user input is 2
if (computerinput == 1 && citizenLimiter == false) {
System.out.println("you have played the citizen, this defeats the slave");

cardLimiter++;
} else if (computerinput == 2 && citizenLimiter == false) {
System.out.println("you have played the citizen, this ties with the citizen");
cardLimiter++;
}
if (computerinput==1 && citizenLimiter == true) {

System.out.println("you are out of citizen cards, you play the emperor instead, this is defeated by the slave");
//i need to make it so that once you are out of a certain type of card it plays the emperor instead


}
else if (computerinput == 2 && citizenLimiter == true ){

System.out.println("you are out of citizen cards, you play the emperor instead, this defeats the citizen");
}
}
}

最佳答案

不是应该反过来吗?

if (cardLimiter > 4){
citizenLimiter = true;
}

关于java - 如何创建一个循环,当与该循环关联的变量达到特定数字时更改 boolean 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56622877/

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