在如何计算百分比方面遇到一些麻烦(缺乏理解)。我需要找出:
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
}
}
}
答案 0 :(得分:1)
要计算百分比,您需要跟踪已扫描的total
个字符数,以及每个字符的计数。每次读取一行时,请将该行中的字符数添加到总计数中。
然后在最后,当你打印出你的计数时,你需要简单地将计数除以扫描的字符总数(这将给你一个介于0 - 1之间的数字),然后将其乘以100获得这个角色的百分比。
( count[ i ] / total ) * 100
答案 1 :(得分: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
}
答案 2 :(得分: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
}
我在您的代码中所做的更改
添加变量
int myTotal = 0;
添加了用于计算总字母数的相同变量。
myTotal = myTotal + 1;
从下线删除了ln
System.out.println(“”+ count [i]);
添加了几行
if(count [i]&gt; 0) System.out.println(“”+((count [i] / myTotal)* 100)+“%”); 其他 System.out.println(“0%”);
如果您有任何疑问,请与我们联系
我已经改变了
System.out.println(" " + ((count[i]/myTotal)*100) + "%");
到
System.out.println(" " + (((float) count[i]/myTotal)*100) + "%");
答案 3 :(得分: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 + "%");
}
}
}
}