java - 有点卡在简单的黑 jack 程序上

标签 java blackjack

所以我正在开发一个黑 jack 程序,但我有点卡住了。我会警告大家,我对编程真的很陌生,而且,我正在项目中期......所以有一些松散的结局和未使用的变量,以及一些不必要的逻辑(用于测试),但这就是我需要帮助的内容与。

1) 我使用 Math.random 来充当一副牌,起初它似乎工作正常......但在第二次点击之后,很明显以前的牌值被当前的牌取代值(value)。如果你画了一个 3,5,9...数组将读取 9,9,9 而不是 3,5,9。

2) Eclipse 指出变量 user_total(在方法 user_hand 中)和 Dealer_total(在方法 Dealer_hand 中)无法返回到 main,因为它们“无法解析为变量”。我不明白为什么,据我所知,它们是正常的整数。

抱歉,如果它的格式很奇怪,stackoverflow 正在提示一些事情...... 这是我的代码:

public class blackJack 
{
final static int DEALER_STAND_THRSH = 17;
final static int MAX_CARD_VALUE = 10;
static Random randomNumbers = new Random();

public static void main(String[] args)throws IOException
{
BufferedReader in;
in = new BufferedReader (new InputStreamReader (System.in));
int number_of_hits=0;
boolean still_playing=true;
while (still_playing)
{   
//tracks number of hits----------------------------------

number_of_hits ++;
System.out.println("this is draw  : " + number_of_hits);
System.out.println(" ");

// gets users 
int user_total=user_hand(number_of_hits);
//get dealers card----------------------------------------
int dealer_total=dealer_hand(number_of_hits);
//ask user if they'd like to hit or stand
System.out.println("\nwould you like to hit or stand?\nenter:H to hit\nenter:S to stand");
char hit_or_stand=in.readLine().charAt(0);
char hit_or_stand_case = Character.toLowerCase(hit_or_stand);

if (hit_or_stand_case=='s')
{               
//compare cards
who_wins(user_total,dealer_total );
}

// continue game prompt --------------------------------
System.out.println("are you still playing? enter 1 for yes. 2 for no.");
int still_playing_response = Integer.parseInt(in.readLine());

if(still_playing_response==1)
{
still_playing=true;
}
else
{
still_playing=true;
System.out.println("goodbye");
}
}//while end
}//main end

public static int generate_a_card()
{

Scanner input = new Scanner(System.in);

int card=randomNumbers.nextInt(MAX_CARD_VALUE) +1 ;

return card;

}

public static int user_hand(int number_of_hits)
{   
int user_card=generate_a_card();
System.out.println( "you drew: " + user_card );
int user_current_hand [] = new int [number_of_hits];

for (int i=0; i<user_current_hand.length; i++)
{
user_current_hand [i]= user_card;
System.out.println( user_card + " has been stored in index " + i + " of user_current_hand array");
int user_total=0;

for(int j=0; j<user_current_hand.length; j++)
{
user_total+= user_current_hand[i];

if (user_total>21)
{
System.out.println("you have exeeded 21, you lose!");
}//if end
}//nested for loop

}//first for loop


return user_total;


}//end user_hand method

public static int dealer_hand(int number_of_hits )

{       
System.out.println("-----------------------------------------\n");
System.out.println("now for the dealers draw");
System.out.println(" ");

int dealer_card=generate_a_card();
System.out.println( "the dealer drew: " + dealer_card);
int dealer_current_hand [] = new int [number_of_hits];

for (int i=0; i<dealer_current_hand.length; i++)
{
dealer_current_hand [i]= dealer_card;
System.out.println( dealer_card + " has been stored in index " + i + " dealer_current_hand array");

int dealer_total=0;
for(int j=0; j<dealer_current_hand.length; j++)
{
dealer_total+= dealer_current_hand[i];

if (dealer_total>21)
{
System.out.println("the dealer has exeeded 21, you win!");
}//if end
}//nested for loop
}//first for loop

return dealer_total;

}//end main

public static void who_wins(int user_total, int dealer_total  )
{

if(user_total > dealer_total)
{
System.out.println("congradulations, you won!\nYour score: " + user_total + "\ncomputer's score: " + dealer_total);
}
else if(user_total < dealer_total)
{
System.out.println("Sorry, you lost.\nYour score: " + user_total + "\ncomputer's score: " + dealer_total);
}
else
{
System.out.println("this round was a tie!\nYour score: " + user_total + "\ncomputer's score: " + dealer_total);
}

}


}

最佳答案

dealer_total 和 user_total 在 for 循环内声明,并在返回之前超出范围。这就是 eclipse 提示的原因。

关于java - 有点卡在简单的黑 jack 程序上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9059890/

相关文章:

javascript - window.setTimeout() 问题

java - 如何使用 Arrays.sort 输出

java - Jenkins:config.jelly 中的 validateButton 用于没有描述符的类

java - android.os.NetworkOnMainThreadException 里面新线程

c++ - 初学者二十一点游戏

c - 对我的简单二十一点程序的建议/改进

java - ScheduledExecuterService.scheduleAtFixedRate 创建多个线程池 - Android

java - 如何比较原始类型中忽略大小写的字符

java - 用java编写二十一点和纸牌程序时遇到问题

python - ValueError 与 TypeError,为什么?