Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我需要从用户输入中随机获取_Java - Fatal编程技术网

Java 我需要从用户输入中随机获取

Java 我需要从用户输入中随机获取,java,Java,导入java.util.Scanner 公共类hh{ Scanner input = new Scanner(System.in); System.out.print("Enter the size of the array: "); int numbers = input.nextInt(); // Declare an array called numbers with a size of 10 int[] numbers1 = new int[numb

导入java.util.Scanner

公共类hh{

Scanner input = new Scanner(System.in);

    System.out.print("Enter the size of the array:  ");
    int numbers = input.nextInt();

    // Declare an array called numbers with a size of 10
    int[] numbers1 = new int[numbers];

    insertRandomNumbers(numbers1);

    // Print size of numbers
    System.out.println("Initial Array: ");
    for (int i = 0; i < numbers1.length; i++) {
        System.out.print(numbers1[i] + " ");
    }
    System.out.println("");


    //Print First and Last Elements
    System.out.println();
    System.out.println("First and Last Elements");

    int [] lastStep = lastStep(numbers1);
    for (int i = 0; i < lastStep.length; i++) {
        System.out.print(lastStep[i] + ", ");
    }




} // end main

public static int[] lastStep(int[] numbers1) {

    //to get first
    int [] firstElement= numbers1.get(0);

    //last number
    int [] lastElement= numbers1.get(numbers1.size()-1);

}
return lastStep;









public static void insertRandomNumbers(int[] x) {
    for (int i = 0; i < x.length; i++) {
        x[i] = random();
        // System.out.print(x[i] + " ");
    }
    // System.out.println();
}

public static int random() {

    int r = 0 + (int) (Math.random() * (101 - 0)) + 0;
    return r;

}

我发现数组无法解析或不是字段,这是因为您使用的是
数组
而不是
数组列表
。请尝试使用
numbers1[0]
numbers1[numbers.length-1]

你应该考虑改变你的ListPoad函数。从我所看到的,它什么也不做,因为返回语句在函数括号之外。在函数里面也没有变量<代码> LSTSTEP< /COD>。请尝试以下内容:

public static string firstAndLast(int[] numberArray)
{
    return numberArray[0] + ", " + numberArray[numberArray.length - 1];
}
那就这样说吧:

System.out.println(firstAndLast(numbers1));
不能在数组上使用
.get(0)
,必须使用
数组[0]


在您的情况下,它将是:
numbers1[0]
使用数组列表添加随机数,然后打印最后一个和第一个。

public static int[]lastStep(int[]numbers1){//获取第一个int[]firstElement=numbers1.Array[0];//最后一个int[]lastElement=numbers1.Array[numbers1.size()-1];}return lastStep;我得到一个错误:数组无法解析或不是一个字段您不使用
numbers1。数组[0]
,它只是
numbers1[0]
我试过了,但仍然有一个错误。查看我的代码更新到您上次编辑时的情况:您没有使用
numbers1.array[0]
,它只是
numbers1[0]
System.out.println(firstAndLast(numbers1));