2012-12-06 2 views
0

이 클래스의 클래스를 작성하려고합니다.이 클래스는 객체가 움직이는 보드를 만듭니다. 보드는 네 모서리에 "+"가 있고 "-" 수직으로 가고 "|" 빈 중심으로 가로 :Java toString 오류

+-----+ 
|  | 
|  | 
|  | 
|  | 
+-----+ 

I를, 다른 한편으로는, 중간을 채우고 가장자리 주위 horzontally가는 괄호, 쉼표를 얻을, 나는 이유를 알고하지 않습니다

[ , , , , ] 
[ , , , , ] 
[ , , , , ] 
[ , , , , ] 
[ , , , , ] 

내 프로그램을 맞습니다. 그러나 제 수업에 도움이 필요합니다.

import java.util.Arrays; 
import java.util.Random; 

public class Board { 

    private char [][] theBoard; 

    public Board() { 
     this(10, 25); 
    } 


    public Board (int rows, int cols) { 
     if (rows < 1 || rows>80) { 
      rows = 1; 
     } 
     if (cols<1 || cols > 80) { 
      cols = 1; 
     } 
     theBoard = new char [rows][cols]; 
     for (int row = 0; row < theBoard.length; row++) { 
      for (int col = 0; col < theBoard[row].length; col++) 
       theBoard[row][col] = ' '; 
    } 
    } 

     public void clearBoard() { 
     for (int row = 0; row < theBoard.length; row++) { 
     for (int col = 0; col < theBoard[row].length; col++) { 
     if (theBoard[row][col] < '0' || theBoard[row][col] > '9') { 
     theBoard[row][col] = ' '; 
    } 
    } 
    } 
    } 

      public void setRowColumn(int row, int col, char character) { 
      theBoard[row][col] = character; 
     } 

      public char getRowColumn(int row, int col) { 
      return theBoard[row][col]; 
     } 

     public String toString() { 
    StringBuilder strb = new StringBuilder(); 
    for (char[] chars : theBoard) { 
     strb.append(Arrays.toString(chars) + "\n"); 
    } 
    return strb.toString(); 
    } 
    public static void main(String [] args) 
    { 
     Board aBoard, anotherBoard; 

     System.out.println("Testing default Constructor\n"); 
     System.out.println("10 x 25 empty board:"); 

     aBoard = new Board(); 
     System.out.println(aBoard.toString()); 

     System.out.println(); 

     // And, do it again 
     System.out.println("Testing default Constructor again\n"); 
     System.out.println("10 x 25 empty board:"); 

     anotherBoard = new Board(); 
     System.out.println(anotherBoard.toString()); 

    } // end of method main 
} // end of class 
+0

~ 해당 수정에 대한 루치아이 감사합니다. – user201535

+0

무엇이 오류입니까? toString() 메서드를 변경하십시오. –

+0

그래, 그게 toString 메소드와 관련이있어.하지만 찾을 수없는 것 같아.. <그냥 잘못된 문자를 출력하지만, 내가 가지고 있다고 확신했다 .. – user201535

답변

2

기본 생성자는 배열에 빈 공백을 채 웁니다 (' ').

Arrays.toString() 메서드를 호출하면 열려있는 대괄호를 출력하고 배열 내용 (쉼표로 구분)과 그 뒤에 닫는 대괄호가옵니다. 예를 들어

, 당신은 배열이 있다면 : Arrays.toString(a) 인쇄를 호출

i a[i] 
0 1 
1 2 
3 5 
4 8 

을 : 또 다른 예로서

[1, 2, 5, 8] 

, 당신은 빈 공간으로 가득 배열이 있다면, 당신은 얻을 것 :

[ , , , , , ] 

(공백 표시?)

이것이 출력물을받는 이유입니다.

+0

재미 있고 놀랄 일도 아닙니다! 접근 자 메서드를 통해 필요한 것을 만드는 방법을 알고 있지만이 경우 toString 메서드를 사용하는 방법을 알지 못합니다. – user201535

+0

배열을 반복하고 배열의 각 문자를 문자열에 추가하여'Arrays.toString()'메서드를 사용하지 않아도됩니다. 각 행의 끝에 문자열에 새 행을 입력하십시오. – apnorton

+0

괜찮아, public String toString() { int i; 문자열 temp = 새 문자열 (""); 문자열 줄 = ""; char topBottom = '-'; int k; int row2 = theBoard [0] .length; (k = 0; k user201535