Java 如何从方法获取用户输入的大小和填充数组

Java 如何从方法获取用户输入的大小和填充数组,java,arrays,methods,user-input,Java,Arrays,Methods,User Input,我对Java比较陌生,对为什么我的代码不能正常工作感到困惑。我正在使用方法和数组创建一个计算器。在我的代码中,我下面提供的getOperator方法应该首先提示用户输入足够的值来填充数组,然后返回数组。这就是我困惑的地方。如果我必须使用相同的方法来检索数组的大小并从用户输入中填充数组,那么我是否正确设置了数组?两个都可以退回吗?下面我尝试了我认为有效的方法,但似乎无效。当我尝试填充数组时,会得到ArrayIndexOutOfBoundsException if(selection == 1) {

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

我在这里看到的问题是

int size = getOperand("What size would you like the array", size);
您的
getOperand
方法应该返回一个
double[]
。但是,您将其存储在int类型的size变量中

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


希望这有帮助

首先,要更准确地回答这个问题,因为很难理解你想要完成什么,你可以提供更多关于你想要完成什么样的操作的信息

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

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
方法用作为您创建数组的函数。e、 g

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;
    }
(操作顺序错误,因为您在实际询问用户问题之前接受了用户输入。)

您应该创建另一个方法,从用户处获取double

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

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

另外
double operan1[]=getOperand(“您希望第一个数字是什么”,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)

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

这没有逻辑意义:
int size=getOperand(“您希望数组的大小”,size)
为什么要创建一个返回double数组的方法并使用它来尝试获取int结果?如果用Java声明数组,请使用
double[]operand
,而不是
double operand[]
。第一个被认为是标准的,使用它可以提高熟悉Java的人的可读性。为什么不包括add()函数?如果用Java声明数组,请使用
double[]operand
,而不是
double operand[]
。第一种被认为是标准的,使用它可以提高熟悉Java的人的可读性。
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)