Java-BlackJack项目-I/O和周期问题
我正在尝试制作一个由角色输入驱动的简单的Java-BlackJack项目-I/O和周期问题,java,io,blackjack,Java,Io,Blackjack,我正在尝试制作一个由角色输入驱动的简单的BlackJack游戏,在后面的部分我遇到了很多问题 我评论了给我带来麻烦的部分,其余部分似乎没有错误,我做了单元测试。 那么,我做了什么?我创建了一个类来保存所绘制的卡片并管理它们,table、player和dealer都是table的实例。 一个表最多有5张卡(为了简单起见),每个卡对象都来自具有向卡对象添加数据的方法的卡类 主类驱动程序,通过键盘输入字符来做出决定,我在这一点上遇到了问题 import java.io.IOException;
BlackJack
游戏,在后面的部分我遇到了很多问题
我评论了给我带来麻烦的部分,其余部分似乎没有错误,我做了单元测试。
那么,我做了什么?我创建了一个类来保存所绘制的卡片并管理它们,table、player和dealer都是table的实例。
一个表最多有5张卡(为了简单起见),每个卡对象都来自具有向卡对象添加数据的方法的卡类
主类驱动程序,通过键盘输入字符来做出决定,我在这一点上遇到了问题
import java.io.IOException;
class Table{
Card[] hand = new Card[5];
int counter = 1;
Table() {
for ( int i=0; i<hand.length; i++) {
hand[i]=new Card();
}
hand[0].GetCard();
}
void ReadCards(){
for(int i= 0;i<counter;i++ ) {
System.out.println("The card "+(i+1)+" is " + hand[i].name + " "+ hand[i].seed + "." );
}
}
void DrawCards() {
hand[counter].GetCard();
counter++;
}
boolean isOut() {
int sum = 0;
for(int i= 0;i<counter;i++ ) {
sum += hand[i].value;
if(sum >21) {
return true;
}
}
return false;
}
int TheSum() {
int sum = 0;
for(int i= 0;i<counter;i++ ) {
sum += hand[i].value;
}
return sum;
}
boolean isWIN(Table p2) {
int sum = 0;
int sump2 = 0;
for(int i= 0;i<counter;i++ ) {
sum += hand[i].value;
}
for(int i= 0;i<p2.counter;i++ ) {
sump2 += p2.hand[i].value;
}
if (sum>sump2) {
return true;
}
else return false;
}
class Card {
public int value = 0;
public String name = "";
public String seed = "";
void GetCard(){
int positive = 0;
do {
positive = (int) (Math.random()*100) % 10;
}while(positive == 0 );
value = positive;
if(value<10) {
name = String.valueOf(value);
}
else {
positive = 0;
do {
positive = (int) (Math.random()*100) % 3;
}while(positive == 0 );
switch(positive) {
case 1:
name = "J";
break;
case 2:
name = "Q";
break;
case 3:
name ="K";
break;
}
}
positive = 0;
do {
positive = (int) (Math.random()*100) % 4;
}while(positive == 0 );
switch(positive) {
case 1:
seed = "CLUB";
break;
case 2:
seed = "DIAMOND";
break;
case 3:
seed ="SPADE";
break;
case 4:
seed ="HEART";
break;
}
}
}
}
public class BlackJack {
public static void main(String args[])throws IOException {
System.out.println("Welcome to the BlackJack's table! Press y to start! ");
char flag;
do {
flag = (char)System.in.read();
}while(flag != 'y' );
Table dealer = new Table();
Table player = new Table();
System.out.println("DEALER");
dealer.ReadCards();
System.out.println("PLAYER");
player.ReadCards();
flag = ' ';
System.out.println("Do you want to draw a card? I'll draw until you'll press n");
/*
flag = (char)System.in.read();
while(flag != 'n' ) {
player.DrawCards();
player.ReadCards();
if (player.isOut()) {
System.out.println("YOU LOSE");
System.exit(0);
}
flag = (char)System.in.read();
}
System.out.println("The dealer will draw");
while(dealer.TheSum()<18) {
dealer.DrawCards();
dealer.ReadCards();
if (dealer.isOut()) {
System.out.println("YOU WIN");
System.exit(0);
}
}
*/
System.out.println("WHO WON?");
if (player.isWIN(dealer)){
System.out.println("YOU WIN");
}
else System.out.println("YOU LOSE");
}
}
import java.io.IOException;
类表{
卡片[]手=新卡片[5];
int计数器=1;
表({
对于(int i=0;i这里是一个修复循环问题的示例,但是游戏逻辑可能存在一些问题,这超出了本问题的范围
public static void main(String args[]) throws IOException {
Scanner s = new Scanner(System.in);
System.out.println("Welcome to the BlackJack's table! Press y to start! ");
char flag;
do {
flag = (char) s.nextLine().charAt(0);
} while (flag != 'y');
Table dealer = new Table();
Table player = new Table();
System.out.println("DEALER");
dealer.ReadCards();
System.out.println("PLAYER");
player.ReadCards();
flag = ' ';
System.out.println("Do you want to draw a card? I'll draw until you'll press n");
while (true) {
flag = s.nextLine().charAt(0);
if (flag == 'n') {
break;
}
player.DrawCards();
player.ReadCards();
if (player.isOut()) {
System.out.println("YOU LOSE");
System.exit(0);
}
System.out.println("The dealer will draw");
while (dealer.TheSum() < 18) {
dealer.DrawCards();
dealer.ReadCards();
if (dealer.isOut()) {
System.out.println("YOU WIN");
System.exit(0);
}
}
System.out.println(String.format("The dealer has finished drawing. Dealers score %d. Your score %d", dealer.TheSum(), player.TheSum()));
}
System.out.println("WHO WON?");
if (player.isWIN(dealer)) {
System.out.println("YOU WIN");
} else {
System.out.println("YOU LOSE");
}
}
只是一个问题,这是作业?还是个人学习。你应该看看个人学习,我多年来一直避免使用java,一周前我买了一本书,现在我在这里。我主要想知道我是否使用了错误的I/O方法,因为I/O方法太多了,我不明白为什么它会在我开始通信的地方跳过我的请求我已经错过了C++、C、python。如果代码太多,我可以问I/O,但是扫描器不工作,在按下“Y”之后,我就在无限循环中,BuffeldEdPixSt流有同样的效果。这些是我看到的建议。是的,可能在游戏中有一些问题,毕竟我自己几乎不知道游戏。我当时想的更简单的事情。这个程序在这个循环中仍然存在问题,如果我按下n以外的键,它就会崩溃,n跳过庄家的回合。我想知道为什么输入字符这么难。你必须输入一个字符,如果你只按enter键,你会得到超出范围的索引,因为它使用字符(0),它将查看第一个字符。
Welcome to the BlackJack's table! Press y to start!
y
DEALER
The card 1 is 8 CLUB.
PLAYER
The card 1 is 2 SPADE.
Do you want to draw a card? I'll draw until you'll press n
n
WHO WON?
YOU LOSE