给定的程序编译,但不以我想要的方式运行。我认为这不是应用我写的方法。有任何想法吗?为什么我的程序不使用我写的方法?
import java.util.Scanner;
public class Assignment_1_q1 {
static Scanner input = new Scanner(System.in);
public static int i;
public static int counter;
public static int n;
//main method
public static void main(String[] args) {
System.out.println("Enter n's value: ");
n = input.nextInt(); //Prompts the user to input the integer number n.
int[] table = new int[10]; //Create an array of size 10.
getArrayValues(table); //Calls the first method
matchCriteria(table, counter, n); //Calls the second methods
System.out.println("There is "+ n +"numbers greater than n!"); //display the result.
}
//the first method to input array values from the user,
//allows nonnegative numbers only to be stored into the array.
public static int[] getArrayValues(int table[]){
while (table[i] < 0)
System.out.println("Pleas try a nonnegative number!");
for (int i = 0; table[i] < table.length; i++){
System.out.println("Enter an array value: ");
table[i] = input.nextInt();
}
return table;
}
// the second method determines how many of array values are greater than the value of n.
public static int matchCriteria(int array[], int counter, int n){
counter = 0;
for(int i = 0; i < array.length && i > n;) {
if (i > n) counter++;
}
return counter;
}
}
您没有储存(或使用)调用你的方法的结果。 –