我试图接受用户输入,将其放入数组,显示数组,然后打印所有大于用户提供的 "n "值的数值。 我想我已经很接近了,但是我不能把用户输入的数据放到数组中。当我调用 "arrayValues "方法(最下面的main)时,我一直在eclipse中得到一个错误信息 不能解析为变量:
import java.util.Arrays;
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] + " ");
}
}
public static void main(String[] args) {
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);
但现在我的输出看起来像:
数组会有多大?
4
现在输入4个数字。
1 2 3 4
阵列会有多大?
3
现在输入3个数字。
1 2 3
1 2 3 4
你想和哪个数字比较其他的?你的n值是。
2
3
4
注意,下面的代码在java中没有任何作用......
public void set(int n, int value) {
n = value;
}
你好像在很多应该返回值的函数中都写了这样的代码。
例如,函数定义的:
static void fillArrayWithUserInt(int[] integerArray, int arraySize, int arrayValues, int n)
其实应该写成.NET。
static int[] fillArrayWithUserInt()
它可以实现如下
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.");
System.out.println("What are the numbers in your array?");
for (int i = 0; i < integerArray.length; i++) {
integerArray[i] = sc.nextInt();
}
return integerArray;
}
上述函数将要求用户提供数组的大小。用给定的大小创建数组。然后提示用户用正确的数值填充数组。然后返回这个过程中创建的数组。
现在你必须处理不同的是找到要比较的值。这必须在 fillArrayWithUserInt
功能。
像这样。
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] array = fillArrayWithUserInt();
displayArray(array);
System.out.println("To which number would you like to compare the rest? Your n value is: ");
int n = sc.nextInt();
printGreaterThanN(array, n);
}
最后,你应该不需要在类的顶部声明任何静态变量。
这些行都可以删除。
//initialize n
static int n;
static int arraySize;
//setup the array
static int [] integerArray = new int [] {};
这是我的解决方案,看看吧。
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();
}
}