import java.util.Scanner ;
/**
* The Letter Counter program counts the frequency of the letters of the
* alphabet in some lines of text. After a period and a return, the computer
* displays the frequency.
*
* @author Quang Pham
* @version Module 8, Homework Project 2, 4/1/20
*
* Algorithm:
*
* 1. User enters multiple lines of text.
* 2. The program will read in the lines of text and display a list of all the
* letters that occur in the text, with the number of times the letter occurs.
* 3. The last line of input should be ended with a period, which serves as a
* sentinel value.
*
* Problem description:
* Write a program that will read in multiple lines of text from the user
* and display a list of all the letters that occur in the text, along with the
* number of times each letter occurs.
*
* The last line of input from the user should end with a period, which will
* serve as a sentinel value. Once the last line has been entered, the counts
* for all letters entered by the user should be listed in alphabetical order as
* they are output. Use an array of base type int of length 28, so that each
* indexed variable contains the count of how many letters there are. Array
* indexed variable 0 contains the number of a’s, array indexed variable 1 contains
* the number of b’s and so forth. Allow both uppercase and lowercase letters as
* input, but treat uppercase and lowercase versions of the same letter as being equal.
*
* Hints: You might find it helpful to define a "helper" method that takes a character
* as an argument and returns an int value that is the correct index for that character,
* such as ‘a’ returning 0, ‘b’ returning 1, and so forth. Note that you can use a
* typecast to change a char to an int, like (int) letter. This will not get the
* number you want, but if you subtract (int) 'a', you will then have the right index.
* Allow the user to repeat this task until the user says she or he is finished.
*
* A dialog may look something like the following
*
* Enter several lines of text to analyze. (Copy and paste to save time.) When done,
* end a line with a period and press return.
* 1: Four score and seven years ago our forefathers
* 2: brought forth upon this continent a new nation,
* 3: conceived in liberty, and dedicated to the
* 4: proposition that all men are created equal.
*
* Here's the counts of characters:
* a: 13
* b: 2
* c: 6
* d: 7
* e: 19
* f: 4
* g: 2
* h: 6
* i: 9
* l: 4
* m: 1
* n: 14
* o: 15
* p: 3
* q: 1
* r: 12
* s: 6
* t: 15
* u: 5
* v: 2
* w: 1
* y: 2
*
* Again, you can submit a single class for this project which contains your main
* method and any helper methods where you feel they can be used.
*
* Along with the file containing your program, submit three print screens or screen
* snips, each with several lines of text entered by the user, and the count for each
* character (a-z).
*/
public class LetterCounter
{
public static void main(String[] args) {
int frequency = 0 ;
char character = ' ' ;
String linesOfText = " " ;
char[] alphabet = new char[28] ; // new alphabet array
for(char ch = 'a'; ch <= 'z'; ++ch)// fills alphabet array with the alphabet
{
alphabet[ch-'a']=ch ;
}
System.out.println("Welcome to the Letter Count program.") ;
System.out.println("Please enter some lines of text followed by a period and a return.") ;
Scanner keyboard = new Scanner(System.in) ;
linesOfText = keyboard.nextLine() ;
System.out.println("Letter Frequency") ;
for (int i = 0; i < alphabet.length; i++)
{ frequency = 0 ;
for (int j = 0; j < linesOfText.length(); j++) {
character = linesOfText.charAt(j) ;
if (character == alphabet[i]) {
frequency++ ;
}
}
System.out.println(alphabet[i] + "\t\t" + frequency) ;
i++;
}
}
}
你问你做错了什么。好吧,你很亲密,你只做错了两件事。
char[]alphabet=new char[28]
应为26。 for (int i = 0; i < alphabet.length; i++)
{ frequency = 0 ;
for (int j = 0; j < linesOfText.length(); j++) {
character = linesOfText.charAt(j) ;
if (character == alphabet[i]) {
frequency++ ;
}
}
System.out.println(alphabet[i] + "\t\t" + frequency) ;
i++;
}
在第一个循环中,您使用i++
来增加i,但也在print语句之后的末尾增加它。最后一个应该去掉。
int count[] = new int[26];
count[c - 'a']++; ///increments the count for letter i.
问题内容: 这是来自pyschools的问题。 我确实做对了,但我猜测会有一个更简单的方法。这是最简单的方法吗? 看起来应该像这样: 问题答案: 在2.7+中: 较早的版本(2.5或更高版本,到目前为止):
问题内容: 我正在做一个作业,在该作业中,我必须编写程序以读取用户的字符串,并打印出字符串中出现次数的字母。 例如,“ Hello world”应该打印出“ h = 1 e = 1 l = 3 o = 2 …等”,但是我只写“ hello world”和字母总数。 我不能使用hashmap函数,只能使用数组。有人可以给我一两个提示,提示如何从下面的书面代码继续进行操作以获得我的首选功能?我不完全了
问题内容: 如何使用MySQL查询来计算大写字母?我现在正在尝试 但这给我一个错误的说法: 我猜,我不允许在AGAINST子句中使用列表,这很烂 那么,有没有办法实现这一目标? 问题答案: 试试这个功能- 例子:
编写一个程序,当给定一个代表地毯的字符串时,输出其价格。示例:abacx答案:20(长度5乘以4种不同类型) 以下是我到目前为止的情况。以下是测试案例3a)qiraat 3b)cdefghijklmnopqrstuwxyz 3c)warrior 3d)SupercalibragilisticExpialidious
问题内容: 我有字母“ a”,“ b”,“ c”。我希望我的结果在TSQL中分别为“ b”,“ c”,“ d”。我会用什么来实现这一目标? 问题答案: 使用得到字符的值,增加一个,并使用转换的值返回到一个字符。
问题内容: 我试图实现的目标很简单,但是很难解释,而且我不知道在postgres中它是否甚至有可能实现。我处于一个基本的水平。,等等基本的东西。 我试图计算包含特定字母/数字的行数,并根据字母/数字显示该计数。 即有多少行的条目包含“ a / A”(不区分大小写) 我要查询的表是电影名称的列表。我要做的只是对“ az”和“ 0-9”进行分组并计数,然后输出总计。我可以依次运行36个查询: 然后在结