将用户输入的数字推入一个二维数组,形成4列3行。

huangapple go评论126阅读模式
英文:

Pushing user input numbers into a 2D Array to form 4 columns with 3 rows

问题

我正在处理一个两部分的任务,第一部分是创建一个3行4列的数组,然后让用户输入4个数字,以填充第一行的第一列。所以如果用户输入了1、2、3、4,数组应该打印出:(第二部分的任务只是留白。)

1  2  3  4
0  0  0  0
0  0  0  0

到目前为止,这是我的代码,但我只是刚开始学习Java几天,我肯定没有清楚地看到我的错误。我真的很感谢在我做错了什么方面提供任何帮助。

以下是我的代码:

import java.util.Scanner;

public class multipleElements {
    public static void main(String[] args) {
        // 设置数组并分配变量名和表格大小
        int[][] startNum = new int[3][4];

        // 设置数组的用户输入变量
        Scanner userInput = new Scanner(System.in);
        for (int i = 0; i < startNum.length; i++) {
            // 获取用户输入
            System.out.print("请输入您的值:");

            // 将输入推入数组
            int inputValue = userInput.nextInt();
            startNum[i][0] = inputValue;
            startNum[i][0] = userInput
        }
    }
}

至于任务的第二部分,第二行和第三行需要是与输入到相应列的第一行的数字的倍数。所以它会是这样的:

1  2  3  4
2  4  6  8
3  6  9  12

我还不确定我将如何做到这一点,所以任何关于我应该从哪里开始研究或者我应该查找什么的建议也将不胜感激。

英文:

I'm working on a two part task, where the first part is to create an array of 3 rows and 4 columns and then have the user input 4 numbers to make up the first column of the first row. so if the user inputs 1,2,3,4 then the array should print out: (0 just blank for the second part of the task.

1  2  3  4
0  0  0  0
0  0  0  0

So far this is what I have, but I've only been working with Java for a few days and i'm sure i'm not seeing my error clearly. I would really appreciate any help in what i am doing wrong.

Here is my code:

import java.util.Scanner;

public class multipleElements {
    public static void main(String[] args) {
        //set up the array and assign variable name and table size
        int[][] startNum = new int[3][4];

        //set user input variable for the array
        Scanner userInput = new Scanner(System.in);
        for (int i = 0; i &lt; startNum.length; i++) {
            //get user input
            System.out.print(&quot;please enter your value: &quot;);

            //push into the array
            String inputValue = userInput.nextInt();
            startNum[i][0] = inputValue;
            startNum[i][0] = userInput
        }
    }
}

as for the second part of the task, the second and third row need to be multiples of whatever number is entered into the first row of that column. So it would look like this:

1  2  3  4
2  4  6  8
3  6  9  12

I'm not yet sure how i'm going to do that so any advice on where i could start researching or what i should look into would also be appreciated.

答案1

得分: 0

请尝试以下代码:

public static void main(String[] args) {
    // 设置数组并分配变量名称和表格大小
    int[][] startNum = new int[3][4];

    // 设置数组的用户输入变量
    Scanner userInput = new Scanner(System.in);

    for (int i = 0; i < startNum[0].length; i++) {
        System.out.print("请输入您的值:");
        int inputValue = userInput.nextInt();
        startNum[0][i] = inputValue;
    }

    for (int i = 1; i < startNum.length; i++) {
        for (int j = 0; j < startNum[0].length; j++) {
            startNum[i][j] = (i + 1) * startNum[0][j];
        }
    }
}

在第一个循环中,您正在从用户那里获取值并设置二维数组的第一行。

在第二个循环中(两个循环嵌套),您正在为第二行和第三行的每列设置值。这就是为什么第一个循环从i=1开始,第二个循环从j=0开始的原因。

英文:

Please try this code:

public static void main(String[] args) {
    //set up the array and assign variable name and table size
    int[][] startNum = new int[3][4];

    //set user input variable for the array
    Scanner userInput = new Scanner(System.in);

    for (int i = 0; i &lt; startNum[0].length; i++) {
        System.out.print(&quot;please enter your value: &quot;);
        int inputValue = userInput.nextInt();
        startNum[0][i] = inputValue;
    }

    for (int i = 1; i &lt; startNum.length; i++) {
        for (int j = 0; j &lt; startNum[0].length; j++) {
            startNum[i][j] = (i + 1) * startNum[0][j];
        }
    }
}

In the first loop you are getting the values from user and setting first row of 2d array.

In the second for loops(2 fors) you are setting the values for 2nd and 3rd loop for each column from first row. That's why first for loop is starting from i=1 and second for loop is starting from j=1.

答案2

得分: 0

for (int i = 0; i < startNum[0].length; i++) {
    // 获取用户输入
    System.out.print("请输入您的值:");

    // 添加到数组中
    int inputValue = userInput.nextInt(); // <-- 赋值给整型变量
    // 将用户输入赋值给第一行的各列
    startNum[0][i] = inputValue; 

    //startNum[i][0] = userInput // <-- 不需要
}

for (int i = 1; i < startNum.length; i++) {
    for (int j = 0; j < startNum[0].length; j++) {
        // 从第二行开始,计算每行在当前列的值
        // 每列的值为第一行对应列的值乘以行数
        startNum[i][j] = startNum[0][j] * (i + 1); 
    }
}
英文:
for (int i = 0; i &lt; startNum[0].length; i++) {
    // get user input
    System.out.print(&quot;please enter your value: &quot;);

    // push into the array
    int inputValue = userInput.nextInt(); // &lt;-- assign to an int value
    // assigns the user input to the columns of 0th row
    startNum[0][i] = inputValue; 

    //startNum[i][0] = userInput // &lt;-- not required
}

for (int i = 1; i &lt; startNum.length; i++) {
    for (int j = 0; j &lt; startNum[0].length; j++) {
        // starting from 1st row calculate the values for all the rows
        // for a column and then move on to next column once finished
        startNum[i][j] = startNum[0][j] * (i + 1); 
    }
}

huangapple
  • 本文由 发表于 2020年8月29日 19:01:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63646234.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定