扫描仪输入问题

发布于 2025-01-05 18:49:04 字数 543 浏览 0 评论 0原文

如何从扫描仪接收用户的输入,然后将该输入放入二维数组中。这就是我所拥有的,但我认为它不正确:

public static void main(String[] args)
{
    Scanner in = new Scanner(System.in);
    int [][] a = new int[row][col];
    Scanner in = new Scanner(System.in);

    System.out.println("Enter a sequence of integers: ");
    while (in.hasNextInt())
    {
        int a[][] = in.nextInt();
        a [row][col] = temp;
        temp = scan.nextInt();
    }
    Square.check(temp);
}

我想做的是创建一个 2D 数组并创建一个幻方。我已经弄清楚了布尔部分,我只需要帮助将用户的数字序列输入到数组中,以便布尔方法可以测试数字。非常感谢所有帮助

How do I take in input from a user from a scanner, then put that input into a 2D Array. This is what I have but I dont think it is right:

public static void main(String[] args)
{
    Scanner in = new Scanner(System.in);
    int [][] a = new int[row][col];
    Scanner in = new Scanner(System.in);

    System.out.println("Enter a sequence of integers: ");
    while (in.hasNextInt())
    {
        int a[][] = in.nextInt();
        a [row][col] = temp;
        temp = scan.nextInt();
    }
    Square.check(temp);
}

What I am trying to do is create a 2D array and create a magic Square. I have the boolean part figured out, I just need help with inputting users sequence of numbers into the array so the boolean methods can test the numbers. All help greatly appreciated

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

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

发布评论

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

评论(2

梦初启 2025-01-12 18:49:04

我不相信你的代码会按照你想要的方式工作。如果我正确理解你的问题,我会这样做:

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    int [][] a = new int[row][col];

    for(int i = 0; i < row; i++) {
        for(int j = 0; j < col; j++) {
            System.out.print("Enter integer for row " + i + " col " + j + ": ");
            a[i][j] = in.nextInt();
        }
    }

    // Create your square here with the array
}

在循环中,i 是当前行号,j 是当前列号。它将询问用户每个行/列的组合。

I don't believe your code will work how you want it to. If I'm understanding your question correctly, here's what I would do:

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    int [][] a = new int[row][col];

    for(int i = 0; i < row; i++) {
        for(int j = 0; j < col; j++) {
            System.out.print("Enter integer for row " + i + " col " + j + ": ");
            a[i][j] = in.nextInt();
        }
    }

    // Create your square here with the array
}

In the loops, i is the current row number and j is the current column number. It will ask the user for every row/column combination.

少女的英雄梦 2025-01-12 18:49:04

您可以使用它来同时输入所有数字:

int [][] a = new int[3][3];
Scanner in = new Scanner(System.in);

System.out.println("Enter a sequence of integers: ");
int row=0,col=0;
while (in.hasNextInt())
{
     a [row][col++] = in.nextInt();
     if(col>=3){
         col=0;
         row++;
     }
     if(row>=3)break;
}

然后您可以输入:

1 2 3 4 5 6 7 8 9

来填充您的数组。

You can use that in order to enter all number at the same time :

int [][] a = new int[3][3];
Scanner in = new Scanner(System.in);

System.out.println("Enter a sequence of integers: ");
int row=0,col=0;
while (in.hasNextInt())
{
     a [row][col++] = in.nextInt();
     if(col>=3){
         col=0;
         row++;
     }
     if(row>=3)break;
}

Then you can enter :

1 2 3 4 5 6 7 8 9

to fill your array.

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