先按逗号分割输入
然后
word.toCharArray
然后 循环每一个char数组,按字母存map,其中value存个数
OK了
然后
word.toCharArray
然后 循环每一个char数组,按字母存map,其中value存个数
OK了
解决方案 »
- java通过poi读取excel,文档对象显示多出一个sheet表单对象
- 关于方法重载(Overloading)的执行顺序问题??
- 用org.commons.net.ftp包上传功能问题
- 求助:P2P流量检测的java代码
- **********看看这个GZ解压代码的问题************
- 怎样从外部连接处于局域网中的某一台机器(此机器可以通过代理服务器上网,代理服务器有固定IP)?
- 串口通信:javax.com.api的设置——敬请南来北往的各位给查个错。谢谢!!
- ★★★升级为两颗星,散分,呵呵★★★
- 语音对讲技术!!!!
- 带有数据库的APPLET为什么在Ie5里不能正常运行
- J2SE 潜艇大战小游戏(附源码)
- jsp上传大文件失败的问题
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個出現最多次的字母...
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哥出手幫幫我
---------------------
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 : APPLETotal chars 5
---------------------
Frequency Alphabet
--------- ---------==================================================
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);}
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
这个加上面,Map和HashMap需要引入的
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;
}
}
}
}
}