我正在尝试将一个计数器变量读入另一个方法(PrintPartiallyFilledArray),以便计算和打印数组中正确的字符串元素数。但是,每当我试图编译时,它都会说,由于无法找到计算变量,所以不能编译。如何使第二个方法知道计数器变量的值?
public static void main(String [] args)
{
// Instantiate a String array that can contain 10 items.
Scanner keyboard = new Scanner(System.in);
int ARRAY_SIZE = 10;
String[] array = new String[ARRAY_SIZE];
int counter = 0;
// Read names of subjects into this array
// and count how many have been read in.
// There may be fewer than 10.
System.out.println("Please enter a subject name or enter q to quit: ");
String subject = keyboard.nextLine();
while(subject.equals("q")!=true && counter<ARRAY_SIZE)
{
array[counter] = subject;
counter++;
System.out.println("Please enter a subject name or enter q to quit: ");
subject = keyboard.nextLine();
}
// Call printPartiallyFilledArray to print the names in the array.
printPartiallyFilledArray(array);
}
/**
* Method printPartiallyFilledArray prints the String values
* in a partially-filled array, one per line. Only the
* significant items in the array should be printed.
*
* @param array the array of Strings to be printed on the screen
* @param count the number of items in the partially-filled array
*/
public static void printPartiallyFilledArray(String[] array)
{
System.out.println("The array elements: ");
for (int i = 0; i < counter; i++){
System.out.println(array[i]); }
}}
发布于 2015-09-16 06:01:54
还可以使用
public static void printPartiallyFilledArray(String[] array, int counter) 并利用它。
https://stackoverflow.com/questions/32600392
复制相似问题