Java I';我试图用扫描仪计算文档中每个字母的百分比,但我能';我不知道如何正确地计算和显示它

Java I';我试图用扫描仪计算文档中每个字母的百分比,但我能';我不知道如何正确地计算和显示它,java,frequency,alphabet,Java,Frequency,Alphabet,首先我上传了我正在使用的文件,宪法。然后,我创建了两个数组,一个用于字母表的长度,另一个用于计算频率。我创建了一个while循环来读取文档中的每一行并计算每一个字母的频率,我只是不知道如何计算每个字母占每个字母的百分比,因为在我将它放入条形图之后 int [ ]lettersLabels = new int [26]; int [ ]lettersFrequency = new int [26]; String line; //initializ

首先我上传了我正在使用的文件,宪法。然后,我创建了两个数组,一个用于字母表的长度,另一个用于计算频率。我创建了一个while循环来读取文档中的每一行并计算每一个字母的频率,我只是不知道如何计算每个字母占每个字母的百分比,因为在我将它放入条形图之后

        int [ ]lettersLabels = new int [26];
        int [ ]lettersFrequency = new int [26];
        String line;

//initialize arrays
        letterLabels = 0;
        lettersFrequency = 0;

        Scanner sc = new Scanner(constitution);

         while(sc.hasNextLine())
         {
            line = sc.nextLine();
            line = sc.toLowerCase();
            char[] characters = line.toCharArray();
         
            for (int i = 0; i< characters.length ; i++) 
            {
                if((characters[i] >='a') && (characters[i]<='z'))
                {
                     lettersLabels[characters[i] -'a' ]++;
                }
            }
         }
         
         for (int i = 0; i < 26; i++) 
         {
          lettersFrequency = 'a' + [characters[i] -'a' ]++;
          lettersFrequency = lettersFrequency / 100;
          System.out.println(lettersFrequency);
         }

int[]字母slabels=新int[26];
int[]字母频率=新int[26];
弦线;
//初始化数组
字母标签=0;
字母频率=0;
扫描仪sc=新扫描仪(构成);
while(sc.hasNextLine())
{
line=sc.nextLine();
line=sc.toLowerCase();
char[]characters=line.toCharArray();
for(int i=0;i如果((characters[i]>='a')&&(characters[i]百分比是
比率*100
1/2=0.5=(0.5*100)%=50%
),这里的比率是
lettersLabels[i]/totalLetters
。因此,为了找到百分比,您只需使用
letteresfrequency[i]=(double)lettersLabels[i]/(double)totaletters*100;
。因此,您可能需要在开始时使用变量
totaletters
来计算字母数

您的代码中有一个错误,即使用
0
初始化数组。您已使用
int[]arr=new int[26]初始化数组;
,因此您不需要这两行,因为使用
int
初始化数组是错误的类型,这会产生错误。此外,当将
lettsfrequency
的某些部分设置为某物时,您需要指定索引,如
lettsfrequency[i]
,当获取字符超出范围时,不要使用
字符
,因此必须使用
字母标签
数组


希望这能起作用!

在这里,我用说明修复了一些错误,请参见代码注释以获取解释:

        int [ ]lettersLabels = new int [26];
        int [ ]lettersFrequency = new int [26];
        String line;
        
        int totalLetter =0; // needed to calculate %

// No need to initialize, it's already initialized with 0 values.
// remove those lines.
//initialize arrays
//        letterLabels = 0;
//        lettersFrequency = 0;

        Scanner sc = new Scanner(constitution);
        
         while(sc.hasNextLine())
         {
            line = sc.nextLine();
            // need to lower case the current line
            line = line.toLowerCase();
            char[] characters = line.toCharArray();
         
            for (int i = 0; i< characters.length ; i++) 
            {
                if((characters[i] >='a') && (characters[i]<='z'))
                {
                     // lets count the letter frequency.
                     totalLetter++;
                     lettersFrequency[characters[i] -'a' ]++;
                }
            }
         }

         // get rid of divide by zero exception
         if(totalLetter ==0) totalLetter =1;
         
         for (int i = 0; i < 26; i++) 
         {
          char ch = 'a'+i;
          // let's count the parentage of each letter.
          double parcentage = (lettersFrequency[i]*100.00)/totalLetter;
          System.out.println("parcentage of "+ ch+ " is: " +lettersFrequency +"%");
         }
int[]字母slabels=新int[26];
int[]字母频率=新int[26];
弦线;
int totaletter=0;//需要计算%
//无需初始化,它已用0值初始化。
//拆下那些线。
//初始化数组
//字母标签=0;
//字母频率=0;
扫描仪sc=新扫描仪(构成);
while(sc.hasNextLine())
{
line=sc.nextLine();
//需要降低当前行的大小写
line=line.toLowerCase();
char[]characters=line.toCharArray();
for(int i=0;i如果((字符[i]>='a')&&(字符[i]非常感谢您的反馈,这非常有帮助!