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

Java 将用户输入的数字推入二维数组,形成4列3行,java,multidimensional-array,Java,Multidimensional Array,我正在处理一个由两部分组成的任务,第一部分是创建一个3行4列的数组,然后让用户输入4个数字组成第一行的第一列。因此,如果用户输入1,2,3,4,那么数组应该打印出来:(0)任务的第二部分为空 1 2 3 4 0 0 0 0 0 0 0 0 到目前为止,这就是我所拥有的,但我只在Java上工作了几天,我确信我并没有清楚地看到我的错误。我真的非常感谢任何对我所犯错误的帮助 这是我的密码: import java.util.Scanner; 公共类多元素{ 公共静态void mai

我正在处理一个由两部分组成的任务,第一部分是创建一个3行4列的数组,然后让用户输入4个数字组成第一行的第一列。因此,如果用户输入1,2,3,4,那么数组应该打印出来:(0)任务的第二部分为空

1  2  3  4
0  0  0  0
0  0  0  0
到目前为止,这就是我所拥有的,但我只在Java上工作了几天,我确信我并没有清楚地看到我的错误。我真的非常感谢任何对我所犯错误的帮助

这是我的密码:

import java.util.Scanner;
公共类多元素{
公共静态void main(字符串[]args){
//设置数组并指定变量名和表大小
int[]startNum=新int[3][4];
//为数组设置用户输入变量
扫描仪用户输入=新扫描仪(System.in);
对于(int i=0;i
对于任务的第二部分,第二行和第三行需要是该列第一行中输入的任何数字的倍数。因此,它将如下所示:

1  2  3  4
2  4  6  8
3  6  9  12
我还不知道我该怎么做,所以对于我可以从哪里开始研究或者我应该研究什么的任何建议都将不胜感激。

请尝试此代码-

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 < startNum[0].length; i++ ) {
        System.out.print("please enter your value: ");
        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];
        }
    }
}
publicstaticvoidmain(字符串[]args){
//设置数组并指定变量名和表大小
int[]startNum=新int[3][4];
//为数组设置用户输入变量
扫描仪用户输入=新扫描仪(System.in);
对于(int i=0;i
在第一个循环中,您将从用户获取值并设置2d数组的第一行

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

for(int i=0;i    for (int i = 0; i < startNum[0].length; i++ ) {
        
        //get user input
        System.out.print("please enter your value: ");
        
        //push into the array
        int inputValue =  userInput.nextInt(); // <-- assign to an int value
        startNum[0][i] = inputValue; // assigns the user input to the columns of 0th row
     
        //startNum[i][0] = userInput //<-- not required
    }

    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); //starting from 1st row calcuate the values for all the rows for a column and then move on to next column once finished
        }
    }
//获取用户输入 System.out.print(“请输入您的值:”); //推入阵列
int inputValue=userInput.nextInt();//inputValue的类型应该是整数吗?对不起,我没有得到您需要的…1)你有3行4列,你说
输入4个数字组成第一行的第一列
,但第一行的第一列是一个数字。2)然后你把这4个数字放在第一行的例子中,但在你的代码中输入第一列的3个数字……你能更清楚一点吗?@DeepakPatankar it仍然为
Integer inputValue=userInput.nextInt();
提供了一个错误,其中它说“无法在基本类型int上调用nextInt()”-我不太清楚这是什么意思。@Miguel-谢谢你抓住了这一点。我在这里是在冒烟-我的意思是用户输入的4个数字需要组成第一行,而不是第一列。在代码
[3][4]
中是3行,4列。我们在正确的轨道上。但请记住,行是第一维(这就是为什么您使用
new int[3][4]
初始化数组,而不是
new int[4][3]
)。因此,要填充顶行,您需要运行从0到
startNum[0]的循环。length
(将计算列)并将输入值分配给
startNum[0][i]