簡體   English   中英

甲板交易Java

[英]Deck Dealing Java

我正在嘗試為執行以下操作的類編寫代碼:

  • 詢問要使用多少套牌
  • 每次按下輸入鍵時,程序將從卡座中產生一張未使用的卡
  • 當沒有更多的卡片可以發給時,程序會通知用戶
  • 程序允許用戶再次播放
  • 方法被廣泛使用

到目前為止,我的代碼如下所示:

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個交易然后失敗。 我似乎也無法使它再次運行。 只要我輸入“是”,它就會終止,而不是再次播放。 如您所見,我那里沒有方法(我在方法上迷失了方向)。 任何幫助,將不勝感激,謝謝!

您應該發布完整的錯誤消息。 沒有它,很難猜測正在發生什么。 我看到數組索引可能超出邊界的至少一個地方:

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

如果您的甲板多於1個,則此划分的結果將大於3。 您可以使用以下方法修復它:

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

這樣,您可以確保僅使用索引為[0,1,2,3]的元素。

尚無法發表評論,因此,這里有一些想法可以使此代碼更易於遵循,特別是在您的問題要求“任何幫助”的情況下。

刪除如下所示的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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM