Java 如何循环一个文本文件并每两行返回一个卡片对象?
所以,我有一个这样的文本文件格式,我试着把它变成一张卡片,然后把它们放进一个数组中,我把文本文件和类放在下面,告诉你我做了什么:Java 如何循环一个文本文件并每两行返回一个卡片对象?,java,loops,Java,Loops,所以,我有一个这样的文本文件格式,我试着把它变成一张卡片,然后把它们放进一个数组中,我把文本文件和类放在下面,告诉你我做了什么: 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
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
现在,在评论的帮助下,我添加了以下类:
卡片:
和甲板(当前未完成)-
因此,在我的其他错误现在看来已经修复,我已经从注释中获得了帮助之后,我如何才能将deck类中的arraylist打印到我的GUI类选项1中,谢谢 您可以使用
文件。readAllLines
:
Iterator<String> it = Files.readAllLines(Paths.get(...)).iterator();
for (int i = 0; i < 52; i++) {
String number = it.next();
String suit = it.next();
// ...
}
Iterator it=Files.readAllLines(path.get(…).Iterator();
对于(int i=0;i<52;i++){
字符串编号=it.next();
String suit=it.next();
// ...
}
或者另一种方法是使用
扫描仪
,这非常类似。这里有一些方法可以帮助您从文件中检索卡片
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;
}
}
publicstaticvoidmain(字符串[]args)引发异常{
List cardLines=Files.readAllLines(path.get(yourCardFile));
//从该文件生成卡对象
列表卡=新的ArrayList();
对于(int i=0;i
结果(并非所有卡片都被截屏):
您的代码在哪里?我目前没有代码,我想有人可以给我一些指导,告诉我如何编写循环,我可以使用循环获取卡值。我刚刚开始我的第一堂课,一个卡片课,我对这一点很不了解。@irrossi看一下
扫描器类…会的,谢谢brso,当我写东西时会在这里发布我的代码:)你必须指出buildDeck抛出异常(看我答案中的main),或者您需要将try/catch代码添加到buildDeck方法中。谢谢!你把你的卡片文件放在哪里,我会把“cards.txt”(我的卡片文件的名称)放在哪里?在它周围加上引号?@Irrossi如果你在变量中有你的卡片文件的完整路径,你可以使用变量。如果您不这样做,并且想要硬编码完整路径,则必须使用引号。@Irrossi请检查我的答案是否解决了您的问题。:-)@Irrossi将此代码放在您的问题中,而不是作为评论。确保它的格式是可读的。
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");
}
}
Iterator<String> it = Files.readAllLines(Paths.get(...)).iterator();
for (int i = 0; i < 52; i++) {
String number = it.next();
String suit = it.next();
// ...
}
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;
}
}