所以我的程序应该输出一个类似联盟杰克的模式,例如:
3 = , 7 =
0X0 0 X 0
XXX 0 X 0
0X0 0X0
XXXXXXX
0X0
0 X 0
0 X 0
这是我到目前为止的代码...... 有人能找到什么问题吗?我真的不知道什么是错的,它输出的图案类似,但空格都错了
import java.util.*;
public class uj {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int input;
int counter2=1;
int counter1=1;
do{
System.out.println("Enter size(must be odd): ");
input = sc.nextInt();
}while(input/2==0);
int half = (input/2)+1;
while(counter1<=input+1){
while(counter2<=input+1){
if(counter2==counter1 || counter2==(input-counter1)){
System.out.print("0");
}else if(counter2==half && counter1!=half){
System.out.print("x");
}else if(counter1==half){
System.out.print("x");
}else{
System.out.print(" ");
}
counter2++;
}
System.out.println("");
counter1++;
counter2=0;
}
}
}
答案 0 :(得分:1)
最好打印NxN方形字符。
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int input;
do {
System.out.println("Enter size(must be odd): ");
input = sc.nextInt();
} while(input%2 == 0);
int half = input/2 + 1;
for( int iRow = 1; iRow <= input; ++iRow ){
for( int iCol = 1; iCol <= input; ++iCol ){
// Highest precedence: middle row, middle column
if( iRow == half || iCol == half ){
System.out.print( 'X' );
} else
// next, the diagonals
if( iRow == iCol || input - iCol + 1 == iRow ){
System.out.print( '0' );
} else {
// the rest is white space
System.out.print( ' ' );
}
}
System.out.println();
}
}