因此,我有一个这样的文本文件格式,我试图将其转换为单张卡片并将它们放入数组中,并在下面放置了文本文件和类以向您展示我的所作所为:
a
h
2
h
3
h
4
h
5
h
6
h
7
h
8
h
9
h
t
h
j
h
q
h
k
h
a
d
2
d
3
d
4
d
5
d
6
d
7
d
8
d
9
d
t
d
j
d
q
d
k
d
a
c
2
c
3
c
4
c
5
c
6
c
7
c
8
c
9
c
t
c
j
c
q
c
k
c
a
s
2
s
3
s
4
s
5
s
6
s
7
s
8
s
9
s
t
s
j
s
q
s
k
s
因此,现在在注释的帮助下,我添加了以下类:
卡片:
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 List < Card > buildDeck() throws IOException {
List < String > cardLines = Files.readAllLines(Paths.get("cards.txt"));
// build card objects
List < Card > result = new ArrayList < Card > ();
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
}
return result;
}
/*public void shuffle() {
// fill in
}*/
}
这将是我的GUI类-
import java.util.Scanner;
public class Game {
public Scanner scan;
public 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":
buildDeck();
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 buildDeck() {
// TODO Auto-generated method stub
}
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");
}
}
因此,在我的其他错误现在似乎已得到解决,并且已经通过注释获得了帮助之后,如何将卡座类中的arraylist打印到我的GUI类选项1中,谢谢!
您可以从文件中检索卡。
public static void main(String[] args) throws Exception {
List<String> cardLines = Files.readAllLines(Paths.get(yourCardFile));
// Build your card objects from the file
List<Card> cards = new ArrayList<>();
for (int i = 0; i < cardLines.size(); i += 2) {
cards.add(new Card(cardLines.get(i), cardLines.get(i + 1)));
}
// Print the cards out
for (Card card : cards) {
System.out.println(card);
}
// Do what you need to do after you have your cards
}
public static 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;
}
}
结果(并非所有的卡都被截屏):
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句