1

在如何计算百分比方面遇到了一些麻烦(缺乏理解)。我需要找出:

Q1。如何获得每个字母频率的百分比(满分 100)?,也是
Q2。如何获取段落中每个第一个单词的字母频率?

到目前为止,这是我的代码:

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

public class LetterFrequency
{

  public static void main(String[] args )
  {
    char[] capital = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J','K', 'L', 'M', 'N',
                       'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};

    char[] small = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
                     'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };

    Scanner scan;
    try {
      scan = new Scanner(new File("F:/programming principles/Programming Principles - PART B/enciphered.txt"));
    } catch (Exception e) {
      System.out.println("File not found");
      return;
    }
    int[] count = new int[26];
    while(scan.hasNextLine()) {
      String line = scan.nextLine();
      System.out.println("Line read: " + line);
      char[] digit = line.toCharArray();
      for(int i = 0; i < digit.length; i++) {
        for(int j = 0; j < 26; j++) {
          if(digit[i] == capital[j] || digit[i] == small[j]) {
            count[j]++;
            break;
          }
        }
      }
    }
    System.out.println("");
    System.out.println("Comlete count");
    for (int i = 0; i < 26; i++)
    {
      System.out.print(" " + small[i]);
      System.out.println(" " + count[i]);
      //calculate percentage for the full count
    }

  }
} 
4

4 回答 4

1

要计算百分比,您需要跟踪total已扫描的字符数以及每个字符的计数。每次阅读一行时,将该行中的字符数添加到总数中。

最后,当您打印出计数时,您只需将计数除以扫描的字符总数(这将为您提供一个介于 0 - 1 之间的数字),然后将其乘以 100 得到此字符的百分比。

( count[ i ] / total ) * 100
于 2012-05-17T12:35:28.947 回答
0

当您计算每个字母时,只需增加一个全局计数器(总数),然后将每个字母的数量除以总数 * 100。

从您的代码中,我添加了 //New :

int total = 0;   // New
for(int i = 0; i < digit.length; i++) {
for(int j = 0; j < 26; j++) {
if(digit[i] == capital[j] || digit[i] == small[j]) {
count[j]++;
total++;   // New
break;
}
}
}
}
System.out.println("");
System.out.println("Complete count");
for (int i = 0; i < 26; i++)
{
System.out.print(" " + small[i]);
System.out.println(" " + count[i]);
//calculate percentage for the full count
if (total > 0)      // New
  System.out.println(" " + ((count[i]/total) * 100) + "%");   // New
}
于 2012-05-17T12:31:39.433 回答
0

假设这是您的输入

a A b c d a A B C a

并且您想找到百分比,然后输出将如下所示

a = 50% (as it is 5 time out of 10 alphabet)
b = 20% (as it is 2 time out of 10 alphabet)
c = 20% (as it is 2 time out of 10 alphabet)
d = 10% (as it is 1 time out of 10 alphabet)

总共是 10 个字母。

以下是您需要做的更改

int myTotal = 0;
while(scan.hasNextLine()) {
  String line = scan.nextLine();
  System.out.println("Line read: " + line);
  char[] digit = line.toCharArray();
  for(int i = 0; i < digit.length; i++) {
    for(int j = 0; j < 26; j++) {
      if(digit[i] == capital[j] || digit[i] == small[j]) {
        count[j]++;
        myTotal = myTotal + 1;
        break;
      }
    }
  }
}
System.out.println("");
System.out.println("Comlete count");
for (int i = 0; i < 26; i++)
{
  System.out.print(" " + small[i]);
  System.out.print(" " + count[i]);
  if (count[i] > 0)
    System.out.println(" " + (((float) count[i]/myTotal)*100) + "%");
  else
    System.out.println(" 0%");
  //calculate percentage for the full count
}

我在您的代码中所做的更改是

  1. 添加变量为

    int myTotal = 0;

  2. 添加了相同的变量来计算总字母。

    我的总计 = 我的总计 + 1;

  3. 从下一行删除 ln

    System.out.println(" " + count[i]);

  4. 添加了几行作为

    if (count[i] > 0) System.out.println(" " + ((count[i]/myTotal)*100) + "%"); 否则 System.out.println("0%");

如果您有任何疑问,请告诉我

更新 1

我改变了

    System.out.println(" " + ((count[i]/myTotal)*100) + "%");

    System.out.println(" " + (((float) count[i]/myTotal)*100) + "%");
于 2012-05-17T13:06:20.820 回答
0

使用以下代码:

import java.util.Scanner;

public class JavaProgram {

    public static void main(String[] args) {
        calc();
    }

    public static void calc() {
        int i, j, k;
        String str;
        char c, ch;
        Scanner scan = new Scanner(System.in);

        System.out.print("Enter a String : ");
        str = scan.nextLine();

        i = str.length();
        for (c = 'A'; c <= 'z'; c++) {
            k = 0;
            for (j = 0; j < i; j++) {
                ch = str.charAt(j);
                if (ch == c) {
                    k++;
                }
            }
            if (k > 0) {
                float e1 = k;
                float e2 = i;
                double r = (e1 / e2) * 100;

                System.out.println("The character " + c + " has occurred for " + k + " times and its percentage is  " + r + "%");
            }
        }
    }
}
于 2016-03-07T13:54:05.143 回答