【问题标题】:Deck Dealing Java甲板交易 Java
【发布时间】:2014-08-08 02:54:55
【问题描述】:

我正在尝试为执行以下操作的类编写代码:

  • 询问要使用多少套牌
  • 程序会在每次按下 Enter 时从卡组中生成一张未使用的卡片
  • 当没有更多牌可以发牌时,程序会通知用户
  • 程序允许用户再次播放
  • 方法被广泛使用

到目前为止,我的代码如下所示:

import java.util.*;
public class IA3 {

@SuppressWarnings({ })
public static void play () {
}
@SuppressWarnings("resource")
public static void main(String[] args) {
    boolean draw = true;
    boolean pa = true;
    Scanner console = new Scanner(System.in);
    // TODO Auto-generated method stub
    System.out.println("Hello! Please input the number of decks you would like to use:");
    int decks = console.nextInt();

    int t = decks * 52;
    System.out.println("Your total amount of cards to use are " +t);
    int a = 0;
    do {
        int[] deck = new int[t];
        //declaration of suits and ranks
        String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
        String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};

        //Initializing of the cards
        for (int i = 0; i < t; i++) {
        deck[i] = i;
                }

            System.out.println("Press enter to draw a random card : "); //allows player to prompt the system for the next card

            String input = console.nextLine();
            a++;
            if (input.equals("")) {
                // Shuffles the cards
                for (int i = 0; i < 1; i++) {
                  int index = (int)(Math.random() * t);
                  int temp = deck[i];
                  deck[i] = deck[index];
                  deck[index] = temp;
                }

                      for (int i = 0; i < 1; i++) {
                      String suit = suits[deck[i] / 13];
                      String rank = ranks[deck[i] % 13];
                      System.out.println(rank + " of " + suit);
                      }
             if (a>t) {
                 System.out.println ("No more cards available");
                 break;
             }
                      System.out.println("Draw another card? (Yes or No) : ");
                      String again = console.nextLine();
                      if (again.equals("Yes")) {
                          continue;
                      }
                      if (again.equals("No")) {
                          draw = false;
                          System.out.println("Game over!");
                      }
                        System.out.println("Play again? (Yes or No) : ");
                          String plag = console.nextLine();
                          if (plag.equals("Yes")) {
                              continue;
                          }
                          if (plag.equals("No")) {
                              pa = false;
                              System.out.println("Thank you for playing!");
                              break;
                          } while (pa == true);
                  }
        } while (draw == true);
    }
}

当我尝试运行超过 1 套牌时会出现问题,有时会立即失败,而有时可能会运行 4 次交易然后失败。我似乎也无法让它再次运行;每当我输入“是”而不是再次播放时,它就会终止。如您所见,我没有方法(我对方法很迷茫)。任何帮助将不胜感激,谢谢!

【问题讨论】:

  • 请正确格式化您的代码。很难阅读。
  • 对不起,我真的不擅长整个编码。
  • 在网上搜索“如何在(您的文本编辑器或 IDE)中格式化代码”

标签: java methods


【解决方案1】:

您应该发布完整的错误消息。没有它,很难猜测发生了什么。我看到至少有一个地方数组索引可能超出边界:

String suit = suits[deck[i] / 13];

如果您有 1 个以上的牌组,此划分的结果将大于 3。您可以使用以下方法修复它:

String suit = suits[(deck[i] / 13) % 4];

这样可以确保不仅仅使用索引为 [0,1,2,3] 的元素。

【讨论】:

  • 那部分有效;谢谢!你会不会碰巧知道我如何输入法,并让再次播放功能发挥作用?
【解决方案2】:

还不能发表评论,所以这里有一些想法可以让这段代码更容易理解,尤其是你的问题是寻求“任何帮助”。

删除如下所示的 for 循环:

for (int i = 0; i < 1; i++) {
 // code
}

上面的 for 循环将 i 设置为 0,然后执行内部代码,然后增加 i(所以 i 现在等于 1),然后检查 i 是否小于 1(不是因为它等于 1现在),因此它不会再次执行内部代码。所以换句话说,这种类型的 for 循环是不必要的,因为它只能执行一次其内部代码。周围的 for 循环应该被移除,内部代码保持不变。

将你的牌组存储为 ArrayList 而不是数组可能更简单。这样做的好处是您可以利用 .remove(int) 和 .size() 之类的操作,并且您不必担心洗牌(您可以随机删除一个条目并将其打印出来)。

希望能给你一些正确方向的指点。继续加油!

这是一个有用的创建方法的教程,我通过谷歌搜索“java 方法”找到了它。 http://www.tutorialspoint.com/java/java_methods.htm

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2019-07-17
  • 1970-01-01
  • 2013-01-12
  • 1970-01-01
  • 2011-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多