1
2 import java.util.Scanner;
3
4 public class WordGameApp {
5 public static void main(String args[]){
6
7 Scanner sc = new Scanner(System.in);
8 int numPlayer;
9 String namePlayer;
10 String sayWord;
11
12 System.out.println("게임에 참가하는 인원은 몇몇입니까?");
13 numPlayer=sc.nextInt();
14
15 Player Gamer[] = new Player[numPlayer];
16 for(int i=0; i<numPlayer; i++){
17 System.out.print("참가자의 이름을 입력하시오>>");
18 namePlayer=sc.next();
19 Gamer[i].SetName(namePlayer); <<<< 여기서 오류 나드라 ㅇㅇ
20 Gamer[i].InitPlayer("아버지");
21 }
22
23 System.out.println("시작하는 단어는 아버지 입니다.");
24
25 int i=0;
26 do{
27 if(i>numPlayer)
28 i=0;
29 System.out.print(Gamer[i].GetName()+">>");
30 sayWord=sc.next();
31 Gamer[i].SetWord(sayWord);
32 }while(Gamer[i].CmpWord());
33
34 System.out.println(Gamer[i].GetName()+"이 졌습니다");
35
36 }
37
38 }
39
40 class Player{
41 private String name;
42 private String sayWord;
43 private static char lastWord;
44
45 public Player(String word){
46 this.sayWord=word;
47 int lastIndex=word.length()-1;
48 lastWord=word.charAt(lastIndex);
49 }
50 private static void SetLastWord(char changWord){
51 lastWord=changWord;
52 }
53 public void InitPlayer(String word){
54 this.sayWord=word;
55 int lastIndex=word.length()-1;
56 lastWord=word.charAt(lastIndex);
57 }
58 public void SetName(String name){
59 this.name=name;
60 }
61 public void SetWord(String word){
62 this.sayWord=word;
63 }
64 public Boolean CmpWord(){
65 char firstChar = this.sayWord.charAt(0);
66 if(firstChar != lastWord)
67 return false;
68 SetLastWord(firstChar);
69 return true;
70 }
71 public String GetName(){
72 return this.name;
73 }
74
75 }
끝말잇기 게임이다... 여기 고수들 많은거 같은데, 왜 오류나는지 좀 봐주라 ㅡㅡ
참고로 이거
명품 JAVA PROGRAMMING 228p 챌린지 문제인데, 자존심 상한다.
더 핥아라
1 시발놈이? 지적해주고 핥으랄면 핥겠음ㅇㅇ
어떤오류
Exception in thread "main" java.lang.NullPointerException at WordGameApp.main(WordGameApp.java:56) 내가 위치를 조금 바꿔서 라인이 좀 다르게 나오는데 내가 밑줄친 부분인듯 ㅇㅇ
아. 알았다. Player Gamer[] = new Player[numPlayer]; 이렇게 Player 배열을 할당해주긴한데
이건 할당해주는 것뿐이지 객체가 생성된것은 아님 ..
그러니까 Gamer[i] = new Player~~ 이런식으로 들어가야만 함.
NullPointerException이 나온것도 객체가 비어있어서 나타난 오류임.
오오오 ㅡㅡ 땡큐 베리 머치 ㅋㅋㅋㅋ 감사요