Java程序打印x&y坐标图并显示标绘点



我必须打印一个6乘6的图并绘制给定点。这是x=2和y=3的示例:

5 . . . . . 
4 . . . . . 
3 . x . . . 
2 . . . . . 
1 . . . . . 
0 1 2 3 4 5 

我的代码:

public static String[][] plotPoint(String[][] plot){
int x=0, y=0;
Scanner sc = new Scanner(System.in);
boolean bool = true;
while(bool) {
System.out.println("Enter x coordinate between 0 and 5: ");
x = sc.nextInt();
System.out.println("Enter y coordinate between 0 and 5: ");
y = sc.nextInt();
if(x>=5||x<0||y>=5||y<0) {
System.out.println("The coordinates entered exceed the limit.");
}else {
x = x-1;
bool=false;
}
}
for(int i=0; i<6; i++) {
for(int j = 0; j<6; j++) {
if(i==x && j==y) {
plot[x][y]="x";//assigning "x" to coordinates
break;
}
}
}
return plot;
}
public static void main(String [] args) {
int h = 5;
Scanner sc = new Scanner(System.in);
String [][] strArr = new String[6][6];
char c='.';
for(int i = 0; i<6; i++) {
for(int j = 0; j<6; j++) {
if(j==0||i==5) {
strArr[i][j] = Integer.toString(h);
if(i==5)h++;
else h--;
}
else {
strArr[i][j]= Character.toString(c);
}
}
}
String[][] nStrArr = new String[6][6];
nStrArr = plotPoint(strArr);
for(int i = 0; i<6; i++) {
for(int j = 0; j<6; j++) {
System.out.print(nStrArr[i][j]+ " ");
}
System.out.println();
}
}

目前它正在正确打印图形,但点完全错误,我认为plotPoint方法中的第二个if语句不正确,但我不确定还能尝试什么。

我认为在图中标记x不需要for循环。试试这个。

public static String[][] plotPoint(String[][] plot) {
int x = 0, y = 0;
Scanner sc = new Scanner(System.in);
boolean bool = true;
while (bool) {
System.out.println("Enter x coordinate between 0 and 5: ");
x = sc.nextInt();
System.out.println("Enter y coordinate between 0 and 5: ");
y = sc.nextInt();
if (x >= 5 || x < 0 || y >= 5 || y < 0) {
System.out.println("The coordinates entered exceed the limit.");
} else {
y = y + 1;
bool = false;
}
}
plot[plot.length - y][x] = "x";
return plot;
}

最新更新