2014-02-07 1 views
0

주어진 행을 #, 열 수, 각 사각형의 크기, 필러 문자의 형태로 주어진 사용자 입력으로 사용자 정의 체크 보드를 만들려고합니다. 루프를 만들었지 만, 1 1 1 바둑판을 제대로 얻을 수 없습니다. 저는 필러 문자를 어떻게 나누어 문자가 지금처럼 선이 아닌 개별 사각형으로 만드는지에 집착하고 있습니다.중첩 루프가있는 Java 사용자 정의 바둑판

import java.util.Scanner; 

public class Checker { 

    public static void main(String[] args) { 
     int col, row, size; 
     char filler; 
     System.out.println("Please enter 3 numbers and a character."); //output 
     Scanner scan = new Scanner(System.in); //input 

     row = scan.nextInt(); 
     col = scan.nextInt(); 
     size = scan.nextInt(); 
     filler = scan.next().charAt(0); // defined variables 

     int r, c, i = 0, j = 0, k = 0; 

     for (r = 1; r <= row; r++) { 
      for (c = 1; c <= col; c++) { 
       do { 
        if ((r % 2 != 0 && c % 2 != 0) || (r % 2 == 0 && c % 2 == 0)) { 
         do { 
          System.out.print(filler); 
          i++; 
         } 
         while (i < size); 
         i = 0; 

        } else { 
         do { 
          System.out.print(" "); 
          j++; 
         } 
         while (j < size); 
         j = 0; 
        } 
        k++; 
       } while (k < size); 
       k = 0; 

      } 
      System.out.println("\n"); 
     } 
     System.out.println("\nHave a nice day. Goodbye."); 

    } 
} 


3 3 3 x would give: 
    xxx xxx 
    xxx xxx 
    xxx xxx 
     xxx 
     xxx 
     xxx 
    xxx xxx 
    xxx xxx 
    xxx xxx 
+0

문제 솔기가 처음 DO-동안으로 다음과 같이

난 당신이 마무리의 자유를 촬영했습니다. 'k'는 정확히 무엇입니까? –

+0

고마워, 나는 그걸 꺼냈다.하지만 어떻게하면 사각형을 만들기 위해 지금 줄을 복제 할 수 있을까? – user3285579

+0

사각형으로 무엇을 의미하는지 설명 할 수 있습니까? 아마도 당신이보기를 원하는 부분을 수동으로 타이핑 한 것을 포함 할 것입니다. –

답변

0

첫 번째 do-while은 막 멀리까지 삽입됩니다. 두 번째 for 루프 앞에 와야합니다. 또한 println 내부의 "\ n"을 제거했습니다. 그런 식으로 원했을 수도 있습니다. 확실하지 않습니다. 입력 3 3 3 X

import java.util.*; 

public class Checker{ 

    public static void main(String[] args) { 
    int col, row, size; char filler; 
    System.out.println("Please enter 3 numbers and a character."); //output 
    Scanner scan = new Scanner(System.in); //input 

    row = scan.nextInt(); 
    col = scan.nextInt(); 
    size = scan.nextInt(); 
    filler = scan.next().charAt(0); // defined variables 

    int r,c,i=0,j=0,k=0; 

    for (r=1; r<=row; r++) { 
     do { 
     for (c=1; c<=col; c++){ 
      if ((r % 2 != 0 && c % 2 !=0) || (r %2 == 0 && c % 2 == 0)){ 
      do { 
       System.out.print(filler); 
       i++; 
      } 
      while (i<size); 
      i = 0; 

      } 


      else { 
      do 
      { System.out.print(" "); 
       j++; 
      } 
      while (j<size); 
      j = 0; 
      } 
     } 
     k++; 
     System.out.println(); 
     } 
     while (k < size); 
     k = 0; 
    } 
    System.out.println("\nHave a nice day. Goodbye."); 
    } 

} 

는 다음을 생성.

XXX XXX 
XXX XXX 
XXX XXX 
    XXX 
    XXX 
    XXX 
XXX XXX 
XXX XXX 
XXX XXX 

결과가 원하는 것과 일치하지 않습니다. 그러나 3X3이기 때문에 그것이 맞다고 믿습니다. 이를 변경하려면 행의 길이를 늘리면됩니다. (열 수 증가)

+0

그게 다야, 정말 고마워! – user3285579

+0

@ user3285579 답변으로 표시해주세요. –

0

거의 사용했습니다. 상자를 인쇄 할 때 논리가 실패합니다. 현재 완료 될 때까지 필러 크기를 인쇄합니다. 그런 다음 공백을 인쇄합니다. 그러나 문제는 행을 완료 할 때까지 다음 행으로 이동하지 않는다는 것입니다. 실제로는 여러 행의 첫 번째 행을 처리해야하며 다음 행으로 이동하기 전에 새 행 SIZE 배로 값을 이동해야합니다.

import java.util.*; 

public class Main{ 

public static void main(String[] args) { 
int col, row, size; char filler; 
System.out.println("Please enter 3 numbers and a character."); //output 
Scanner scan = new Scanner(System.in); //input 

row = scan.nextInt(); 
col = scan.nextInt(); 
size = scan.nextInt(); 
filler = scan.next().charAt(0); // defined variables 

int r,c,i=0,j=0,k=0; 

for(r=1; r<=row; r++){ 
    for(int boxSize = 0; boxSize < size; boxSize++){ 
     for(c=1; c<=col; c++){ 

       if((r % 2 != 0 && c%2 !=0) || (r % 2 == 0 && c % 2 == 0)){ 
        do{ 
         System.out.print(filler); 
         i++; 
        } 
        while(i < size); 
        i = 0; 
       } 
       else{ 
        do{ 
         System.out.print(" "); 
         j++; 
        } 
        while(j < size); 
        j = 0; 
       } 
     } 
     System.out.println(); 
    } 
    System.out.println("\n"); 
} 
System.out.println("\nHave a nice day. Goodbye."); 
} 
}