I am searching for the lines of code to create a new array using a method called insertRow(int[] row). With this method, users can insert 5 numbers to form an array. Then this array should be named row2. Please help.
我正在搜索使用名为insertRow(int [] row)的方法创建新数组的代码行。使用此方法,用户可以插入5个数字以形成数组。然后这个数组应该命名为row2。请帮忙。
public class App
{
public static void main(String[] args)
{
int[] row = new int[5];
int[] row1 = {2,7,1,9,4};
//int[] row2 = insertRow(row); this is wrong
}
public static void insertRow(int[] row)
{
for (int i = 0; i < row.length; i++)
{
int number;
do
number = Integer.parseInt(JOptionPane.showInputDialog("Insert the " + (i+1) + "th positif number"));
while (getal < 0);
row[i] = number;
}
}
}
1 个解决方案
#1
2
You were on the right track: change the signature of your method to return int[]
, allocate the row
inside, and put your code in place of ...
below:
你是在正确的轨道上:改变方法的签名以返回int [],在里面分配行,并将代码放在......下面的位置:
public static int[] insertRow() {
int[] row = new int[5];
...
return row;
}
Now this will work:
现在这将工作:
int[] row2 = insertRow(); // this is no longer wrong :)
#1
2
You were on the right track: change the signature of your method to return int[]
, allocate the row
inside, and put your code in place of ...
below:
你是在正确的轨道上:改变方法的签名以返回int [],在里面分配行,并将代码放在......下面的位置:
public static int[] insertRow() {
int[] row = new int[5];
...
return row;
}
Now this will work:
现在这将工作:
int[] row2 = insertRow(); // this is no longer wrong :)