Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.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
将数组作为参数传递;使用Scanner类使用用户输入在Java中设置数组_Java_Arrays_Methods_User Input - Fatal编程技术网

将数组作为参数传递;使用Scanner类使用用户输入在Java中设置数组

将数组作为参数传递;使用Scanner类使用用户输入在Java中设置数组,java,arrays,methods,user-input,Java,Arrays,Methods,User Input,我试图获取用户输入,将其放入数组中,显示数组,然后打印所有大于用户提供的“n”值的值。我想我很接近了,但我无法获得用户输入以进入阵列。我在eclipse中调用该方法(最底部的main)时一直遇到一个错误,“arrayValues”无法解析为变量: import java.util.Scanner public class LargerThanN{ //initialize n static int n; static int arraySize; //setup the array sta

我试图获取用户输入,将其放入数组中,显示数组,然后打印所有大于用户提供的“n”值的值。我想我很接近了,但我无法获得用户输入以进入阵列。我在eclipse中调用该方法(最底部的main)时一直遇到一个错误,“arrayValues”无法解析为变量:

import java.util.Scanner

public class LargerThanN{

 //initialize n
static int n;

static int arraySize;

//setup the array
static int [] integerArray = new int [] {};

public static void printGreaterThanN(int[] integerArray, int n) {

    for (int i = 0; i < integerArray.length; i++) {
            if (integerArray[i]>n) { 
                System.out.println(integerArray[i]);

            }
    } 

}

public static int[] fillArrayWithUserInt() {
    Scanner sc = new Scanner(System.in);

    System.out.println("How big will the array be?");
    int arraySize = sc.nextInt();
    sc.nextLine(); // clears rest of input, including carriage return

    int[] integerArray = new int[arraySize];

    System.out.println("Enter the " + arraySize + " numbers now.");

        for (int i = 0; i < integerArray.length; i++) {
        integerArray[i] = sc.nextInt();
    }

    return integerArray;

    }





/**
 * This method prints the array to the standard output
 * @param array
 */
private static void displayArray( int[] integerArray) {

    for (int i = `0; i < integerArray.length; i++) {
        System.out.print(integerArray[i] + " ");
    }
}   
但是现在我的输出看起来像:

阵列有多大

四,

现在输入4个数字

1234

阵列有多大

三,

现在输入3个数字

1 2 3

1234

您希望将其余数字与哪个数字进行比较?您的n值为:

二,

三,


4

这是我的解决方案,请查看

import java.util.Scanner;

public class LargerThanN {
    static int[] integerArray = null;
    static int n ;

    public static void printGreaterThanN() {

        for (int i = 0; i < integerArray.length; i++) {
            if (integerArray[i] > n) {
                System.out.println(integerArray[i]);
            }
        }

    }

    public static void fillArrayWithUserInt() {

        Scanner sc = new Scanner(System.in);

        System.out.println("How big will the array be?");
        int arraySize = sc.nextInt();
        sc.nextLine(); // clears rest of input, including carriage return

        integerArray = new int[arraySize];

        System.out.println("Enter the " + arraySize + " numbers now.");

        System.out.println("What are the numbers in your array?");

        for (int i = 0; i < integerArray.length; i++) {
            integerArray[i] = sc.nextInt();
        }

        System.out.println("To which number would you like to compare the rest? Your n value is: ");
        n = sc.nextInt();

    }

    /**
     * This method prints the array to the standard output
     * 
     * @param array
     */
    private static void displayArray() {

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

    public static void main(String[] args) {

        fillArrayWithUserInt();

        displayArray();

        printGreaterThanN();

    }
}
import java.util.Scanner;
公共级大教堂{
静态int[]integerArray=null;
静态int n;
公共静态无效printGreaterThanN(){
for(int i=0;in){
System.out.println(integerArray[i]);
}
}
}
公共静态无效fillArrayWithUserInt(){
扫描仪sc=新的扫描仪(System.in);
System.out.println(“阵列有多大?”);
int arraySize=sc.nextInt();
sc.nextLine();//清除其余输入,包括回车
integerArray=新整数[arraySize];
System.out.println(“立即输入“+arraySize+”数字”);
System.out.println(“数组中的数字是多少?”);
for(int i=0;i
请注意,以下代码在java中不起任何作用

公共无效集(int n,int值){
n=数值;
}
您似乎在许多函数中编写了这样的代码,其中应该返回一个值

例如,函数定义:

static void fillArrayWithUserInt(int[]integerArray,int-arraySize,int-arrayValues,int-n)

应该写为:

static int[]fillArrayWithUserInt()

可按如下方式实施

public static int[]fillArrayWithUserInt(){
扫描仪sc=新的扫描仪(System.in);
System.out.println(“阵列有多大?”);
int arraySize=sc.nextInt();
sc.nextLine();//清除其余输入,包括回车
int[]integerArray=新的int[arraySize];
System.out.println(“立即输入“+arraySize+”数字”);
System.out.println(“数组中的数字是多少?”);
for(int i=0;i
上述函数将询问用户数组大小。使用给定大小创建数组。然后提示用户使用正确的值填充数组。然后返回在此过程中创建的数组

现在,您必须进行不同的处理,即查找要比较的值。这必须在
fillArrayWithUserInt
函数之外完成

像这样:

publicstaticvoidmain(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
int[]数组=fillArrayWithUserInt();
显示阵列(数组);
System.out.println(“您希望将其余的数字与哪个数字进行比较?您的n值为:”);
int n=sc.nextInt();
printgreeaterthann(数组,n);
}
最后,您不需要在类的顶部声明任何静态变量

这些行都可以删除:

//初始化
静态int n;
静态int数组化;
//设置阵列
静态int[]整数数组=新int[]{};

您从未声明过
ArrayValue
。也许您忘记了什么?有两个数组声明了一个在
printgreeaterthann()
fillArrayWithUserInt()中
指定要使用的函数,或者删除初始化数组后声明的第一个函数,它的大小和fill func中的值。我尝试在方法中声明ArrayValue,但它会给我一个重复的标志variable@FishingCode那么我应该从FillArrayWithUserInt()中删除int[]integerArray吗?如果您在那里删除它,那么您仍然需要在函数中初始化它,或者只是删除第一个函数,我看不到它的值?这非常有用。因此,如果我将n比较移到main,但是,我在运行它时返回的值不会大于它。(更新原始帖子中的代码)好的,
fill
函数将只创建一个具有指定数量元素的数组。您在
main
中的任务是使用创建的值…尝试添加
array=fillArrayWithUserInt()
main
。然后在函数调用中使用
array
…按照我的greaterThanN的设置方式,在我调用它时,它不应该打印我的值吗?到目前为止,你是这个网站上最有帮助的人,所以我非常赞赏你。是的,但你目前没有传递任何值。
        int [] array ;

        array = fillArrayWithUserInt();

        Scanner sc = new Scanner(System.in);

        fillArrayWithUserInt();

        displayArray(array);

         System.out.println("To which number would you like to compare the rest? Your n value is: ");

            n = sc.nextInt();

        printGreaterThanN(array, n); 
import java.util.Scanner;

public class LargerThanN {
    static int[] integerArray = null;
    static int n ;

    public static void printGreaterThanN() {

        for (int i = 0; i < integerArray.length; i++) {
            if (integerArray[i] > n) {
                System.out.println(integerArray[i]);
            }
        }

    }

    public static void fillArrayWithUserInt() {

        Scanner sc = new Scanner(System.in);

        System.out.println("How big will the array be?");
        int arraySize = sc.nextInt();
        sc.nextLine(); // clears rest of input, including carriage return

        integerArray = new int[arraySize];

        System.out.println("Enter the " + arraySize + " numbers now.");

        System.out.println("What are the numbers in your array?");

        for (int i = 0; i < integerArray.length; i++) {
            integerArray[i] = sc.nextInt();
        }

        System.out.println("To which number would you like to compare the rest? Your n value is: ");
        n = sc.nextInt();

    }

    /**
     * This method prints the array to the standard output
     * 
     * @param array
     */
    private static void displayArray() {

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

    public static void main(String[] args) {

        fillArrayWithUserInt();

        displayArray();

        printGreaterThanN();

    }
}