为什么我的卡片不能打印到我的文本文件中?

时间:2015-08-23 22:49:35

标签: java file text printing

我正在写一个玩扑克的程序。

目前,我认为问题在于shuffle方法。它应该生成52张卡,它们彼此不同,生成一个从0到51的随机数(总共52个),将“id”与特定卡匹配(通过名为“Card”的类中的构造函数),然后检查看看卡片是否已经生成。如果有,它将不会通过,计数器将不计算它。如果没有,它将被打印在一个名为“Deck.txt”的文本文件中,依此类推,直到打印出52张卡片。

该程序似乎运行正常但是当我检查Deck.txt文件时,那里从来没有任何东西。

这是我的代码:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.NoSuchElementException;
import java.util.Random;
import java.util.Scanner;

public class Deck {

static private File deck = new File("Deck.txt");
static private Random shuffler = new Random();
static private Scanner dreader;

static public void shuffle() {
    int card;
    for (int i = 0; i < 52; i++) {
        card = shuffler.nextInt(52)+1;
        PrintWriter cwriter = null;
        try {
            cwriter = new PrintWriter(deck);
        } catch (FileNotFoundException e) {
            // File not found
        }
        if (DeckChecker(card)) {
            try {
                cwriter.println((new Card(card)).toString());
            } catch (NullPointerException e) {
                // File never opened
            }
        }
        else n--;
    }
}

private static boolean DeckChecker(int card) {
    try {
        dreader = new Scanner(deck);
    } catch (FileNotFoundException e) {
    }
    boolean switch1 = true;
    String s =  null;
    while (switch1) {
        try{
            s = dreader.nextLine();
        }catch(NoSuchElementException e){
            return true;
        }
        if (Card.getId(s) == card)
            return false;

    }
    return true;
}
}

public class Card {

static final private String[] number = { "2", "3", "4", "5", "6", "7", "8",
        "9", "10", "J", "Q", "K", "A" };
static final private String[] symbol = { "Diamond", "Spades", "Hearts",
        "Clubs" };
private String cnumber;
private String csymbol;
private String cardName;
private StringBuilder namer;

public Card(int id) {
    csymbol = symbol[(int) id / 13];
    cnumber = number[id - (((int) id / 13) * 13)];
    namer.append(cnumber).append(" of ").append(csymbol);
    cardName = namer.toString();
}

@Override
public String toString() {
    return cardName;
}

static public int getId(String card) {
    int s = 0;
    int n = 0;
    boolean switch1 = true;
    String cards = card.substring(6);
    String cardn = card.substring(0, 1);
    while (switch1) {
        if (cardn.equals(number[n]))
            switch1 = false;
        else
            n++;
    }
    switch1 = true;
    while (switch1) {
        if (cards.equals(number[s]))
            switch1 = false;
        else
            s++;
    }
    return (s * 13) + n;
}
}

1 个答案:

答案 0 :(得分:3)

首先,这张卡不能编译。你有:

else n--;

此时不存在n

您还为该文件创建了许多不同的PrintWriter对象,但从不关闭流,这意味着它们都在争用写入同一文件,这可能就是它永远不会写入它们的原因。

所有这一切,这是一个非常糟糕的方式来洗牌你的牌组;它可以尝试多次,连续多次寻找最后丢失的卡,每次执行磁盘IO,这是程序可以做的最慢的事情之一。因此,我完全删除了DeckChecker方法,因为它不需要。

相反,只需生成所有卡片,然后将它们随机播放,然后将它们写入文件,如下所示:

import java.util.*; 

// snip, your code

public static void shuffleAndPrint() {
  List<Card> cardList = createShuffledDeck();
  writeDeckToFile(cardList);
}

private static List<Card> createShuffledDeck() {
  List<Card> cardList = new ArrayList<>();
  for (int i = 0; i < 52; i++) {
    cardList.add(new Card(i));
  }
  Collections.shuffle(cardList);
  return cardList;
}

private static void writeDeckToFile(List<Card> cardList) {
  try (PrintWriter cwriter = new PrintWriter(deck)) {
    for (Card c : cardList) {
      cwriter.println(c);
    }
  } catch (FileNotFoundException e) {
    e.printStackTrace();
  }
}