『新手』检查一下句子的英文字..
我现在学习JAVA中,所以有一些问题不慬。input: "June, July and August"
output: 4 u, 2 J, 2 n (有分大小草)
要检查一下句子中,出现最多次數的3個英文字..
我只慬写出
import java.util.Scanner;
public class Task_2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Input the word: " );
word = input.next();
不知道应该怎样用array去写

希望大哥们可以教教我

然后
word.toCharArray
然后 循环每一个char数组,按字母存map,其中value存个数
OK了 --------------------编程问答--------------------
import javax.swing.JOptionPane;
public class test {
public static void main(String[] args) {
String words = JOptionPane.showInputDialog(null,"Write a sentence." );
String lower = words.toLowerCase();
char[] ch = lower.toCharArray();
int[] freq = new int[26];
for(int i = 0; i< ch.length;i++)
{
if(ch[i] <= 122)
{
if(ch[i] >= 97)
{
freq[(ch[i]-97)]++;
}
}
}
System.out.println("Total chars " + ch.length);
for(int i = 0; i < 26; i++)
{
if(freq[i] != 0)
System.out.println(freq[i] + "\t" + ((char)(i+97)));
}
}
}
我只計算出小草的英文,不懂計算出大草的.
還有選出3個出現最多次的字母... --------------------编程问答-------------------- String lower = words.toLowerCase(); 这一步已将字母小写了,把这一行干掉。
--------------------编程问答-------------------- import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String words;
System.out.print("Write a sentence/words : " );
words = input.next();
char[] ch = words.toCharArray();
int[] freq = new int[26];
for(int i = 0; i< ch.length;i++)
{
if(ch[i] <= 90)
{
if(ch[i] >= 65)
{
freq[(ch[i]-65)]++;
}
}
}
System.out.println("\nTotal chars " + ch.length);
//this is just for styling the look of the output
System.out.println("---------------------" + "\nFrequency\tAlphabet" + "\n--------- ---------");
for(int i = 0; i < 26; i++)
{
//printing only those alphabets whose count is not '0'
if(freq[i] != 0)
System.out.println(" "+freq[i] + "\t\t " + ((char)(i+65)));
}
}
}
我將它們轉了大草,不過不能計算小草.. --------------------编程问答-------------------- 不能同時計算小草


大哥,,你可以教教我嗎

--------------------编程问答-------------------- 大哥,謝謝你,哪我再試試看,
等一會再請教你

一起加油吧

Description: Counting the frequencies of the 52 English letters
& display the top three frequencies together with their letters.
*/
import java.util.Scanner;
public class Task_2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String words;
System.out.print("Input a sentence/words : " );
words = input.next();
int[] lettersArray = new int[128];
for ( int i = 0; i < words.length(); i++ ) {
char ch= words.charAt(i);
int value = (int) ch;
if (value >= 97 && value <= 122){
lettersArray[ch-'a']++;
}
}
System.out.println("\nTotal chars " + words.length());
System.out.println("---------------------" + "\nFrequency\tAlphabet"
+ "\n--------- ---------");
for (int i = 0; i < lettersArray.length; i++) {
if(lettersArray[i]>0){
System.out.println(" "+lettersArray[i] + "\t\t " + ((char)(i+97)));
}
}
}
}
mag哥,我已經重新改寫,不過真的不懂怎樣寫你的方法,,
我剛剛有看過map的資料.
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
不過不知道怎用的

所以請mag哥出手幫幫我

