2012-01-30 2 views
0

그래서 블랙 잭 프로그램에서 일하고 있어요. 조금 붙어 있습니다. 나는 정말로 프로그래밍에 익숙하지 않다는 것을 모두에게 경고 할 것이다. 또한, 나는 중간 프로젝트이다 .... 그래서 약간의 느슨한 목적과 사용되지 않는 변수, 그리고 불필요한 로직 (테스트 용)이있다.하지만 도움이 필요한 것이있다. 와.간단한 블랙 잭 프로그램에 조금 붙어 있습니다

1) 저는 Math.random을 사용하여 카드 덱으로 사용하고 있습니다. 처음에는 제대로 작동하는 것처럼 보였습니다 ...하지만 두 번째 히트가 끝나면 이전 카드 값이 현재 카드 값으로 대체됩니다. 카드 가치. 만약 당신이 3,5,9을 그린다면 배열은 3, 5, 9 대신 9,9,9를 읽습니다.

2) 이클립스는 변수 user_total (메소드 user_hand) 및 dealer_total (메소드 dealer_hand)을 "변수로 해결할 수 없으므로"main에 반환 할 수 없음을 나타냅니다. 나는 왜 그들이 정상적인 지 알아낼 수 없다. 이 이상한 형식 않다면

죄송합니다, 유래는 ... 뭔가에 대해 불평 여기 내 코드입니다 : 루프

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

들여 쓰기 코드를 게시하지 마십시오! 그것은 읽을 수없는 혼란입니다. * "유감스럽게 생각한다면 형식이 이상합니다. stackoverflow가 뭔가에 대해 불평하고있었습니다."* 그래서 '오류가 발생하지 않습니다'라는 오류 메시지가 표시됩니다. 메시지는 무엇입니까? –

답변

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

2

dealer_total 및 user_total가 내에서 선언하고 반환 할 전 범위의 외출. 그것이 일식이 불평하는 이유입니다.

+0

소프트웨어 도구는 '불평'하지 않으며 간단히 '보고'합니다. 사용 된 용어를 개선하십시오. –

1

쌍 내에 정의 무엇이든 this

를 살펴 보자 내 Janvo ​​ 을 지적 당신은 당신의 코드에서 범위의 문제가 의 중괄호

{ } 

은 그 안쪽에 표시됩니다.

관련 문제