我创建了纸牌游戏,但我不知道如何返回我的数组,因为我过去使用过instanceof来使用不同的方法,但从来没有像这样的整个数组。我把我的代码放在下面:

卡类-

/**
 *
 * Constructor for a single card
 *
 */

public class Card {
  public String number;
  public String suit;

  public Card(String n, String s) {
    number = n;
    suit = s;
  }

  @
  Override
  public String toString() {
    return number + suit;
  }
}


甲板课-

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class Deck {
  private ArrayList < Card > cards;

  public Deck() {
    cards = new ArrayList < Card > ();
  }

  public void buildDeck() throws IOException {

    List < String > cardLines = Files.readAllLines(Paths.get("cards.txt"));



    for (int i = 0; i < cardLines.size(); i += 2) { //if lines read are smaller than file then read for another card
      cards.add(new Card(cardLines.get(i), cardLines.get(i + 1))); //add new card to cards array
    }

  }

  public List < Card > getDeck() {
    return cards;

    /*public void shuffle() {
        // fill in
    }*/

  }
}


GUI类-

import java.util.List;
import java.util.Scanner;


public class Game {

  private Scanner scan;
  private Deck deck;

  private void runMenu() {
    String response;
    do {
      printMenu();
      System.out.println("What would you like to do:");
      scan = new Scanner(System.in);
      response = scan.nextLine().toUpperCase();
      switch (response) {
        case "1":
          PrintDeck();
          break;
        case "2":
          ShuffleCards();
          break;
        case "3":
          DealCard();
          break;
        case "4":
          MoveToPrevious();
          break;
        case "5":
          Move2PilesBack();
          break;
        case "6":
          AmalgamateInMiddle();
          break;
        case "7":
          PlayforMe();
          break;
        case "8":
          ShowLowScores();
        case "Q":
          break;
        default:
          System.out.println("Try again");
      }
    } while (!(response.equals("Q")));
  }

  private void ShowLowScores() {
    // TODO Auto-generated method stub

  }

  private void PlayforMe() {
    // TODO Auto-generated method stub

  }

  private void AmalgamateInMiddle() {
    // TODO Auto-generated method stub

  }

  private void Move2PilesBack() {
    // TODO Auto-generated method stub

  }

  private void MoveToPrevious() {
    // TODO Auto-generated method stub

  }

  private void DealCard() {
    // TODO Auto-generated method stub

  }

  private void ShuffleCards() {
    // TODO Auto-generated method stub

  }

  private void PrintDeck() {
    List < Card > buildadeck = deck.getDeck();
    System.out.println(buildadeck);


  }

  private void printMenu() {

    System.out.println("1 -  Print the pack ");
    System.out.println("2 -  Shuffle");
    System.out.println("3 -  Deal a card");
    System.out.println("4 -  Move last pile onto previous one");
    System.out.println("5 -  Move last pile back over two piles");
    System.out.println("6 -  Amalgamate piles in the middle");
    System.out.println("7 -  Play for me");
    System.out.println("8 -  Show low scores");
    System.out.println("q - Quit");

  }

  public static void main(String args[]) {
    System.out.println("****Welcome to patience is virtue****");
    Game cardsgame = new Game();
    cardsgame.runMenu();
    System.out.println("****Thanks for playing****");
  }
}


我刚刚更新了代码,没有语法错误,但是当我运行程序时,我在PrintDeck中遇到异常问题,为什么?

最佳答案

希望我能理解您的要求。
基本上,您希望下面的方法返回卡列表,以便可以在GUI中打印该列表?

使用buildDeck方法构建卡组,然后将卡添加到卡列表中。您不需要返回任何东西。相反,您可以为甲板创建一个getter方法,如下所示。

public void buildDeck() throws IOException {

    List<String> cardLines = Files.readAllLines(Paths.get("cards.txt"));

    for (int i = 0; i < cardLines.size(); i += 2) { //if lines read are smaller than file then read for another card
        cards.add(new Card(cardLines.get(i), cardLines.get(i + 1))); //add new card to cards array
    }
}

public List<Card> getDeck() {
     return cards;
}


Game类中,您应该这样做:

public class Game {
    private Deck deck;

public void runMenu() {
    deck = new Deck();
    deck.buildDeck();


然后在printDeck中,您可以按以下方式使用此实例:

public void printDeck() {
    for(Card card : deck.getDeck()) {
        // print card.
    }
}


完全不同的是,您的方法应以小写字母开头。 printDeck而不是PrintDeck

10-05 22:21