简体   繁体   English

甲板交易Java

[英]Deck Dealing Java

I'm trying to write a code for class that does the following: 我正在尝试为执行以下操作的类编写代码:

  • Asks how many decks to use 询问要使用多少套牌
  • Program produces a single unused card, from the deck(s) each time enter is pressed 每次按下输入键时,程序将从卡座中产生一张未使用的卡
  • Program notifies the user when there are no more cards to deal 当没有更多的卡片可以发给时,程序会通知用户
  • Program Allows the user to play again 程序允许用户再次播放
  • Methods are used extensively 方法被广泛使用

So far, my code looks like this: 到目前为止,我的代码如下所示:

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);
    }
}

Issues arise when I try to run more than 1 deck, sometimes failing immediately, while sometimes running maybe 4 deals then failing. 当我尝试运行超过1个套牌时,会出现问题,有时会立即失败,而有时会运行4个交易然后失败。 I also cannot seem to get it to run again; 我似乎也无法使它再次运行。 it just terminates whenever I input "Yes" instead of playing again. 只要我输入“是”,它就会终止,而不是再次播放。 As you can see, I have no methods in there (I am SO LOST on methods). 如您所见,我那里没有方法(我在方法上迷失了方向)。 Any help would be appreciated, thank you! 任何帮助,将不胜感激,谢谢!

You should post full error message. 您应该发布完整的错误消息。 Without it it's hard to guess what is happening. 没有它,很难猜测正在发生什么。 I see at least one place where array index can be out of boundaries: 我看到数组索引可能超出边界的至少一个地方:

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

Result of this devision will be greater than 3 if you have more than 1 deck. 如果您的甲板多于1个,则此划分的结果将大于3。 You can fix it using: 您可以使用以下方法修复它:

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

This way you ensure than only elements with indexes [0,1,2,3] will be used. 这样,您可以确保仅使用索引为[0,1,2,3]的元素。

Can't post a comment yet, so here are a few ideas to make this code easier to follow, especially that your question is asking for "any help." 尚无法发表评论,因此,这里有一些想法可以使此代码更易于遵循,特别是在您的问题要求“任何帮助”的情况下。

Remove for loops that look like this: 删除如下所示的for循环:

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

The above for loop sets i to 0, then executes the internal code, then increments i (so i is now equal to 1), then checks to see if i is less than 1 (which it is not because it equals 1 now), so it does not execute the internal code again. 上面的for循环将i设置为0,然后执行内部代码,然后递增i(所以i现在等于1),然后检查i是否小于1(不是因为现在等于1),因此它不会再次执行内部代码。 So in other words, this type of for loop is not necessary because it can only ever execute its internal code once. 因此,换句话说,这种类型的for循环是不必要的,因为它只能执行一次其内部代码。 The surrounding for loop should be removed and the internal code left intact. 应该删除周围的for循环,并保持内部代码不变。

It may be simpler to store your deck as a ArrayList, instead of an array. 将甲板存储为ArrayList而不是数组可能更简单。 The advantage of this is that you can take advantage of operations like .remove(int) and .size(), and you wouldn't have to worry about shuffling your deck (you could just randomly remove an entry and print it out). 这样做的好处是您可以利用.remove(int)和.size()之类的操作,而不必担心改组您的卡片组(您可以随意删除条目并将其打印出来)。

Hope that gives you a few pointers in the right direction. 希望能为您提供正确方向的一些指导。 Keep it up! 保持!

And here is an helpful tutorial for creating methods, which I found by doing a google search for "java methods". 这是有关创建方法的有用教程,我通过谷歌搜索“ java方法”发现了该方法。 http://www.tutorialspoint.com/java/java_methods.htm http://www.tutorialspoint.com/java/java_methods.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM