简体   繁体   English

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

[英]Why don't my cards print to my text file?

I am writing a program to play poker. 我正在编写一个玩扑克的程序。

Currently, I think the problem is in the shuffle method. 目前,我认为问题在于改组方法。 It should generate 52 cards, all different from each other by generating a random number from 0 to 51 (52 in total), matching that "id" to a specific card (via a constructor in a class called "Card") and then checking to see if the card had already been generated. 它应生成52张卡,彼此完全不同,方法是生成0到51之间的随机数(总共52张),将“ id”与特定卡匹配(通过名为“ Card”的类中的构造函数),然后检查查看卡是否已经生成。 If it had, it wouldn't go through and the counter wouldn't count it. 如果有的话,它将不会通过,柜台也不会计数。 If it hadn't it would be printed in a text file called "Deck.txt", and so on untill the 52 cards had been printed. 如果没有,它将被打印在一个名为“ Deck.txt”的文本文件中,依此类推,直到打印出52张卡片为止。

The program seems to run fine but when I check the Deck.txt file, there's never anything there. 该程序似乎运行良好,但是当我检查Deck.txt文件时,那里什么也没有。

Here is my code: 这是我的代码:

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

First of all, this card doesn't compile. 首先,该卡无法编译。 You have: 你有:

else n--;

Except n doesn't exist at that point. 那时n不存在。

You also create many different PrintWriter objects for that file, but never close the stream, meaning they're all in contention to write to the same file, which may be why it never writes to them. 您还为该文件创建了许多不同的PrintWriter对象,但是从不关闭流,这意味着它们都在争用同一文件的写入,这可能就是为什么它从不写入它们的原因。

All that being said, this is a really bad way to shuffle your deck of cards; 综上所述,这是洗牌的一种非常糟糕的方法。 it could try many, many times in a row looking for the last missing card, every time doing Disk IO which is one of the slowest things a program can do. 每次执行Disk IO时,它可能会连续尝试很多次以查找最后丢失的卡,这是程序可以执行的最慢的操作之一。 Therefore, I've deleted the DeckChecker method entirely, as it's not needed. 因此,由于DeckChecker它,我已完全删除了DeckChecker方法。

Instead, just generate all the cards, then shuffle them, then write them to the file, like this: 相反,只需生成所有卡,然后将它们洗牌,然后将它们写入文件,如下所示:

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

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

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