2014-02-07 277 views
0

Java自定义棋盘我试图与给定用户输入一个自定义的棋盘,在形式(#行,列#,每平方米的大小,填充字符)。 我已经做了我的循环,但我无法正确获得更多的1 1 1棋盘格。 我被困在如何分配填充字符,使个人广场,而不是字符变得像他们现在的线。与嵌套循环

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

问题接缝是第一个做,而。 'k'用于什么? –

+0

谢谢,我把它拿出来了,但是我现在怎么复制这些线使它们变成正方形? – user3285579

+0

你能解释一下你的意思吗?也许包括一些手动输入你想要它看起来像什么? –

回答

0

第一个do-while只是嵌入到远处。它应该在第二个循环之前出现。另外,我删除了println中的“\ n”。你可能想这样,我不知道。

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."); 
    } 

} 

与输入3 3 3 X 产生以下。

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

输出不符合你想要的。但我相信这是正确的,因为它是3X3。如果你想改变它,你只需要增加行的长度。 (增加列数)

+0

就是这样,非常感谢! – user3285579

+0

@ user3285579你可以请标记为答案? –

0

你差不多已经有了。打印盒子时,您的逻辑失败。你现在已经打印了填充SIZE时间直到完成,然后打印空格等等。但是问题在于,在你完成这一行之前,你不会移动到下一行。实际上,您需要在多行中处理第一行,并在移动到下一行之前移动到新行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."); 
} 
}