通过扫描仪更新二维数组

发布于 2024-12-05 02:02:05 字数 383 浏览 2 评论 0原文

我是 Java 和一般编程的新手。我最近开始使用数组,并在书中发现了一个练习,我认为我应该尝试一下。目标是使用扫描仪类读取文件,将每个数字分配给二维数组中的不同单元格。这就是我所拥有的方法。但无论我如何改变它,我似乎都无法得到想要的结果。要么我最终得到每个单元格中的最后一个数字,要么我收到错误。请帮忙。

int row = 0;
int col = 0;
while (A[row][col] != -1)
{
for (row = 0; row < A.length; row++)
{
    for (col = 0; col < A[row].length; col++)
        A[row][col] = scan.nextInt();   
        }
}

I'm new to Java, and programming in general. I recently started working with arrays, and came upon an exercise in the book that i thought id give a try. The goal is to read a file using the scanner class assign each number to a different cell in a 2d array. This is what i have for the method. But no matter how i change it, i cant seem to get the desired result. Either i end up getting the last number in every cell, or i get an error. Please help.

int row = 0;
int col = 0;
while (A[row][col] != -1)
{
for (row = 0; row < A.length; row++)
{
    for (col = 0; col < A[row].length; col++)
        A[row][col] = scan.nextInt();   
        }
}

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

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

发布评论

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

评论(2

紅太極 2024-12-12 02:02:05

扫描需要在最内层循环中进行。此时,您可能需要重新阅读您正在阅读的章节,并在发布到 SO 之前花一些时间来解决问题。

...

for (int col = 0; col < A[row].length; col++){
   A[row][col] = temp;
   temp = scan.nextInt();
}
...

您可能还会发现在观看程序执行时打印出值很有用。在 temp 中读取的位置之后添加 System.out.println(temp)。这样一来问题就很明显了。您还需要更改 while 循环构造。就目前而言,这没有多大意义。

The scanning needs to happen in the inner-most loop. At this point, you might want to re-read the chapter you're on and spend a bit longer working on problems before posting to SO.

...

for (int col = 0; col < A[row].length; col++){
   A[row][col] = temp;
   temp = scan.nextInt();
}
...

You might also find that printing out values is useful while watching programs execute. Add System.out.println(temp) after the point where you read in temp. This would have made the problem obvious. You'll also want to change your while looping construct. As of now, it doesn't make much sense.

情深已缘浅 2024-12-12 02:02:05

根据您的评论...这应该满足您的要求。您遇到的问题是,如果外部循环没有某种条件,您就无法摆脱内部循环。

请注意,我将 A 更改为 a;变量永远不应该以大写字母开头。

int a[][] = new int[20][20];
int row = 0;
int col = 0;
int current = 0;
for (row = 0; row < a.length, current != -1; row++)
{
    for (col = 0; col < a[row].length; col++)
    {
         try
         {
             current = scan.nextInt();           
             if (current == -1)
             {
                 break;
             }
             else
             {
                 a[row][col] = current;   
             }
         }
         catch ( NoSuchElementException e)
         {
             System.out.println("I hit the end of the file without finding -1!");
             current = -1;
             break;
         }
         catch ( ArrayIndexOutOfBoundsException e)
         {
                System.out.println("I ran out of space in my 2D array!");
                current = -1;
                break;
         }
     }
}

我个人不会使用嵌套循环,而是走这条路:

int a[][] = new int[20][20];
int row = 0;
int col = 0;
int current = 0;

while (scan.hasNextInt())
{
    current = scan.nextInt();
    if (current == -1)
    {
        break;
    }

    a[row][col] = current;
    col++;
    if (col == a[row].length)
    {
        row++;
        col = 0;

        if (row == a.length)
        {
            System.out.println("I ran out of space in my 2D array!");
            break;
        }
    }
}

Based on your comments ... this should do what you're asking. The problem you're having is that you're not able to break out of your inner loop without having some sort of conditional on the outer one.

Note that I changed A to a; variables should never start with uppercase.

int a[][] = new int[20][20];
int row = 0;
int col = 0;
int current = 0;
for (row = 0; row < a.length, current != -1; row++)
{
    for (col = 0; col < a[row].length; col++)
    {
         try
         {
             current = scan.nextInt();           
             if (current == -1)
             {
                 break;
             }
             else
             {
                 a[row][col] = current;   
             }
         }
         catch ( NoSuchElementException e)
         {
             System.out.println("I hit the end of the file without finding -1!");
             current = -1;
             break;
         }
         catch ( ArrayIndexOutOfBoundsException e)
         {
                System.out.println("I ran out of space in my 2D array!");
                current = -1;
                break;
         }
     }
}

I personally wouldn't use the nested loops, and go this route:

int a[][] = new int[20][20];
int row = 0;
int col = 0;
int current = 0;

while (scan.hasNextInt())
{
    current = scan.nextInt();
    if (current == -1)
    {
        break;
    }

    a[row][col] = current;
    col++;
    if (col == a[row].length)
    {
        row++;
        col = 0;

        if (row == a.length)
        {
            System.out.println("I ran out of space in my 2D array!");
            break;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文