当前位置:网站首页>实验七 常用类的使用(修正帖)
实验七 常用类的使用(修正帖)
2022-07-06 09:22:00 【文文喜欢郭子吖】
十分感谢热心的友友们及时发现并指出了我实验报告中的错误,现对存在问题的两题做出如下修正:
2. 从键盘输入一串字符,输出有多少个不同的字符、每个字符出现的次数。
package code72;
import java.util.Scanner;
import java.util.HashMap;
public class code72 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("请输入一串字符:");
String str=in.nextLine();
StringBuffer b=new StringBuffer(str); //插入字符串缓冲区StringBuffer
int length=b.length(); //字符总数
str=str.toLowerCase(); //将输入的字符串的大写字母全部转换成小写字母
int c=0;
HashMap hm=new HashMap();
for(int i=0;i<str.length();i++) {
char ch=str.charAt(i); //返回指定索引处的char值
if((ch>='a'&&ch<='z')) {
if(!hm.containsKey(ch)) { //判断是否包含指定的键名
hm.put(ch,new Integer(1)); //put方法,可以单次向HashMap中添加一个键值对
c++;
}
else {
Integer d=(Integer)hm.get(ch)+1;
hm.put(ch,d);
}
}
}
System.out.println("其中不同的字符总数为:"+c);
String a; //声明单个字符
int index,sum;
for(int i=0;i<b.length();) {
sum=0;
a=b.substring(0,1); //截取单个字符
index=b.indexOf(a); //搜索单个字符在整个字符串中出现的位置
while(index!=-1) {
sum++;
b.deleteCharAt(index); //删除字符
index=b.indexOf(a,index); //统计次数
}
System.out.println(a+"字母有"+sum+"个!");
}
}
}
4. 输入一个八进制数字串(在整型数据范围内),分别以2进制、10进制、16进制输出。
提示:Integer.parseInt("100",8); //将8进制100转换成十进制数
Integer.toBinaryString(100); //将十进制整数100转换成二进制数
package code74;
import java.util.Scanner;
public class code74 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("输入一个八进制数字串(在整型数据范围内):");
String a=in.nextLine(); //输入原字符串
int b=Integer.parseInt(a); //原字符串转换为数字
System.out.println("2进制为:"+Integer.toBinaryString(b)); //将b转换为2进制数
System.out.println("10进制为:"+Integer.parseInt(a,8)); //将b转换为10进制数
System.out.println("16进制为:"+Integer.toHexString(b)); //将b转换为16进制数
}
}
本人学识尚浅,衷心希望能够有小伙伴指出我的错误并提出宝贵的建议,我都会及时改正和采纳,争取能够呈现更好的内容供大家来参考和学习,同时也希望我们能够共同进步!
边栏推荐
猜你喜欢
3.输入和输出函数(printf、scanf、getchar和putchar)
SRC挖掘思路及方法
1. C language matrix addition and subtraction method
Change vs theme and set background picture
Mortal immortal cultivation pointer-2
Programme de jeu de cartes - confrontation homme - machine
[during the interview] - how can I explain the mechanism of TCP to achieve reliable transmission
Caching mechanism of leveldb
4. Branch statements and loop statements
Safe driving skills on ice and snow roads
随机推荐
The difference between overloading and rewriting
仿牛客技术博客项目常见问题及解答(一)
A comprehensive summary of MySQL transactions and implementation principles, and no longer have to worry about interviews
抽象类和接口的区别
Leetcode.3 无重复字符的最长子串——超过100%的解法
TypeScript快速入门
【黑马早报】上海市监局回应钟薛高烧不化;麦趣尔承认两批次纯牛奶不合格;微信内测一个手机可注册俩号;度小满回应存款变理财产品...
Change vs theme and set background picture
Safe driving skills on ice and snow roads
Redis实现分布式锁原理详解
[the Nine Yang Manual] 2019 Fudan University Applied Statistics real problem + analysis
Mode 1 two-way serial communication is adopted between machine a and machine B, and the specific requirements are as follows: (1) the K1 key of machine a can control the ledi of machine B to turn on a
Differences among fianl, finally, and finalize
View UI plus released version 1.3.0, adding space and $imagepreview components
1.C语言初阶练习题(1)
Difference and understanding between detected and non detected anomalies
Wei Pai: the product is applauded, but why is the sales volume still frustrated
更改VS主题及设置背景图片
杂谈0516
5月27日杂谈