从文本到二维数组的网格

发布于 2024-09-28 16:57:01 字数 1028 浏览 0 评论 0原文

  import java.io.*;
import java.util.*;
import java.awt.*;




public class FileInputExample2
{



static public void main(String[] args) throws IOException
  {
  int t;
    BufferedReader filein;
    filein = new BufferedReader (new FileReader("GridDATA.txt"));
   int intGrid [] [] = new int [10] [10];
    String inputLine = filein.readLine();

    StringTokenizer st = new StringTokenizer(inputLine, " ");

    for (int i=0; i<10; i++)
   for (int j=0; j<10; j++)
    {String eachNumber = st.nextToken();
      intGrid [i] [j] = Integer.parseInt(eachNumber);
    }
     for (int i=0; i<10; i++)
     for (int j=0; j<10; j++)
    {
      System.out.println( intGrid[i][j]);
    }

  }
}

这就是我到目前为止所拥有的,我试图显示这个网格,我的文本文件如下所示:

0 1 1 1 1 1 1 1 1 1
0 0 0 1 1 1 1 1 1 1
1 1 0 1 1 1 1 0 0 0
1 1 0 0 0 0 1 0 1 0
1 1 1 1 1 0 1 0 1 0
1 1 1 1 1 0 0 0 1 0
1 1 1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1 1 0

我不知道为什么它不工作。最终我会建造一个迷宫。

  import java.io.*;
import java.util.*;
import java.awt.*;




public class FileInputExample2
{



static public void main(String[] args) throws IOException
  {
  int t;
    BufferedReader filein;
    filein = new BufferedReader (new FileReader("GridDATA.txt"));
   int intGrid [] [] = new int [10] [10];
    String inputLine = filein.readLine();

    StringTokenizer st = new StringTokenizer(inputLine, " ");

    for (int i=0; i<10; i++)
   for (int j=0; j<10; j++)
    {String eachNumber = st.nextToken();
      intGrid [i] [j] = Integer.parseInt(eachNumber);
    }
     for (int i=0; i<10; i++)
     for (int j=0; j<10; j++)
    {
      System.out.println( intGrid[i][j]);
    }

  }
}

this is what i have so far im trying to display this grid that i have the text file looks like this :

0 1 1 1 1 1 1 1 1 1
0 0 0 1 1 1 1 1 1 1
1 1 0 1 1 1 1 0 0 0
1 1 0 0 0 0 1 0 1 0
1 1 1 1 1 0 1 0 1 0
1 1 1 1 1 0 0 0 1 0
1 1 1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1 1 0

I have no idea why its not wokring. ultimatly i wil be makin a maze.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一江春梦 2024-10-05 16:57:01

一方面,System.out.println(intGrid[i][j]); 将每行打印一个网格元素。

您可能想要更多类似的内容,

 for (int i=0; i<10; i++) {
     for (int j=0; j<10; j++)
         {
         System.out.print( intGrid[i][j]);
         System.out.print(" ");
         }
     System.out.println("");
     }

请注意,我们在内循环中使用 print,而不是 println。这不会执行回车,因此数字将在一行上。然而,在内循环之后,我们执行 println 来执行回车/换行。

For one thing, System.out.println( intGrid[i][j]); will print one grid element per line.

You probably want something more like

 for (int i=0; i<10; i++) {
     for (int j=0; j<10; j++)
         {
         System.out.print( intGrid[i][j]);
         System.out.print(" ");
         }
     System.out.println("");
     }

Note that we're using print in the inner loop, not println. This will not perform a carriage return so the numbers will be on one line. After the inner loop, however, we execute a println to perform the carriage return/newline.

烧了回忆取暖 2024-10-05 16:57:01
public class FileInputExample2 {

    static public void main(String[] args) throws IOException {

        BufferedReader filein = new BufferedReader(new FileReader("GridDATA.txt"));
        int intGrid[][] = new int[10][10];
        Scanner st = new Scanner(filein);
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++)
                intGrid[i][j] = st.nextInt();
        }

        for (int[] arr1d : intGrid)
            System.out.println(Arrays.toString(arr1d));

    }
}
public class FileInputExample2 {

    static public void main(String[] args) throws IOException {

        BufferedReader filein = new BufferedReader(new FileReader("GridDATA.txt"));
        int intGrid[][] = new int[10][10];
        Scanner st = new Scanner(filein);
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++)
                intGrid[i][j] = st.nextInt();
        }

        for (int[] arr1d : intGrid)
            System.out.println(Arrays.toString(arr1d));

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文