2012-01-30 51 views
0

所以我正在做一個黑色的插孔程序,我有點卡住了。我會提醒大家,我對編程真的很陌生,另外,我在項目中期......所以有一些鬆散的末端和未使用的變量,以及一些不必要的邏輯(用於測試),但這裏是我需要幫助的用。有點卡在簡單的黑色插孔程序

1)我使用Math.random作爲一副撲克牌,起初它似乎可以正常工作......但是在第二次擊中之後,很明顯,之前的撲克牌值被替換爲當前卡值。如果你畫了3,5,9 ...陣列將讀取9,9,9而不是3,5,9。 2)Eclipse已經指出變量user_total(in method user_hand)和dealer_total(in method dealer_hand)無法返回到main,因爲它們「無法解析爲變量」。據我所知,我不明白爲什麼,它們是正常的int。

對不起,如果它的格式怪異,計算器是滿腹牢騷...... 這裏是我的代碼:

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); 
} 

} 


} 
+1

不要將無意的代碼發佈到SO!這是一個難以理解的混亂。 *「抱歉,如果它的格式奇怪,stackoverflow是抱怨某事」* SO不'抱怨',它提供了錯誤信息。信息是什麼? – 2012-01-30 05:52:00

回答

1

user_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; 

}

+0

謝謝Kushan,那工作:) – ThisBetterWork 2012-01-30 06:14:46

2

dealer_total和user_total都內聲明的for循環,走出去的要返回之前的範圍。這就是爲什麼日食抱怨。

+0

軟件工具不「抱怨」,他們只是「報告」。請改善使用的術語。 – 2012-01-30 05:59:11

1

你有範圍的問題在你的代碼,指出我的Janvo 採取在this

看看無論是對內的定義花括號

{ } 

只是在裏面可見。