首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >试图将int计数器读取到字符串method[java]中

试图将int计数器读取到字符串method[java]中
EN

Stack Overflow用户
提问于 2015-09-16 05:26:12
回答 2查看 63关注 0票数 1

我正在尝试将一个计数器变量读入另一个方法(PrintPartiallyFilledArray),以便计算和打印数组中正确的字符串元素数。但是,每当我试图编译时,它都会说,由于无法找到计算变量,所以不能编译。如何使第二个方法知道计数器变量的值?

代码语言:javascript
运行
复制
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]); }

    }

}

EN

Stack Overflow用户

发布于 2015-09-16 06:01:54

还可以使用

代码语言:javascript
运行
复制
public static void printPartiallyFilledArray(String[] array, int counter) 

并利用它。

票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32600392

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档