如何将用户输入的数据输入 Java 中的二维数组?
How do I enter data from user input into a 2D array in Java?
我有一个二维数组,需要根据用户输入更改其值。
private static double nursesArray[][] = {
{2020, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{2021, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{2022, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{2023, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{2024, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};
该程序围绕它询问用户 2020 年第二列(索引 1)的基本工资。然后程序将要求用户输入下面每一年的百分比差异,在同一列的每一行中向下。这个过程需要在每一列中一直迭代到最后。
我设置其余代码的方式是使用数组作为方法的参数。
public class nursesUnion {
private static double nursesArray[][] = {
{2020, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{2021, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{2022, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{2023, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{2024, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};
public static void dataEntry(double arr[][]) {
Scanner inp = new Scanner(System.in);
}
...
我真的不知道从哪里开始。 Java 对我来说是一门新语言,我还没有完全理解它。
public static void dataEntry(double arr[][]) {
Scanner inp = new Scanner(System.in);
for(int column = 1; column < arr[0].length; column++){
System.out.println("Enter base wage for col:"+column);
arr[0][column]=inp.nextInt();
System.out.println("Enter % increase per year");
int increase=inp.nextInt();
for(int row=1; row<arr.length; row++){
arr[row][col] += arr[row-1][column]*increase/100;
}
}
}
假设用户已经知道他们需要输入多少个值,您可以从这个基本版本开始:
public static void dataEntry(double arr[][]) {
Scanner inp = new Scanner(System.in);
int rows = nursesArray.length;
int columns = nursesArray[0].length;
for (int row = 0; row < rows; row++) {
System.out.println("Please, enter values for year " + nursesArray[row][0]);
// starts with 1 to skip the year.
for (int column = 1; column < columns; column++) {
nursesArray[row][column] = inp.nextDouble();
}
}
}
它只是从左到右、从上到下遍历行和列。
我有一个二维数组,需要根据用户输入更改其值。
private static double nursesArray[][] = {
{2020, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{2021, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{2022, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{2023, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{2024, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};
该程序围绕它询问用户 2020 年第二列(索引 1)的基本工资。然后程序将要求用户输入下面每一年的百分比差异,在同一列的每一行中向下。这个过程需要在每一列中一直迭代到最后。
我设置其余代码的方式是使用数组作为方法的参数。
public class nursesUnion {
private static double nursesArray[][] = {
{2020, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{2021, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{2022, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{2023, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{2024, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};
public static void dataEntry(double arr[][]) {
Scanner inp = new Scanner(System.in);
}
...
我真的不知道从哪里开始。 Java 对我来说是一门新语言,我还没有完全理解它。
public static void dataEntry(double arr[][]) {
Scanner inp = new Scanner(System.in);
for(int column = 1; column < arr[0].length; column++){
System.out.println("Enter base wage for col:"+column);
arr[0][column]=inp.nextInt();
System.out.println("Enter % increase per year");
int increase=inp.nextInt();
for(int row=1; row<arr.length; row++){
arr[row][col] += arr[row-1][column]*increase/100;
}
}
}
假设用户已经知道他们需要输入多少个值,您可以从这个基本版本开始:
public static void dataEntry(double arr[][]) {
Scanner inp = new Scanner(System.in);
int rows = nursesArray.length;
int columns = nursesArray[0].length;
for (int row = 0; row < rows; row++) {
System.out.println("Please, enter values for year " + nursesArray[row][0]);
// starts with 1 to skip the year.
for (int column = 1; column < columns; column++) {
nursesArray[row][column] = inp.nextDouble();
}
}
}
它只是从左到右、从上到下遍历行和列。