[英]How do i create a loop that changes a boolean's value when the variable associated with that loop reaches a certain number
我正在寫一個紙牌游戲,我有一個特定的布爾值,我已將其設置為 false,citizenLimiter。 當我的游戲中的公民卡總共玩了 4 次時,我想將此布爾值設置為 true。 這應該允許它阻止您玩牌並改用另一張牌。 我的問題是 cardLimiter,我用來計算游戲獲勝次數的變量,在第一次使用卡片后自動將 CitizenLimiter 設置為 true。 我需要做什么來解決這個問題?
我試過使用嵌套循環,也試過設置將布爾值更改為不同數字所需的變量。
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.