如何调用一个变量FROM另一个方法INSIDE主要执行数学计算的方法

时间:2017-04-26 09:26:43

标签: java math methods count

也许我只是愚蠢(可能)但在过去的五个小时里,我一直在努力解决这个问题,我真的无法理解。这个网站/谷歌上没有任何东西似乎对我有帮助;每个人都想知道如何在另一种方法中调用main方法中定义的方法,但我试图以相反的方式进行。我是java新手,但我知道你不能直接从一个方法调用一个变量到另一个方法。但是,我尝试了很多不同的迭代尝试获取值并且NOTHING正在编译,我一遍又一遍地得到相同的错误:“错误:找不到所有变量的符号”。

我所要做的就是阅读一个文本文件并打印出x长度达到13的单词的百分比,并说明文档中有多少这样的单词,如“单字母的比例: .7%(2个单词)“一直打印到”13个字母的比例:80.7%(7000个单词)“(这是输出应该看起来的样子,我知道它不漂亮)。

无论如何,请帮助我,因为我被卡住并撕裂了我的头发。

import java.util.*;
import java.io.*;

public class FileReader
{
public static void main (String [] args)throws FileNotFoundException
{

    WordCount();
    WordLengthCount();

    File file = new File("RomeoAndJuliet.txt");
    Scanner keyboard = new Scanner(new FileInputStream(file));

    System.out.println("Proportion of 1-letter words: " + count1/count + "% (" + count1 + " words)");
    System.out.println("Proportion of 2-letter words: " + count2/count + "% (" + count2 + " words)");
    System.out.println("Proportion of 3-letter words: " + count3/count + "% (" + count3 + " words)");
    System.out.println("Proportion of 4-letter words: " + count4/count + "% (" + count4 + " words)");
    System.out.println("Proportion of 5-letter words: " + count5/count + "% (" + count5 + " words)");
    System.out.println("Proportion of 6-letter words: " + count6/count + "% (" + count6 + " words)");
    System.out.println("Proportion of 7-letter words: " + count7/count + "% (" + count7 + " words)");
    System.out.println("Proportion of 8-letter words: " + count8/count + "% (" + count8 + " words)");
    System.out.println("Proportion of 9-letter words: " + count9/count + "% (" + count9 + " words)");
    System.out.println("Proportion of 10-letter words: " + count10/count + "% (" + count10 + " words)");
    System.out.println("Proportion of 11-letter words: " + count11/count + "% (" + count11 + " words)");
    System.out.println("Proportion of 12-letter words: " + count12/count + "% (" + count12 + " words)");
    System.out.println("Proportion of 13-letter words: " + count13/count + "% (" + count13 + " words)");
}


public static int WordCount(int n)throws FileNotFoundException
{
    File file = new File("RomeoAndJuliet.txt");
    Scanner keyboard = new Scanner(new FileInputStream(file));
    int countABC=0;
    while(keyboard.hasNext())
    {
        keyboard.next();
        countABC++;
    }
    return countABC;
}

public static int WordLengthCount(int n) throws FileNotFoundException
{
    File file = new File("RomeoAndJuliet.txt");
    Scanner keyboard = new Scanner(new FileInputStream(file));

    int count1 = 0;
    int count2 = 0;
    int count3 = 0;
    int count4 = 0;
    int count5 = 0;
    int count6 = 0;
    int count7 = 0;
    int count8 = 0;
    int count9 = 0;
    int count10 = 0;
    int count11 = 0;
    int count12 = 0;
    int count13 = 0;

    int blob = 0; // so that if statement runs

    while(keyboard.hasNext())
    {
        if (keyboard.next().length() == 1)
        {
            count1++;
            keyboard.next();
            return count1;
        }
        else if (keyboard.next().length() == 2)
        {
            count2++;
            keyboard.next();
            return count2;
        }
        else if (keyboard.next().length() == 3)
        {
            count3++;
            keyboard.next();
            return count3;
        }
        else if (keyboard.next().length() == 4)
        {
            count4++;
            keyboard.next();
            return count4;
        }
        else if (keyboard.next().length() == 5)
        {
            count5++;
            keyboard.next();
            return count5;
        }
        else if (keyboard.next().length() == 6)
        {
            count6++;
            keyboard.next();
            return count6;
        }
        else if (keyboard.next().length() == 7)
        {
            count7++;
            keyboard.next();
            return count7;
        }
        else if (keyboard.next().length() == 8)
        {
            count8++;
            keyboard.next();
            return count8;
        }
        else if (keyboard.next().length() == 9)
        {
            count9++;
            keyboard.next();
            return count9;
        }
        else if (keyboard.next().length() == 10)
        {
            count10++;
            keyboard.next();
            return count10;
        }
        else if (keyboard.next().length() == 11)
        {
            count11++;
            keyboard.next();
            return count11;
        }
        else if (keyboard.next().length() == 12)
        {
            count12++;
            keyboard.next();
            return count12;
        }
        else if (keyboard.next().length() == 13)
        {
            count13++;
            keyboard.next();
            return count13;
        }
    }   return blob;
}
}

谢谢!

3 个答案:

答案 0 :(得分:0)

使变量成为静态,并从main方法

调用它

答案 1 :(得分:0)

您的代码中存在一些错误,但最大的问题是当您找到具有特定长度的单词时,您将返回计数。

您可能想要创建一个类(比如Document),它具有您在WordLengthCount(int count1,int count2等)中列为变量的属性。由于属性通常应该是私有的,我建议使用增量计数方法。

最后,你的WordLengthCount可以为正确的单词类型调用increment count方法,并返回你创建的对象。

此外,我建议不要创建13个变量,而是建议使用数组

int[] wordCount= new int[13];

答案 2 :(得分:0)

您正在尝试访问某个其他功能中的一个功能的局部变量。这是不可能的。顾名思义,局部变量是声明它们的块或函数的本地变量。如果要全局访问这些变量,请将它们设为类级变量,即在类体内声明它们,但在任何其他函数之外。此外,如果要在不创建类对象的情况下从静态方法访问它们,请将这些变量设置为静态。