【问题标题】:How to get user input on size and fill the array from a method如何获取用户输入的大小并从方法中填充数组
【发布时间】:2020-02-09 01:36:27
【问题描述】:

我对 Java 比较陌生,对我的代码为什么不能正常工作感到困惑。我正在使用方法和数组创建一个计算器。在我的代码中,我在下面提供的 getOperand 方法应该首先提示用户输入足够的值来填充数组,然后返回它们的数组。这就是我感到困惑的地方。如果我必须采用相同的方法来检索数组的大小并从用户输入中填充数组,我是否正确设置了数组?两个都可以退货吗?下面我尝试了我认为可行的方法,但它似乎不起作用。当我尝试填充数组时,我得到一个 ArrayIndexOutOfBoundsException

if(selection == 1) {

    double operand1[] = getOperand("Please enter the values for the first array, with a space after each input", size);
    double operand2[] = getOperand("Please enter the values for the second array, with a space after each input", size);

    System.out.println(add(operand1, operand2));


public static double[] getOperand(String prompt, int size) {    
Scanner input = new Scanner(System.in);
System.out.println("Please enter the size for the array");
size = input.nextInt();

double operand[] = new double[size];
for (int x = 0; x < size; x++) {

    operand[size] = input.nextDouble();
}
                                                                            // creating new arrays

System.out.println(prompt);
return operand;

我还没有设置其他方法,我首先尝试学习如何让用户输入来填充数组。不幸的是,我也无法添加任何新方法,因为我的方法是为我预定义的。这是方法列表。

public static double[] add(double[] operand1, double[] operand2)
public static double[] subtract(double[] operand1, double[] operand2)
public static double[] multiply(double[] operand1, double[] operand2)
public static double[] divide(double[] operand1, double[] operand2)
public static double[] dotProduct(double[] operand1, double[] operand2)
public static double[] random(double lowerlimit, double upperlimit, int size)

【问题讨论】:

  • 这不合逻辑:int size = getOperand("What size would you like the array", size); 为什么要创建一个返回 double 数组的方法并使用它来尝试获取 int 结果?
  • 如果你用Java声明数组,请使用double[] operand,而不是double operand[]。第一个被认为是标准的,使用它可以提高任何熟悉 Java 的人的可读性。
  • 为什么不包含 add() 函数?

标签: java arrays methods user-input


【解决方案1】:

首先,问题要更加准确,因为很难理解您要完成的工作,您可以提供更多有关您要完成的操作的信息。

因此,如果我理解正确,您正在尝试获取数组的大小,然后从用户那里获取两个数字(要添加到数组中的数字),然后将它们加在一起。

int size = getOperand("What size would you like the array", size);
double operand1[] = getOperand("What would you like the first number to be", size);
double operand2[] = getOperand("What would you like the second number to be", size);
System.out.println(add(operand1, operand2));

这些行没有任何意义,因为您要求用户输入数组的大小,但是您的方法返回数组。同样在您的getOperand 中,您正在向您的用户询问尺寸。因此,使用您的 getOperand 方法作为为您创建数组的函数。例如

public static double[] getOperand(String prompt) {

        Scanner input = new Scanner(System.in);
        System.out.println(prompt);

        int size = input.nextInt();
        double operand[] = new double[size]; // creating new arrays

        return operand;
    }

(您的操作顺序错误,因为您在实际询问用户问题之前接受了用户输入。)

您应该创建另一个从用户那里获取双精度的方法,例如

    {
        Scanner input = new Scanner(System.in);
        System.out.println(Message);
        return (input.nextDouble());

    }

这将帮助您从用户那里获取数字,然后将它们放入数组的正确条目中。

另外double operand1[] = getOperand("What would you like the first number to be", size); 也没有任何意义。这是因为您试图从用户那里获取一个数字,但您没有尝试访问数组中的条目并实际添加它们,而是您试图创建另一个数组?这没有任何逻辑意义。 您应该做的是创建一个数组,然后将用户输入的数字放入第一个和第二个条目(0 和 1)中。我会给你一个伪代码的例子,这样你就可以自己编码了;

CREATE array NAME userNumber TYPE double = SIZE userInputForSize
usernumber ENTRY 0 = userInputDouble
usernumber ENTRY 1 = userInputDouble
DOUBLE result = SUM(usernumber ENTRY 0, usernumber ENTRY 1)
print("The sum of your numbers is" result)

如果您能提供更多信息,我可以提供更多帮助。

【讨论】:

  • 如果你用Java声明数组,请使用double[] operand,而不是double operand[]。第一个被认为是标准的,使用它可以提高任何熟悉 Java 的人的可读性。
【解决方案2】:

我在这里看到的问题是

int size = getOperand("What size would you like the array", size);

您的getOperand 方法应该返回double[]。但是,您将其存储在 int 类型的 size 变量中。

接下来出现的问题是你的getOperand方法的第二个参数是int size。但是,您在修复中将int size 更改为double[] size。我不确定您的代码到底要完成什么,这就是为什么我不能给出实现。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-15
    • 1970-01-01
    • 2012-12-02
    • 2020-10-07
    • 2010-09-11
    • 1970-01-01
    • 2022-01-04
    • 2017-09-21
    相关资源
    最近更新 更多