Java 为什么我的网格没有从';O';至';S';(阵列)

Java 为什么我的网格没有从';O';至';S';(阵列),java,arrays,Java,Arrays,基本上,我试图创建一个程序,当用户将坐标输入扫描仪时,我的网格从“O”变为“S”。对我来说,这是一种合乎逻辑的方法。我对java还不熟悉,不知道我的代码是否有错误。扫描器正在挑战性地工作,好像我试图打印x一样。它会显示用户输入的int值。先谢谢你 import java.util.Scanner; public class TheGame { public static void main(String[] args) { { System.out.println ("Players

基本上,我试图创建一个程序,当用户将坐标输入扫描仪时,我的网格从“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++)
import java.util.Scanner;
公开课游戏{
公共静态void main(字符串[]args){{
System.out.println(“玩家板”);
字符[][]网格=新字符[10][10];
//填充网格//

对于(int-outerLoopValue=0;outerLoopValue,
网格确实已更改,但用户输入后未打印网格,因此未看到网格更改

我建议您提取一种打印网格的方法

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]+"  ");
        }
    }
}
以下是完整的代码:

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]+"  ");
        }
    }
}
publicstaticvoidmain(字符串[]args){
System.out.println(“玩家板”);
字符[][]网格=新字符[10][10];

对于(int-outerLoopValue=0;outerLoopValue,
网格确实已更改,但用户输入后未打印网格,因此未看到网格更改

我建议您提取一种打印网格的方法

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]+"  ");
        }
    }
}
以下是完整的代码:

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]+"  ");
        }
    }
}
publicstaticvoidmain(字符串[]args){
System.out.println(“玩家板”);
字符[][]网格=新字符[10][10];

对于(int outerLoopValue=0;outerLoopValue如何知道网格未更新?在将位置设置为
S
后,您不会打印任何内容。哦,好的,我如何打印更新的网格?是否无法更新已显示的网格?如何知道网格未更新?在设置位置后,您不会打印任何内容到
S
。哦,好吧,我该如何打印更新后的网格?是否没有办法更新已经显示的网格?我理解你告诉我要做什么,但我并不真正理解编码和你使用的方法,例如,为什么printgrid现在处于公共静态无效状态?这应该是主类吗?@Chop应该在t中主类。如果您愿意,可以将其声明为私有。我认为在这样一个小程序中,它并不重要(除非这不是您的完整代码)。存在
static
以便从静态上下文访问它,即
main
方法。这是当主字符串args替换为您的代码时我遇到的错误:错误:在类游戏中找不到主方法,请将主方法定义为:public static void main(string[/args)或者JavaFX应用程序类必须扩展JavaFX.application。Application@Chop你应该使
main
看起来像我的第二个代码片段,并将
printGrid
放在
main
方法旁边。我这样做了,但这不起作用,如果你直接将代码复制粘贴到java中,它对你有用吗?很抱歉,给你添麻烦了我是一个非常内行的人,在我的作业压力下。我理解你告诉我要做什么,但我并不真正理解你使用的编码和方法。例如,为什么printgrid现在处于公共静态无效状态?这应该是主类吗?@Chop应该在主类中。如果你愿意,可以声明它为私有。我认为在这样一个小程序中,它并不重要(除非这不是您的完整代码)。存在
static
以便从静态上下文访问它,即
main
方法。这是当主字符串args替换为您的代码时我遇到的错误:错误:在类游戏中找不到主方法,请将主方法定义为:public static void main(string[/args)或者JavaFX应用程序类必须扩展JavaFX.application。Application@Chop你应该使
main
看起来像我的第二个代码片段,并将
printGrid
放在
main
方法旁边。我这样做了,但这不起作用,如果你直接将代码复制粘贴到java中,它对你有用吗?很抱歉,给你添麻烦了我在这方面非常内行,在我的任务压力下,这是我的任务。
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]+"  ");
        }
    }
}