gpt4 book ai didi

java - 为什么我的网格没有从 'O' 更新到 'S'(数组)

转载 作者:太空宇宙 更新时间:2023-11-04 10:54:53 26 4
gpt4 key购买 nike

基本上,我正在尝试创建一个程序,当用户在扫描仪中输入坐标时,我的网格从“O”更改为“S”。对我来说,这是合乎逻辑的方法,我是java新手,想知道我的代码是否有错误,扫描仪正在挑衅地工作,就好像我尝试打印 x 一样,它会显示用户输入的 int 。提前谢谢您。

 import java.util.Scanner;
public class TheGame {
public static void main(String[] args) { {

System.out.println ("Players Board");


char [][] grid = new char [10][10];
//FILL GRID//
for(int outerLoopValue = 0; outerLoopValue<10;outerLoopValue++)

{

    for(int innerLoopValue = 0; innerLoopValue<10;innerLoopValue++)
{
grid[outerLoopValue][innerLoopValue]='O';
}

}
//END OF FILL GRID//
//DRAW GRID//
for(int outerLoopValue = 0; outerLoopValue<10;outerLoopValue++)
{

System.out.println("");
for(int innerLoopValue = 0; innerLoopValue<10;innerLoopValue++)
{
System.out.print(grid[outerLoopValue][innerLoopValue]+" ");
}
}
Scanner sc =new Scanner(System.in);
System.out.println("Please Choose the Co-ordinates of your first ship");
System.out.println("X = ");
int x = sc.nextInt();
System.out.println("Y = ");
int y = sc.nextInt();

grid[x][y] = 'S';

最佳答案

网格确实发生了变化,但是您在用户输入后没有打印网格,因此您没有看到网格发生变化。

我建议你提取一个打印网格的方法。

public static void printGrid(char[][] grid) {
for(int outerLoopValue = 0; outerLoopValue<10;outerLoopValue++)
{
System.out.println("");
for(int innerLoopValue = 0; innerLoopValue<10;innerLoopValue++)
{
System.out.print(grid[outerLoopValue][innerLoopValue]+" ");
}
}
}

然后,在用户输入内容之前和之后调用此方法:

System.out.println ("Players Board");


char [][] grid = new char [10][10];
//FILL GRID//
for(int outerLoopValue = 0; outerLoopValue<10;outerLoopValue++)
{

for(int innerLoopValue = 0; innerLoopValue<10;innerLoopValue++)
{
grid[outerLoopValue][innerLoopValue]='O';
}

}
//END OF FILL GRID//
printGrid(grid); // Note the change here
Scanner sc =new Scanner(System.in);
System.out.println("Please Choose the Co-ordinates of your first ship");
System.out.println("X = ");
int x = sc.nextInt();
System.out.println("Y = ");
int y = sc.nextInt();

grid[x][y] = 'S';
printGrid(grid); // Note the change here!

这是一个示例运行:

Players Board

O O O O O O O O O O
O O O O O O O O O O
O O O O O O O O O O
O O O O O O O O O O
O O O O O O O O O O
O O O O O O O O O O
O O O O O O O O O O
O O O O O O O O O O
O O O O O O O O O O
O O O O O O O O O O Please Choose the Co-ordinates of your first ship
X =
3
Y =
3

O O O O O O O O O O
O O O O O O O O O O
O O O O O O O O O O
O O O S O O O O O O
O O O O O O O O O O
O O O O O O O O O O
O O O O O O O O O O
O O O O O O O O O O
O O O O O O O O O O
O O O O O O O O O O

完整代码如下:

public static void main(String[] args) {
System.out.println ("Players Board");


char [][] grid = new char [10][10];
for(int outerLoopValue = 0; outerLoopValue<10;outerLoopValue++)
{

for(int innerLoopValue = 0; innerLoopValue<10;innerLoopValue++)
{
grid[outerLoopValue][innerLoopValue]='O';
}

}
printGrid(grid); // Note the change here
Scanner sc =new Scanner(System.in);
System.out.println("Please Choose the Co-ordinates of your first ship");
System.out.println("X = ");
int x = sc.nextInt();
System.out.println("Y = ");
int y = sc.nextInt();

grid[x][y] = 'S';
printGrid(grid); // Note the change here!
}

public static void printGrid(char[][] grid) {
for(int outerLoopValue = 0; outerLoopValue<10;outerLoopValue++)
{
System.out.println("");
for(int innerLoopValue = 0; innerLoopValue<10;innerLoopValue++)
{
System.out.print(grid[outerLoopValue][innerLoopValue]+" ");
}
}
}

关于java - 为什么我的网格没有从 'O' 更新到 'S'(数组),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47419309/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com