Java 在简单的black jack程序上有点卡住了

Java 在简单的black jack程序上有点卡住了,java,blackjack,Java,Blackjack,所以我在做一个黑杰克程序,我有点卡住了。我要提醒大家,我对编程真的很陌生,而且,我是项目的中间人……所以有一些松散的结尾和未使用的变量,还有一些不必要的逻辑(用于测试),但以下是我需要帮助的地方 1) 我使用Math.random来充当一副牌,起初它似乎工作正常……但在第二次命中后,很明显,前一张牌的值将替换为当前牌的值。如果你画了一个3,5,9…数组将读取9,9,9而不是3,5,9 2) Eclipse指出变量user_total(在方法user_hand中)和dealer_total(在方法

所以我在做一个黑杰克程序,我有点卡住了。我要提醒大家,我对编程真的很陌生,而且,我是项目的中间人……所以有一些松散的结尾和未使用的变量,还有一些不必要的逻辑(用于测试),但以下是我需要帮助的地方

1) 我使用Math.random来充当一副牌,起初它似乎工作正常……但在第二次命中后,很明显,前一张牌的值将替换为当前牌的值。如果你画了一个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);
}

}


}
公共类21点
{
最终静态积分=17;
最终静态int最大卡值=10;
静态随机数=新随机数();
公共静态void main(字符串[]args)引发IOException
{
缓冲读取器;
in=新的BufferedReader(新的InputStreamReader(System.in));
点击次数的整数=0;
布尔值仍在播放=真;
(还在玩)
{   
//跟踪点击数----------------------------------
点击次数++;
System.out.println(“这是绘图:+点击次数);
System.out.println(“”);
//获取用户
int user_total=用户手(点击次数);
//获得经销商卡----------------------------------------
int经销商总数=经销商手(点击次数);
//询问用户是否想打或站
System.out.println(“\n您想打还是站?\n输入:H打\n输入:S站”);
char hit_或_stand=in.readLine().charAt(0);
char hit_或_stand_case=Character.toLowerCase(hit_或_stand);
if(hit_或stand_case=='s')
{               
//比较卡片
赢家(用户总数、经销商总数);
}
//继续游戏提示--------------------------------
System.out.println(“你还在玩吗?输入1表示是,输入2表示否”);
int still_playing_response=Integer.parseInt(in.readLine());
如果(仍在播放,响应=1)
{
仍然在玩=真的;
}
其他的
{
仍然在玩=真的;
System.out.println(“再见”);
}
}//当结束时
}//主端
公共静态int生成一张卡()
{
扫描仪输入=新扫描仪(System.in);
int card=随机数。nextInt(最大卡值)+1;
回程卡;
}
公共静态整数用户手(整数点击次数)
{   
int user_card=生成_a_card();
System.out.println(“您绘制:+用户卡”);
int user_current_hand[]=新int[点击次数];

对于(int i=0;idealer\u total和user\u total在for循环中声明,并在返回之前超出范围。这就是eclipse抱怨的原因。

dealer\u total和user\u total在for循环中声明,并在返回之前超出范围。这就是eclipse抱怨的原因。

user\u total仅访问on在该循环内。返回循环外的user_total。因为Eclipse无法识别该变量。因为变量超出范围

这样宣布,

public static int user_hand(int number_of_hits)
{   
     int user_total=0;
     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");


           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;
publicstaticint-user\u-hand(点击次数的整数)
{   
int user_total=0;
int user_card=生成_a_card();
System.out.println(“您绘制:+用户卡”);
int user_current_hand[]=新int[点击次数];

for(int i=0;iuser_total仅在该循环内访问。您在循环外返回user_total。因为Eclipse无法识别该变量。因为变量超出范围

这样宣布,

public static int user_hand(int number_of_hits)
{   
     int user_total=0;
     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");


           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;
publicstaticint-user\u-hand(点击次数的整数)
{   
int user_total=0;
int user_card=生成_a_card();
System.out.println(“您绘制:+用户卡”);
int user_current_hand[]=新int[点击次数];

对于(inti=0;i,正如我的Janvo所指出的,代码中存在范围问题 看看

在一对花括号内定义的任何内容

{ }

就在它的内部可见。

正如我的Janvo所指出的,您的代码中存在范围问题 看看

在一对花括号内定义的任何内容

{ }

仅在其内部可见。

不要将未嵌入的代码发布到SO!这是一个无法读取的混乱。“很抱歉,如果格式怪异,stackoverflow正在抱怨某些内容”所以不“抱怨”,它提供错误消息。消息是什么?不要将未嵌入的代码发布到SO!这是一个无法读取的混乱。“很抱歉,如果格式奇怪,stackoverflow正在抱怨一些事情”因此不“抱怨”,它提供错误消息。消息是什么?软件工具不“抱怨”,它们只是“报告”。请改进使用的术语。软件工具不“抱怨”,它们只是“报告”。请改进使用的术语。