发布网友 发布时间:2022-04-24 01:26
共1个回答
热心网友 时间:2023-10-18 10:39
package know;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/**
* 学校有很多班级,分为3班,4班,5班,6班,7班。每个班级有10个学生。请在控制台输入班级,然后打印出该班级的学生总人数和学生姓名
* @author huoger
*
*/
public class T3 {
public static void main(String[] args) {
String[] class3=new String[]{"sdfsd","lklk","erw","xcb","ert","oue","hisdf","hgvke","dfsxcv","ter"};
String[] class4=new String[]{"hj","rte","qwe","cv","fdg","jhk","xcv","ytj","zxcve","nmzn"};
String[] class5=new String[]{"zxce","cxbgh","hju","hrt","lldh","dsfg","fgd","zvd","erwa","xcwe"};
String[] class6=new String[]{"hte","afe","efw","zxc","zxcsd","fsef","xhr","hrwa","xch","shg"};
String[] class7=new String[]{"sdzxcfsd","js","aag","hr","er","rhe","shrxf","cd","cc","ddd"};
Map<String, String[]> classes=new HashMap<String, String[]>();
classes.put("3", class3);
classes.put("4", class4);
classes.put("5", class5);
classes.put("6", class6);
classes.put("7", class7);
Scanner sc=new Scanner(System.in);
while(true){
System.out.println("输入班号:");
String classno=sc.next();
String[] c=classes.get(classno);
if(classno.equals("close")){
sc.close();
break;
}
if(c==null)
System.out.println("不存在该班级");
System.out.println("班级总人数:"+c.length);
System.out.println("学生名单:");
for(String name: c){
System.out.println(name);
}
}
}
}
package know;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
/**
* 用Map实现)在List中存放100个100以内的随机数。然后在控制台中输出每个数字出现的次数
*
* @author huoger
*
*/
public class T4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Integer> list = new ArrayList<Integer>();
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
Random r = new Random();
for (int i = 0; i < 100; i++) {
int k = r.nextInt(100);
list.add(k);
Integer num = map.get(k);
if (num == null) {
map.put(k, 1);
} else {
map.put(k, num + 1);
}
}
for (Entry<Integer, Integer> entry : map.entrySet()) {
System.out.println("数字:"+entry.getKey()+","+"出现次数:"+entry.getValue());
}
System.out.println(map.size());
System.out.println(list.size());
}
}
package know;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
/**
* 从控制台输入一个话,然后在控制台输出每个字符出现的次数
* @author huoger
*
*/
public class T5 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(true){
System.out.println("输入一句话:");
String dia=sc.next();
if(dia.equals("close")){
sc.close();
break;
}
Map<String, Integer> map=new HashMap<String, Integer>();
for(int i=0;i<dia.length();i++){
String c=dia.charAt(i)+"";
Integer num=map.get(c);
if(num==null){
map.put(c, 1);
}else{
map.put(c, num+1);
}
}
for(Entry<String, Integer> entry:map.entrySet()){
System.out.println("字符:"+entry.getKey()+",出现次数"+entry.getValue());
}
}
}
}
热心网友 时间:2023-10-18 10:39
package know;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/**
* 学校有很多班级,分为3班,4班,5班,6班,7班。每个班级有10个学生。请在控制台输入班级,然后打印出该班级的学生总人数和学生姓名
* @author huoger
*
*/
public class T3 {
public static void main(String[] args) {
String[] class3=new String[]{"sdfsd","lklk","erw","xcb","ert","oue","hisdf","hgvke","dfsxcv","ter"};
String[] class4=new String[]{"hj","rte","qwe","cv","fdg","jhk","xcv","ytj","zxcve","nmzn"};
String[] class5=new String[]{"zxce","cxbgh","hju","hrt","lldh","dsfg","fgd","zvd","erwa","xcwe"};
String[] class6=new String[]{"hte","afe","efw","zxc","zxcsd","fsef","xhr","hrwa","xch","shg"};
String[] class7=new String[]{"sdzxcfsd","js","aag","hr","er","rhe","shrxf","cd","cc","ddd"};
Map<String, String[]> classes=new HashMap<String, String[]>();
classes.put("3", class3);
classes.put("4", class4);
classes.put("5", class5);
classes.put("6", class6);
classes.put("7", class7);
Scanner sc=new Scanner(System.in);
while(true){
System.out.println("输入班号:");
String classno=sc.next();
String[] c=classes.get(classno);
if(classno.equals("close")){
sc.close();
break;
}
if(c==null)
System.out.println("不存在该班级");
System.out.println("班级总人数:"+c.length);
System.out.println("学生名单:");
for(String name: c){
System.out.println(name);
}
}
}
}
package know;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
/**
* 用Map实现)在List中存放100个100以内的随机数。然后在控制台中输出每个数字出现的次数
*
* @author huoger
*
*/
public class T4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Integer> list = new ArrayList<Integer>();
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
Random r = new Random();
for (int i = 0; i < 100; i++) {
int k = r.nextInt(100);
list.add(k);
Integer num = map.get(k);
if (num == null) {
map.put(k, 1);
} else {
map.put(k, num + 1);
}
}
for (Entry<Integer, Integer> entry : map.entrySet()) {
System.out.println("数字:"+entry.getKey()+","+"出现次数:"+entry.getValue());
}
System.out.println(map.size());
System.out.println(list.size());
}
}
package know;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
/**
* 从控制台输入一个话,然后在控制台输出每个字符出现的次数
* @author huoger
*
*/
public class T5 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(true){
System.out.println("输入一句话:");
String dia=sc.next();
if(dia.equals("close")){
sc.close();
break;
}
Map<String, Integer> map=new HashMap<String, Integer>();
for(int i=0;i<dia.length();i++){
String c=dia.charAt(i)+"";
Integer num=map.get(c);
if(num==null){
map.put(c, 1);
}else{
map.put(c, num+1);
}
}
for(Entry<String, Integer> entry:map.entrySet()){
System.out.println("字符:"+entry.getKey()+",出现次数"+entry.getValue());
}
}
}
}