正在尝试将int计数器读入字符串方法[java]
我试图将一个计数器变量读入另一个方法(printPartiallyFilledArray),以便计算并打印数组中正确数量的字符串元素。但是,每当我尝试编译时,它都会说我无法编译,因为无法找到calculate变量。如何使第二个方法知道计数器变量的值正在尝试将int计数器读入字符串方法[java],java,Java,我试图将一个计数器变量读入另一个方法(printPartiallyFilledArray),以便计算并打印数组中正确数量的字符串元素。但是,每当我尝试编译时,它都会说我无法编译,因为无法找到calculate变量。如何使第二个方法知道计数器变量的值 public static void main(String [] args) { // Instantiate a String array that can contain 10 items. Scanner keyboard =
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]); }
}
publicstaticvoidmain(字符串[]args)
{
//实例化一个可以包含10项的字符串数组。
扫描仪键盘=新扫描仪(System.in);
int数组_SIZE=10;
字符串[]数组=新字符串[数组大小];
int计数器=0;
//将主题名称读入此数组
//数一数已经读了多少。
//可能少于10个。
System.out.println(“请输入主题名称或输入q以退出:”;
字符串主题=keyboard.nextLine();
(subject.equals(“q”)!=true&&counter有两种方法可以告知方法计数器的值:
在主函数之外全局定义变量计数器。这将允许它随时知道全局更新的计数器变量的值
例如:
public class Solution{
public static int counter =0;
public static void main(String[] args){
---do things as before----
}
public static void printPartiallyFilledArray(String[] array){
---do things as before----
}
}
通过将函数参数重新定义为,将计数器的值传递给函数printPartiallyFilledArray:
printPartiallyFilledArray(String[] array, int counter)
并通过调用
printPartiallyFilledArray(array, counter);
有两种方法可以告知方法计数器的值:
在主函数之外全局定义变量计数器。这将允许它随时知道全局更新的计数器变量的值
例如:
public class Solution{
public static int counter =0;
public static void main(String[] args){
---do things as before----
}
public static void printPartiallyFilledArray(String[] array){
---do things as before----
}
}
通过将函数参数重新定义为,将计数器的值传递给函数printPartiallyFilledArray:
printPartiallyFilledArray(String[] array, int counter)
并通过调用
printPartiallyFilledArray(array, counter);
您还可以使用
public static void printPartiallyFilledArray(String[] array, int counter)
并使用它。您还可以使用
public static void printPartiallyFilledArray(String[] array, int counter)
并使用它。在主方法之外声明它好的,谢谢,我会试试这个。你的代码在我本地的IntelliJ安装程序上构建并运行良好。你到底得到了什么错误?非常感谢Mohammad,我在主方法之外声明了它,它起了作用。我真的很感谢你的帮助。Tim,我收到的错误表明它可以找不到计数器变量。我按照Mohammad的说明在我的主方法之外声明了它,现在对我来说工作正常。在你的主方法之外声明它好的,谢谢,我会尝试这个。你的代码在我的本地IntelliJ安装程序上构建并运行良好。你到底遇到了什么错误?非常感谢Mohammad,我决定我把它放在我的主方法之外,它就工作了。我真的很感谢你的帮助。蒂姆,我收到的错误说明它找不到计数器变量。我按照穆罕默德的指示,把它放在我的主方法之外,它现在对我来说工作正常。