【问题标题】:How to create a loop for this program to repeat the game if there is a tie如果出现平局,如何为该程序创建循环以重复游戏
【发布时间】:2020-10-20 02:43:12
【问题描述】:

我已经创建了这段代码,我正在尝试创建一个循环,让它重复并丢弃 10 张牌,然后如果结果是平局,则显示第 11 张牌,但我不知道如何正确地做到这一点。

public class War {


public static void main(String args[]) {
 Card player = new Card();
    Card computer = new Card();


    final int CARDS_IN_SUIT = 13;

    char[] su = {'C', 'H', 'D', 'S'};
    char randomSuit = su[(char) (Math.random() * (su.length - 1))];
    char randomSuit2 = su[(char) (Math.random() * (su.length - 1))];


    
    int playervalue = ((int) (Math.random() * 100) % CARDS_IN_SUIT + 1);
    int computervalue = ((int) (Math.random() * 100) % CARDS_IN_SUIT + 1);



    System.out.println("Player: " + playervalue + " of " + randomSuit);

    player.getValue(randomSuit, playervalue);

    System.out.println("Computer: " + computervalue + " of " + randomSuit2);
    computer.getValue(randomSuit2, computervalue);


    player.setValue(randomSuit, playervalue);

    computer.setValue(randomSuit2, computervalue);

    if (playervalue > computervalue)
        System.out.println("Results: Player won");
    else if (computervalue > playervalue)
        System.out.println("Results: Computer won");

    else
            System.out.println("Results: Equal");


        }
}

这是类卡片

public class Card {

char suit;
int number;



public void setValue(char su, int num) {
    suit = su;
    number = num;


    if (number <= 1 || number >= 13) {

    } else {

        number = 1;
    }

}


public void getValue(char su, int num) {

    switch (su) {
        case 'C':

            break;

        case 'H':

            break;
        case 'D':

            break;

        case 'S':

            break;



    }

    }
}

关于如何做或开始的任何建议?我对编码非常陌生,所以这是我的第一个程序。

【问题讨论】:

  • 我认为你可以使用 do{}while() :)

标签: java loops for-loop while-loop


【解决方案1】:

我修改了你的代码,它可以按照你的意愿工作。

以下是我更改的内容和原因:

  1. 将变量声明为Global/Class 变量,以便可以从任何方法访问它们。

(如果您正在调用方法,则必须将变量或方法声明为static 或者使用main()方法中的变量,因为它也是static)

  1. 创建了一个单独的方法shuffleCard() 将卡片分配给玩家和计算机,这样您就不必一遍又一遍地编写相同的代码。
  2. 创建了一个单独的方法printResult,它将检查玩家和计算机卡,然后相应地打印结果。

别担心如果你不明白这些变化的含义,只要使用代码继续学习JAVA,你很快就会了解方法、变量等等。

修改代码:

public class War 
{

  static Card player = new Card();
  static Card computer = new Card();  
  
  static final int CARDS_IN_SUIT = 13;
  static char[] su = {'C', 'H', 'D', 'S'};
  
  static int playervalue;
  static int computervalue;
    
public static void shuffleCard()
{
    char randomSuit = su[(char) (Math.random() * (su.length - 1))];
    char randomSuit2 = su[(char) (Math.random() * (su.length - 1))];

    playervalue = ((int) (Math.random() * 100) % CARDS_IN_SUIT + 1);
    computervalue = ((int) (Math.random() * 100) % CARDS_IN_SUIT + 1);

    System.out.println("Player: " + playervalue + " of " + randomSuit);
    player.getValue(randomSuit, playervalue);

    System.out.println("Computer: " + computervalue + " of " + randomSuit2);
    computer.getValue(randomSuit2, computervalue);

    player.setValue(randomSuit, playervalue);
    computer.setValue(randomSuit2, computervalue);  
    printResult();
}        

public static void printResult()
{
 if (playervalue > computervalue)
        System.out.println("Results: Player Won");
 else if (computervalue > playervalue)
        System.out.println("Results: Computer Won");
 else{
        System.out.println("Results: Equal");
        System.out.println("Let's shuffle the cards again");
        shuffleCard();
 }         
}        

public static void main(String args[]) 
{
  shuffleCard();
}
}

如果您想了解,以下是该程序现在的运作方式:

  1. main() 方法调用shuffleCard() 方法生成随机卡片并将值分配给player 和computer。

  2. 然后,在shuffleCard()方法的最后,调用printResult()方法。它比较玩家和计算机卡并相应地打印结果。

  3. 你应该注意的重要的一点是,在printResult()方法中,你可以看到,如果玩家卡和电脑卡相同,在其他也就是说,结果是平局,那么shuffleCard()方法再次调用,即程序回到第1点。

  4. 现在,shuffleCard() 方法将再次生成随机卡片并将值分配给 player 和 computer。然后,它会再次调用printResult() 方法。

(在这里,您可以再次洗牌,也可以通过删除shuffleCard()方法末尾的shuffleCard()方法并添加显示第11张牌的代码来显示第11张牌。我添加了shuffleCard()演示一个正确的循环。)

所以,这个循环会一直持续到玩家卡和电脑卡不同为止。

谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-22
    • 1970-01-01
    • 2012-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多