题目一
准备一个 HashMap 集合,统计字符串"123,456,789,123,456"中每个数字字符串出现的次数并打印出来。
如:
123 出现了 2 次
456 出现了 2 次
789 出现了 1 次
思路分析
- 用String类的split方法进行字符串分割,将字符串分割字符数组
- 将字符串数组的字符串作为hashmap的key存入,每次存入前判断是否存在,如果存在key对应的value+1,如果不存在使value为1
代码示例
package com.lagou.homework;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
/*准备一个 HashMap 集合,统计字符串"123,456,789,123,456"中每个数字字符串出现的次数并打印出来。
如:
123 出现了 2 次
456 出现了 2 次
789 出现了 1 次*/
public class HomeWork3_2 {
public static void main(String[] args) {
HashMap<String,Integer> m1 = new HashMap<>();
String string = new String("123,456,789,123,456");
//拆分字符串为字符串数组
String[] strings = string.split(",");
//打印字符串数组
for (int i = 0;i < strings.length; i++){
System.out.println(strings[i]);
}
for (int i = 0; i<strings.length;i++){
//判断hashmap中是否存在key为String[i]的元素
if (!m1.containsKey(strings[i])){
//不存在则存入key并将value赋值为1
m1.put(strings[i],1);
System.out.println(m1);
}
else {
m1.put(strings[i],m1.get(strings[i])+1);//若存在则修改value为原始value+1
}
}
System.out.println(m1);
//将 hashmap的key转为hashset视图
Set hashSet = m1.keySet();
//遍历hashset
for (Object a:hashSet
) {
System.out.println(a+"出现了"+m1.get(a));
}
}
}
题目二
编程统计字符串"ABCD123!@#$%ab"中大写字母、小写字母、数字、其它字符的个数并打印出来。
思路分析
- 使用字符包装类的静态方法判断或者采用Ascii码值范围判断
代码示例
package com.lagou.homework;
/**
* 编程统计字符串"ABCD123!@#$%ab"中大写字母、小写字母、数字、其它字符的个数并打印出来。
*/
public class HomeWork3_1 {
public static void main(String[] args) {
int countdx= 0;//统计大写字母个数
int countxx= 0;//统计小写字母个数
int countsz= 0;//统计数字个数
int countqt= 0;//统计其他字符个数
String str = "ABCD123!@#$%ab";
char[] arr = str.toCharArray();
for (int i = 0; i < arr.length;i++){
if (Character.isUpperCase(arr[i]))
{
countdx++;
}
else if (Character.isLowerCase(arr[i])){
countxx++;
}
else if (Character.isDigit(arr[i])){
countsz++;
}
else {
countqt++;
}
}
System.out.println("大写字母有:"+(countdx++)+"个");
System.out.println("小写字母有:"+(countxx++)+"个");
System.out.println("数字有:"+(countsz++)+"个");
System.out.println("其他字符有:"+(countqt++)+"个");
}
}
题目三
自定义数组扩容规则,当已存储元素数量达到总容量的 80%时,扩容 1.5 倍。 例如,总容量是 10,当输入第 8 个元素时,数组进行扩容,容量从 10 变 15。
代码示例
public class HomeWork1_5 {
public static void main(String[] args){
System.out.println("请输入一个整数作为数组最开始的容量:");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int[] arr = new int[num];
for (int i = 0 ;i < arr.length; i++ ){
System.out.println("数组容量为:"+arr.length);
//给数组赋值
System.out.println("请输入第"+i+"个元素:");
arr[i] = sc.nextInt();
//判断元素数量是否达到总容量的0.8
if (i >= (int)(arr.length*0.8)-1){
//如果达到 则将arr数组进行扩容
System.out.println("数据达到80%,进行扩容!");
arr = Arrays.copyOf(arr,(int )(arr.length*1.5));
System.out.println("扩容成功,扩容后容量为:"+arr.length);
}
}
}
}
题目四
编程找出 1000 以内的所有完数并打印出来。 所谓完数就是一个数恰好等于它的因子之和,如:6=1+2+3
代码示例
/**
* @author pp_x
* 编程找出 1000 以内的所有完数并打印出来。 所谓完数就是一个数恰好等于它的因子之和,如:6=1+2+3
*/
public class HomeWork1_2 {
public static void main(String[] args){
System.out.println("开始");
for (int i = 1 ; i < 1000; i++){
int sum = 0;//记录因子之和
for (int j = 1; j <= (i/2); j++){
//判断 i能不能整除j 如果能则求j的和
if (i%j == 0){
sum += j;
}
}
//判断sum是否和i相等 如果相等则输入i
if (sum == i){
System.out.println("1000以内完数有:"+i);
}
}
System.out.println("结束");
}
}
题目五
提示用户输入年月日信息,判断这一天是这一年中的第几天并打印。
代码分析
package com.lagou.homework;
import java.util.Scanner;
/**
* @author pp_x
* 提示用户输入年月日信息,判断这一天是这一年中的第几天并打印。
*/
public class HomeWork1_1 {
public static void main(String[] args){
for (;;){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入年份:");
int year = scanner.nextInt();//年份
System.out.println("请输入月份:");
int month = scanner.nextInt();//月份
System.out.println("请输入日:");
int day = scanner.nextInt();//日
int today = 0;//一年中的第几天
switch (month){
case 12 :
today += 30;//11月是30天
case 11:
today += 31;//10月是31天
case 10:
today += 30;//9月是30天
case 9:
today += 31;//8月是31天
case 8:
today += 31;//7月是31天
case 7:
today += 30;//6月是30天
case 6:
today += 31;//5月是31天
case 5:
today += 30;//4月是30天
case 4:
today += 31;//3月是31天
case 3:
//判断该年是否为闰年
if(year%4==0&&year%100!=0||year%400==0){
//闰年2月为29天
today += 29;
}else {
//平年2月是28天
today += 28;
}
case 2:
today += 31;//一月是31天
case 1:
today += day;//如果是一月 today直接等于当前日期
}
System.out.println(year+"年"+month+"月"+day+"日是"+year+"年的第"+today+"天");
}
}
}