带有嵌套循环的Java自定义棋盘
我正在尝试使用给定的用户输入,以(#行、#列、每个正方形的大小、填充字符)的形式创建一个自定义检查板。 我已经完成了循环,但我无法正确地获得超过1个棋盘。 我被困在如何划分填充字符,使个人的广场,而不是像他们现在的字符变成线带有嵌套循环的Java自定义棋盘,java,loops,Java,Loops,我正在尝试使用给定的用户输入,以(#行、#列、每个正方形的大小、填充字符)的形式创建一个自定义检查板。 我已经完成了循环,但我无法正确地获得超过1个棋盘。 我被困在如何划分填充字符,使个人的广场,而不是像他们现在的字符变成线 import java.util.Scanner; public class Checker { public static void main(String[] args) { int col, row, size; char
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
import java.util.Scanner;
公共类检查器{
公共静态void main(字符串[]args){
整数列、行、大小;
煤焦填料;
System.out.println(“请输入3个数字和一个字符”);//输出
Scanner scan=新扫描仪(System.in);//输入
row=scan.nextInt();
col=scan.nextInt();
size=scan.nextInt();
filler=scan.next().charAt(0);//定义的变量
int r,c,i=0,j=0,k=0;
for(r=1;r第一个do while刚刚嵌入到far。它应该位于第二个for循环之前。此外,我删除了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.");
}
}
输出与您想要的不匹配。但我认为它是正确的,因为它是3X3。如果您想更改它,只需增加行的长度。(增加列的#)您几乎已经拥有了它。打印方框时,您的逻辑失败。您当前让它打印填充大小,直到完成为止,然后再打印空格等。但问题是,在完成该行之前,您不会移动到下一行。实际上,您需要处理几行中的第一行,移动到新的行大小mes在移动到下一行之前打印值
我冒昧地为您完成以下内容:
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.");
}
}
import java.util.*;
公共类主{
公共静态void main(字符串[]args){
int col,row,size;字符填充符;
System.out.println(“请输入3个数字和一个字符”);//输出
Scanner scan=新扫描仪(System.in);//输入
row=scan.nextInt();
col=scan.nextInt();
size=scan.nextInt();
filler=scan.next().charAt(0);//定义的变量
int r,c,i=0,j=0,k=0;
对于(r=1;r问题接缝是第一个做的。k
到底是用来做什么的?谢谢,我把它拿出来了,但是我现在怎么复制这些线使它们成为正方形呢?你能解释一下你所说的正方形是什么意思吗?也许包括一些手动输入的你想要的形状吗?3 x将输出3行3列的图形3乘3格,我把它包括在我的原始博文中。你能把它作为code
放在你的问题中,这样格式就保持一致吗?谢谢。
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.");
}
}