简体   繁体   English

用Java编写二十一点控制台程序

[英]Writing a blackjack console program in Java

I have an assignment of making a blackjack like program in a class. 我的任务是在课堂上制作像二十一点一样的程序。 My first problem I am dealing with is creating an array of the cards. 我要处理的第一个问题是创建一系列卡片。 The professor wants an array setup with a txt file with the following format. 教授希望使用以下格式的txt文件进行阵列设置。

2 of hearts
2 of diamonds
2 of spades
2 of clubs
3 of hearts
3 of diamonds
3 of spades 

This goes on till face cards when it replaces the number with jack, queen, king, ace. 当用杰克,王后,国王,王牌代替数字时,这种情况一直持续到面牌。 Following the professors requirements, How would I take input from the txt file and just store the number and the hearts,diamonds,spades, and clubs. 按照教授的要求,我将如何从txt文件中获取输入内容,然后只存储数字以及心,钻石,黑桃和球杆。 Thank you for the help 感谢您的帮助

逐行读取文件,您可以使用“”作为分隔符将其拆分。

I'm sure you heard about the Scanner class. 我确定您听说过Scanner类。

But in case you haven't: http://java.sun.com/javase/6/docs/api/java/util/Scanner.html 但是,如果您没有: http : //java.sun.com/javase/6/docs/api/java/util/Scanner.html

使用java.util.Scanner类,逐行读取文件,在每一行中扫描文本“ of”,以将卡值与卡套分开。

You can read lines with a Scanner object. 您可以使用Scanner对象读取行。 Let's say your setup file is in "cards.txt" 假设您的安装文件位于“ cards.txt”中

Scanner sc = new Scanner(new File("cards.txt));

while(sc.hasNextLine()) {
    String line = sc.nextLine(); // each one of these will be like the "3 of Spades"
    // have code here to decode the line
}

This should point you in the right direction. 这应该为您指明正确的方向。 Don't forget to import java.io.* (or .File) and java.util.* (or.Scanner)! 不要忘记导入java.io。*(或.File)和java.util。*(或.Scanner)! :-) :-)

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

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