让用户输入数字,输入0程序终止,显示出现频率最大的一个或多个数字,这是我编的程序,数次数那边怎么也写不对~高手帮帮忙啦~
package chapter22;/**
*
* @author zzhao
*/
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
public class ex22_7prac { public static void main(String[] args) { HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); int s = 0; do{
Scanner input = new Scanner(System.in); //ask for user's input
System.out.print("Enter the numbers: "); //obtain user's input
s = input.nextInt(); for(int i = 0; i < map.size(); i++){
map.put(s, i);
int key = i;
if (map.containsKey(key)) {
int occurrence = map.get(key);
occurrence++;
map.put(key, occurrence);
} else {
map.put(key, 1);
}
} } while (s != 0); Iterator iterator = map.keySet().iterator(); while (iterator.hasNext()) {
int key = (Integer) iterator.next();
int occurrence = map.get(key);
if(occurrence >= 2)
System.out.println(key + " occur " + occurrence + " time(s).");
} }}
package chapter22;/**
*
* @author zzhao
*/
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
public class ex22_7prac { public static void main(String[] args) { HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); int s = 0; do{
Scanner input = new Scanner(System.in); //ask for user's input
System.out.print("Enter the numbers: "); //obtain user's input
s = input.nextInt(); for(int i = 0; i < map.size(); i++){
map.put(s, i);
int key = i;
if (map.containsKey(key)) {
int occurrence = map.get(key);
occurrence++;
map.put(key, occurrence);
} else {
map.put(key, 1);
}
} } while (s != 0); Iterator iterator = map.keySet().iterator(); while (iterator.hasNext()) {
int key = (Integer) iterator.next();
int occurrence = map.get(key);
if(occurrence >= 2)
System.out.println(key + " occur " + occurrence + " time(s).");
} }}
解决方案 »
- TimerTask Timer怎么样子重新schedule ?
- java可以做出类似sniffer的网络抓包工具么
- 请教一个thread问题。。。很奇怪。
- thinking in java 写的好还是 java核心技术卷ii 写的好?我先看那本?
- java新手急问!关于JTree
- replaceFirst("\","\\");的问题
- 关于类中数据访问的问题
- 请问为什么会出这样的错误?
- 有关语法方面的问题,很简单的!共同探讨一下
- 有劳高手帮我看看我的这个发送邮件程序???希望高手抽点时间给看看!!!
- 关于ClassLoader获取资源的疑惑
- 很急,CBP=Common Business Processes这门课讲的是什么呢?
import java.util.Iterator;
import java.util.Scanner;public class kane {
public static void main(String[] args) {
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
int s = 0;
do {
Scanner input = new Scanner(System.in);
// ask for user's input
System.out.print("Enter the numbers: ");
// obtain user's input
s = input.nextInt();
System.out.println(map.size());
if (map.containsKey(s)) {
int occurrence = map.get(s);
occurrence++;
map.put(s, occurrence);
} else {
map.put(s, 1);
} } while (s != 0);
Iterator iterator = map.keySet().iterator();
while (iterator.hasNext()) {
int key = (Integer) iterator.next();
int occurrence = map.get(key);
if (occurrence >= 2)
System.out.println(key + " occur " + occurrence + " time(s).");
}
}
}
[code]
public class MajorityObj { private Object[] array; public MajorityObj(Object[] array) {
this.array = array;
} public Object majority() {
Object c = candidate(0);
int count = 0;
for (int i = 0; i < array.length; i++) {
if (array[i].equals(c))
count += 1;
}
if (count > (array.length / 2))
return c;
else
return null;
} public Object candidate(int m) {
int j = m;
Object c = array[m];
int count = 1; while (j < array.length - 1 && count > 0) {
j = j + 1;
if (c.equals(array[j]))
count += 1;
else
count -= 1;
} if (j == array.length - 1)
return c;
else
return candidate(j + 1);
}}[/code]
this.array = array;
} public Object majority() {
Object c = candidate(0);
int count = 0;
for (int i = 0; i < array.length; i++) {
if (array[i].equals(c))
count += 1;
}
if (count > (array.length / 2))
return c;
else
return null;
} public Object candidate(int m) {
int j = m;
Object c = array[m];
int count = 1; while (j < array.length - 1 && count > 0) {
j = j + 1;
if (c.equals(array[j]))
count += 1;
else
count -= 1;
} if (j == array.length - 1)
return c;
else
return candidate(j + 1);
}}
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;public class Temp { public static void main(String[] args) {
HashMap hm = new HashMap();
ArrayList al = new ArrayList();
al.add("a");
al.add("a");
al.add("a");
al.add("b");
al.add("c");
al.add("c");
al.add("d");
al.add("d");
al.add("c");
for (int i = 0; i < al.size(); i++) {
if (hm.get(al.get(i)) == null) {
hm.put(al.get(i), new Integer(1));
} else {
hm.put(al.get(i), Integer
.parseInt(hm.get(al.get(i)).toString()) + 1);
}
}
Object o[] = (Object[]) hm.values().toArray();
Arrays.sort(o);
Integer maxValue = (Integer) o[(o.length - 1)];
Set set = hm.entrySet();
Iterator itr = set.iterator();
while (itr.hasNext()) {
Map.Entry me = (Entry) itr.next();
if (me.getValue().equals(maxValue)) {
System.out.println(me.getKey() + " 出现次数最多,共" + me.getValue()
+ "次");
} else {
System.out.println(me.getKey() + " 出现 " + me.getValue() + "次");
}
}
}
}