如何编写while循环

时间:2019-06-10 22:08:15

标签: java

因此,我正在为纸牌游戏编写一些代码,在此纸牌游戏中,我已经编码了第一轮的工作方式。我的问题是我将如何能够一遍又一遍地重复此代码,直到卡用完(因为每张卡只能使用一次)。我在想我必须使用while循环。为了方便起见,我将解释这里的每种方法的作用,以便您可以理解,因此请耐心等待。其中一些是无用的代码,我还没有完成并变得有用。3

游戏规则:https://tobakumokushirokukaiji.fandom.com/wiki/E_Card

**以下是方法:**

typeOfCard(); -这会告诉您您在玩哪面,并且是随机决定的,到目前为止,您只能在皇帝一方玩。

winOrLose()-这是我在发布本文之前使用的最后一种方法,这是我正在尝试实现的方法,以允许我将当前的获胜或失败输出替换为字符串,比打印语句更容易调用。我要这样做,以便获胜的游戏可以将int wincounter设置为1,以便可以打印出来。

emperorsTurn()-这是询问您要与计算机玩哪张卡的主要问题的方法。

wincounter()-我需要删除一些代码,所以不用担心

import java.util.Random;
import java.util.Scanner;


public class CardGame {


    public static void main(String[] args) {

        int numberOfCards = 7;
        if (numberOfCards > 6) {
            numberOfCards--;
            //System.out.println("your number of cards is " +numberOfCards);


        }
        typeOfCard();
    }


    public static void typeOfCard() {
        Random card  = new Random();
        int number = 0;


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

        if (number == 1)    {
            System.out.println("your are playing on the emperor side");

        }
        if (number ==2){
            System.out.println("You are playing on the slave side ");
        }
        if (number ==1){

            emperorsTurn();



        }





}

    public static void winOrLose(){
        int wincounter = 0;
        String win = "You won the round";
        String lose = "You lose the round ";

        if (wincounter >0) {
            System.out.println(win);
        }
        else if (wincounter == 0) {
            System.out.print(lose);
        }


    }

    public static void emperorsTurn() {
        Random cards = new Random();
        int computerinput = 0;
        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) {
            System.out.println("you have played the emperor! \n the emperor is defeated by the slave");

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

            winOrLose();
            wincounter();
        }






        else if (userinput == 2) { //when the user input is 2
            if (computerinput == 1) {
                System.out.println("you have played the citizen, this defeats the slave");
                wincounter();
            } else if (computerinput == 2) {
                System.out.println("you have played the citizen, this ties with the citizen");
            }
            //print out something else if number is not 1,2 or 3
        }
    }

    public static void wincounter() {
        int i = 0;
        if (i < 1)i++;

        System.out.println("you have won " +i +" number of draws");
    }

}

1 个答案:

答案 0 :(得分:0)

尝试做一个while循环,将剩余的纸牌数量与玩游戏所需的最小数量进行比较。

while (numberOfCards > minNumberNeeded) {
   playGame();
}
相关问题