简体   繁体   English

带嵌套循环的Java自定义棋盘

[英]Java custom checkerboard with nested loop

I'm trying to make a custom checkboard with given user input, in the form of (# of rows, # of columns, size of each square, filler character). 我正在尝试以给定的用户输入((行数,列数,每个正方形的大小,填充字符)的形式制作自定义棋盘。 I've made my loop, but I am unable to get more then a 1 1 1 checkerboard properly. 我已经完成循环,但是我无法正常获得更多的1 1 1棋盘。 I am stuck on how to divide the filler characters to make the individual squares rather than the characters becoming lines like they are now. 我坚持如何将填充字符划分为单个正方形,而不是像现在这样将字符变成线条。

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   

The first do-while is just embedded to far. 第一个do-while仅嵌入到很远的地方。 It should come before the second for loop. 它应该出现在第二个for循环之前。 Also, I removed the "\\n" inside of println. 另外,我删除了println中的“ \\ n”。 You might have wanted it that way, im not sure. 您可能不确定是否需要这种方式。

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

}

with the input 3 3 3 X produced the following. 输入3 3 3 X产生以下结果。

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

The output doesnt match what you wanted. 输出与您想要的不匹配。 But I believe it is correct because it is 3X3. 但是我相信它是正确的,因为它是3X3。 If you want to change it you just need to increase the length of the row. 如果要更改它,则只需要增加行的长度即可。 (increase the # of columns) (增加列数)

You almost had it. 你差点就吃了。 Your logic fails when printing the boxes. 打印框时,逻辑失败。 You currently have it printing the filler SIZE times until it's done, then it prints the spaces etc. But the problem is that you don't move to the next line until you're done with that row. 当前,您需要打印填充SIZE次直到完成,然后打印空格等。但是问题是,直到完成该行,您才移至下一行。 In reality you need to process the first row in several lines, moving to a new line SIZE times printing values before moving to the next row. 实际上,您需要处理多行中的第一行,移至新行SIZE乘以打印值,然后再移至下一行。

I've taken the liberty of finishing this for you as follows: 我已为您完成此操作,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM