Java:如何从输入列表返回int数组?

时间:2013-05-07 06:59:02

标签: java arrays types

在观看了很多基础教程之后,我决定尝试制作一些我自己的简单应用程序来帮助我理解OOP并记住我到目前为止学到的东西。

我的尝试是创建一个带有主类和2个对象类的应用程序。

我有Main.java,只调用Performer.java和Calculations.java。

现在我正在处理我的Performer.java对象。

我希望Performer.java从用户那里获得5个整数,然后我希望能够将它作为数组返回并通过我的Calculations.java运行它,这将执行一些简单的任务,如计算平均值和总计。

我将我的方法设置为公开,如果我理解正确,这应该允许我从我的应用程序中的任何其他位置访问这些变量。

我尝试将其作为

返回

return arrayList [] = {u1,u2,u3,u4,u5};

这给了我一个错误“Enum header expected”#

import java.util.Scanner;

public class Performer {

    public static int getUnit(){
        Scanner scan = new Scanner(System.in);

        System.out.println("Enter the first number: ");
        int u1 = scan.nextInt();

        System.out.println("Enter the first number: ");
        int u2 = scan.nextInt();

        System.out.println("Enter the first number: ");
        int u3 = scan.nextInt();

        System.out.println("Enter the first number: ");
        int u4 = scan.nextInt();

        System.out.println("Enter the first number: ");
        int u5 = scan.nextInt();

        return u1, u2, u3, u4, u5;  <--------Confusing Part
    }
}

我的第二次尝试是将它们作为单独的变量返回,但这告诉我期望的类型是int。我认为这是因为这些变量是scan.nextInt()。

如何返回可以通过其他对象传递的数组?

谢谢!

7 个答案:

答案 0 :(得分:5)

您可以使用List或数组。

试试这个 -

public static List<Integer> getUnit(){
    List<Integer> list = new ArrayList<>();
    System.out.println("Enter the first number: ");
    list.add(scan.nextInt());
    ...

    return list;
}

答案 1 :(得分:4)

将getUnit()的返回值更改为int []并返回值,如下所示:

return new int[] {u1, u2, u3, u4};

如果要返回List,可以将返回值设置为List<Integer>并返回值,如下所示:

return Arrays.asList(u1, u2, u3, u4);

请注意,Arrays.asList返回一个不可变的List,因此您无法在其中添加或删除值。

答案 2 :(得分:1)

由于您不熟悉编程,首先让我清除代码中的一些错误。 这一行:

 return u1, u2, u3, u4, u5;  <--------Confusing Part

您使用了return语句,返回一次只允许传递一个值,因此您无法同时传递所有u1,u2,u3,u4。

上面已经由其他编码器提供了解决方案:要么你可以将getUnit()方法的返回类型更改为整数数组并返回该数组的引用,或者你可以更改getUnit的返回类型()方法到一个整数列表并返回列表的引用。

由于您不熟悉编码而且您不了解ArrayList,因此这里有一个链接: Java ArrayList class

答案 3 :(得分:0)

嗯,首先,函数的返回类型是int。你需要它int[]至少表示你想要返回一个整数数组,而不是一个整数。

然后,您需要在Java中找出数组文字语法。

然后,您应该考虑Java集合类是否更适合您尝试执行的操作。查看有关如何构建ArrayList的一些示例。

答案 4 :(得分:0)

替换你困惑的部分:

return new int[]{u1, u2, u3, u4, u5};

和功能签名:

public static int[] getUnit()

答案 5 :(得分:0)

试试这个

`public static int [] getUnit(){

    int[] unit = new int[5];
    Scanner in = new Scanner(System.in);
    for (int i = 0; i < unit.length; i++) {
        System.out.println("Enter element number " + (i + 1) + ":");
        unit[i] = in.nextInt();
    }
    System.out.println("All unit entered");
    return unit;
}`

这就是你想要的。

答案 6 :(得分:-1)

你也可以这样做:

public static int[] getUnit(){
        Scanner scan = new Scanner(System.in);


        System.out.println("Enter the first number: ");
        int u1 = scan.nextInt();
        System.out.println("Enter the first number: ");
        int u2 = scan.nextInt();
        System.out.println("Enter the first number: ");
        int u3 = scan.nextInt();
        System.out.println("Enter the first number: ");
        int u4 = scan.nextInt();
        System.out.println("Enter the first number: ");
        int u5 = scan.nextInt();
        int[] array ={ u1, u2, u3, u4, u5};

        return  array;
        }
        }
相关问题