Total chars 5
---------------------
Frequency Alphabet
--------- ---------
1 a
1 e
1 l
2 p
=================================================
Input a sentence/words : do you like an APPLE?
Total chars 2
---------------------
Frequency Alphabet
--------- ---------
1 d
1 o
==================================================
Input a sentence/words : APPLE
Total chars 5
---------------------
Frequency Alphabet
--------- ---------
==================================================
--------------------编程问答-------------------- 简单的写了下,没有做输入的校验,仅供参考
--------------------编程问答-------------------- 謝謝mag哥 :)
Scanner sc = new Scanner(System.in);
System.out.print("Input a sentence/words : " );
String word = sc.nextLine();
char ca [] = word.toCharArray();
Map<Character, Integer> numMap = new HashMap<Character, Integer>();
for (char c : ca) {
if (numMap.containsKey(c)) {
numMap.put(c, numMap.get(c) + 1);
} else {
numMap.put(c, 1);
}
}
System.out.println("----------------" + "\nFrequency\tAlphabet"
+ "\n--------- ---------");
for (char c : numMap.keySet()) {
System.out.println(" " + c + " " + numMap.get(c));
}
不過不知道是不是我用JCreator LE的原因,所以有3點error
cannot find symbol class Map , cannot find symbol class HashMap
{Map<Character, Integer> numMap = new HashMap<Character, Integer>();}
operator + cannot be applied to <any>.get,int
{if (numMap.containsKey(c)) {
numMap.put(c, numMap.get(c) + 1);} --------------------编程问答--------------------
++ --------------------编程问答-------------------- 去 eclipse官网去下载个eclipse吧,用着方便 --------------------编程问答--------------------
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
这个加上面,Map和HashMap需要引入的 --------------------编程问答-------------------- 哦哦..明白了

然後選出3個出現最多次的字母是否用for 就ok了

聖誕節看看android的書,再學習一下

不過我還有一個問題

選出3個出現最多次的字母是否用for 就ok了?




Yes mag Sir ,
我現在努力中,thank you very much


供参考 --------------------编程问答--------------------
Scanner sc = new Scanner(System.in);
System.out.print("Input a sentence/words : " );
String word = sc.nextLine();
char ca [] = word.toCharArray();
Map<Character, Integer> numMap = new HashMap<Character, Integer>();
for (char c : ca) {
if (numMap.containsKey(c)) {
numMap.put(c, numMap.get(c) + 1);
} else {
numMap.put(c, 1);
}
}
List<Integer> list = new ArrayList<Integer>();
list.addAll(numMap.values());
Collections.sort(list);
list = list.subList(list.size()-3, list.size());
for (char c : numMap.keySet()) {
for (Integer nu : list) {
if (numMap.get(c) == nu) {
System.out.println(c+ " " + nu);
break;
}
}
}
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
/**
* input: "June, July and August"
*
* output: 4 u, 2 J, 2 n (有分大小草)
*
* 要检查一下句子中,出现最多次數的3個英文字..
*
*
*/
public class Test002 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Input a sentence/words : ");
String word = sc.nextLine();
output(word, 3);
sc.close();
}
/**
* 检查一下句子中,出现最多次數的n個英文字
* @param src
* 源字符串
* @param count
* 出现最多次數英文字母个数
*/
static void output(String src, int count) {
if (src != null && count > 0) {
// 循环遍历,按照字符出现顺序依次添加
char[] arr = src.toCharArray();
Map<Character, Integer> map = new LinkedHashMap<Character, Integer>();
for (char c : arr) {
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
if (map.containsKey(c)) {
map.put(c, map.get(c) + 1);
} else {
map.put(c, 1);
}
}
}
// 根据map的value从大到小排序
List<Map.Entry<Character, Integer>> list = new LinkedList<Map.Entry<Character, Integer>>(
map.entrySet());
Collections.sort(list,
new Comparator<Map.Entry<Character, Integer>>() {
@Override
public int compare(Entry<Character, Integer> o1,
Entry<Character, Integer> o2) {
return o2.getValue() - o1.getValue();
}
});
// 输出
// 如果字符串长度小于出现最多次數英文字母个数,则全部输出
int index = 0;
for (Map.Entry<Character, Integer> c : list) {
if (index < count) {
System.out.print("" + c.getValue() + c.getKey() + " ");
++index;
} else {
break;
}
}
}
}
}
补充:Java , Java SE