2014-01-20 2 views
0

그래서 저는 "S, M, A, R, T"라는 글자로 채워진 텍스트 기반 게임을 만들고 있습니다. 이 게임을 이기기 위해 각 행과 열에는 5 개의 주어진 문자 중 1 개만 포함될 수 있습니다. (이 게임은 스도쿠 게임과 비슷합니다.) 게임은 완벽하게 작동하지만 사용자가 글자를 넣을 행과 열을 입력하면 보드에 나타나지 않습니다. 아무도 나를 도울 수 없습니까? 미리 감사드립니다. (참고 : 나는 아직도 자바 코딩에 새로운 오전)글자가 출력에 나타나지 않습니까?

import java.util.Scanner; 

public class SmartPuzzleProgram{ 
    static Scanner keyboard = new Scanner(System.in); 
    static char[][] table = new char[5][5]; 

    public static void main (String[] args){ 
    Board(table); 

    int i = 0; 
    while (i < 5){ 
     if( ((int)table[i][0] + (int)table[i][1] + (int)table[i][2] + (int)table[i][3] + (int)table[i][4]) != 391 ){ 
     Move(); 
     Board(table); 
     } 
      else 
       i++; 
      } 

    int j = 0; 
    while (i < 5){ 
     if( ((int)table[0][j] + (int)table[1][j] + (int)table[2][j] + (int)table[3][j] + (int)table[4][j]) != 391 ){ 
     Move(); 
     Board(table); 
     } 
      else 
       j++; 
      } 

    System.out.println("Congratulations! You must be SMART."); 
    } 

     public static void Move(){ 

     // Ask for user's input 
     System.out.print("Enter a row (1-5): "); 
     int r = keyboard.nextInt(); 
     System.out.print("Enter a column (1-5): "); 
     int c = keyboard.nextInt(); 
     System.out.print("Enter a letter (S,M,A,R or T): "); 
     char L = keyboard.next().toUpperCase().charAt(0); 

     if (L == 'S' || L == 'M' || L == 'A' || L == 'R' || L == 'T') { 
     table[r-1][c-1] = L; 
     } 
      else 
       // Error check 
       System.out.println("Invalid letter. Use 'S', M', 'A', 'R' or 'T'"); 
      } 

     public static void Board(char[][] t){ 

     // Given variables 
     t[0][0] = 'S'; 
     t[0][1] = 'M'; 
     t[0][2] = 'A';  
     t[0][3] = 'R'; 
     t[0][4] = 'T'; 

     t[1][0] = ' '; 
     t[1][1] = 'T';  
     t[1][2] = 'S'; 
     t[1][3] = 'M';   
     t[1][4] = ' '; 

     t[2][0] = ' ';    
     t[2][1] = ' '; 
     t[2][2] = 'R'; 
     t[2][3] = ' '; 
     t[2][4] = 'S'; 

     t[3][0] = ' ';  
     t[3][1] = 'S'; 
     t[3][2] = 'M'; 
     t[3][3] = ' '; 
     t[3][4] = ' '; 

     t[4][0] = ' ';   
     t[4][1] = ' ';   
     t[4][2] = 'T'; 
     t[4][3] = 'S'; 
     t[4][4] = ' '; 

     // Prints out the board  
     System.out.println(" 1 2 3 4 5 "); 
     System.out.println(" ---+---+---+---+--- "); 
     System.out.println("1 | " + t[0][0] + " | " + t[0][1] + " | " + t[0][2] + " | " + t[0][3] + " | " + t[0][4] + " |"); 
     System.out.println(" ---+---+---+---+--- "); 
     System.out.println("2 | " + t[1][0] + " | " + t[1][1] + " | " + t[1][2] + " | " + t[1][3] + " | " + t[1][4] + " |"); 
     System.out.println(" ---+---+---+---+--- "); 
     System.out.println("3 | " + t[2][0] + " | " + t[2][1] + " | " + t[2][2] + " | " + t[2][3] + " | " + t[2][4] + " |"); 
     System.out.println(" ---+---+---+---+--- "); 
     System.out.println("4 | " + t[3][0] + " | " + t[3][1] + " | " + t[3][2] + " | " + t[3][3] + " | " + t[3][4] + " |"); 
     System.out.println(" ---+---+---+---+--- "); 
     System.out.println("5 | " + t[4][0] + " | " + t[4][1] + " | " + t[4][2] + " | " + t[4][3] + " | " + t[4][4] + " |"); 
     System.out.println(" ---+---+---+---+--- "); 
     } 
} 
+0

글쎄, 당신이 t 배열마다 시간을 재 - 초기화 유지하는 것이 나타납니다 널(), 안돼? – OldProgrammer

+0

그래, 나는 코드를하는 동안 그것에 주목하지 않았다. 나는 그것을 알아 들었다! @OldProgrammer를 지적 해 주셔서 감사합니다 – user3000345

답변

1

문제는 당신이 테이블에 값이있는 Board() 메소드를 호출 할 때마다 할당하는 것입니다, 당신은 그들에게 한 시간을 초기화 할 수 있습니다.

public static void initialize() 
{ 
    System.out.println("here"); 
    table[0][0] = 'S'; 
    table[0][1] = 'M'; 
    table[0][2] = 'A'; 
    table[0][3] = 'R'; 
    table[0][4] = 'T'; 

    table[1][0] = ' '; 
    table[1][1] = 'T'; 
    table[1][2] = 'S'; 
    table[1][3] = 'M'; 
    table[1][4] = ' '; 

    table[2][0] = ' '; 
    table[2][1] = ' '; 
    table[2][2] = 'R'; 
    table[2][3] = ' '; 
    table[2][4] = 'S'; 

    table[3][0] = ' '; 
    table[3][1] = 'S'; 
    table[3][2] = 'M'; 
    table[3][3] = ' '; 
    table[3][4] = ' '; 

    table[4][0] = ' '; 
    table[4][1] = ' '; 
    table[4][2] = 'T'; 
    table[4][3] = 'S'; 
    table[4][4] = ' '; 
} 

을 그리고 main()의 시작 부분에 호출 :이를 위해 새로운 방법을 만들고, 이제 initialize()을 가정 해 봅시다

public static void main(String[] args) 
{ 
    initialize(); 
    Board(table); 
    ... 
} 
+0

나를 설명 해주셔서 정말 고마워요! 이제 알겠습니다 :) – user3000345

관련 문제