如何将来自用户输入的数据输入到Java中的2D数组中?
我有一个二维数组,需要根据用户输入更改其值。
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}
};
该程序围绕询问用户第二列(索引 1)2020 年的基本工资。然后,程序将要求用户输入下面每一年的百分比差异,在同一列的每一行中向下输入。这个过程需要在每一列中迭代直到最后。
我设置其余代码的方式是使用数组作为方法的参数。
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 对我来说是一门新语言,我还没有完全理解它。
I have a 2D array that needs it's values to be changed based on user input.
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}
};
The program revolves around it asking the user for the base wage in the year 2020 for the second column (index 1). The program will then ask the user to enter the percent differences in each of the years below, going down each row in that same column. This process needs to be iterated in each of the columns all the way to the end.
The way I have the rest of my code set up is that it uses the array as an argument for the method.
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);
}
...
I really have no idea where to begin for this. Java is a new language for me and I haven't fully wrapped my head around it yet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(2)
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;
}
}
}
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
假设用户已经知道他们需要输入多少个值,您可以从这个基本的方面开始:
它仅迭代槽行和列从左至右,从上到下。
Assuming user already knows how many values they need to enter, you can start with this basic verion:
It just iterates trough rows and columns from left to right, from top to bottom.