如何将来自用户输入的数据输入到Java中的2D数组中?

发布于 01-20 03:45 字数 933 浏览 2 评论 0原文

我有一个二维数组,需要根据用户输入更改其值。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

つ可否回来2025-01-27 03:45:25

假设用户已经知道他们需要输入多少个值,您可以从这个基本的方面开始:

    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();
            }
        }
    }

它仅迭代槽行和列从左至右,从上到下。

Assuming user already knows how many values they need to enter, you can start with this basic verion:

    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();
            }
        }
    }

It just iterates trough rows and columns from left to right, from top to bottom.

痴情换悲伤2025-01-27 03:45:25
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);
    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;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文