Java 如何从main方法中的另一个方法调用变量来执行数学计算

Java 如何从main方法中的另一个方法调用变量来执行数学计算,java,math,methods,count,Java,Math,Methods,Count,也许我只是个傻瓜(可能),但我在过去的五个小时里一直在努力解决这个问题,我真的搞不懂。这个网站/谷歌上的任何东西似乎都帮不了我;每个人都想知道如何在另一个方法中调用在main方法中定义的方法,但我正试图用另一种方法来实现。我是java新手,但我知道不能直接将变量从一个方法调用到另一个方法中。然而,我尝试了很多不同的迭代来获取值,但是没有任何东西在编译,我一次又一次地得到相同的错误:我的所有变量都出现了“错误:找不到符号” 我所要做的就是读取一个文本文件,打印出x长度到13的单词的百分比,并说出文

也许我只是个傻瓜(可能),但我在过去的五个小时里一直在努力解决这个问题,我真的搞不懂。这个网站/谷歌上的任何东西似乎都帮不了我;每个人都想知道如何在另一个方法中调用在main方法中定义的方法,但我正试图用另一种方法来实现。我是java新手,但我知道不能直接将变量从一个方法调用到另一个方法中。然而,我尝试了很多不同的迭代来获取值,但是没有任何东西在编译,我一次又一次地得到相同的错误:我的所有变量都出现了“错误:找不到符号”

我所要做的就是读取一个文本文件,打印出x长度到13的单词的百分比,并说出文档中这些单词的数量,如“1个字母的单词的比例:.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;
}
}

谢谢!

将变量设为静态并从main方法调用它

您的代码中有几处错误,但最大的错误是,当您找到具有特定长度的单词时,将返回计数

您可能希望创建一个类(比如文档),该类的属性在WordLengthCount(int count1、int count2等)中列为变量。由于属性通常是私有的,因此我建议使用增量计数方法

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

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

int[] wordCount= new int[13];

您试图在其他函数中访问某个函数的局部变量。这是不可能的。顾名思义,局部变量是声明它们的块或函数的局部变量。如果您想全局访问这些变量,请将它们设为类级变量,即在类体内但在任何其他函数外声明它们另外,如果您想从静态方法访问它们而不创建类的对象,请将这些变量设置为静态。

您的第一个问题(在加载中)是在调用
WordCount()时没有传递参数
。另外,在方法之外定义
count1
和其他。不幸的是,你的代码设计和编写方式不正确。你有具体的算法吗?你知道结构化编程的原理吗?我建议你在谷歌搜索文本文件java中的
count words。goodluckI正在投票结束这个问题因为你的算法不能以这种方式工作,所以请重新考虑你的算法-如果单词超过
13
字符会发生什么?我会使用字典,wordlengtjh作为键,instance count作为值。虽然这可能是解决问题的一个有价值的提示,但一个好的答案也会演示解决方法。请请举例说明你的意思。或者,把它当作评论来写。