当前位置:网站首页>实验七 常用类的使用(修正帖)
实验七 常用类的使用(修正帖)
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进制数
}
}本人学识尚浅,衷心希望能够有小伙伴指出我的错误并提出宝贵的建议,我都会及时改正和采纳,争取能够呈现更好的内容供大家来参考和学习,同时也希望我们能够共同进步!
边栏推荐
- Miscellaneous talk on May 14
- 8. C language - bit operator and displacement operator
- [the Nine Yang Manual] 2016 Fudan University Applied Statistics real problem + analysis
- C language Getting Started Guide
- 重载和重写的区别
- 2.C语言初阶练习题(2)
- fianl、finally、finalize三者的区别
- 关于双亲委派机制和类加载的过程
- 3.输入和输出函数(printf、scanf、getchar和putchar)
- 【九阳神功】2016复旦大学应用统计真题+解析
猜你喜欢

1. C language matrix addition and subtraction method

魏牌:产品叫好声一片,但为何销量还是受挫

扑克牌游戏程序——人机对抗
![[au cours de l'entrevue] - Comment expliquer le mécanisme de transmission fiable de TCP](/img/d6/109042b77de2f3cfbf866b24e89a45.png)
[au cours de l'entrevue] - Comment expliquer le mécanisme de transmission fiable de TCP

4.二分查找

Mortal immortal cultivation pointer-2

深度强化文献阅读系列(一):Courier routing and assignment for food delivery service using reinforcement learning

C语言入门指南

编写程序,模拟现实生活中的交通信号灯。

Questions and answers of "Fundamentals of RF circuits" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
随机推荐
Miscellaneous talk on May 14
The latest tank battle 2022 full development notes-1
The latest tank battle 2022 - Notes on the whole development -2
关于双亲委派机制和类加载的过程
The latest tank battle 2022 - full development notes-3
编写程序,模拟现实生活中的交通信号灯。
C language to achieve mine sweeping game (full version)
7. Relationship between array, pointer and array
[the Nine Yang Manual] 2021 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
[the Nine Yang Manual] 2020 Fudan University Applied Statistics real problem + analysis
4. Binary search
Using spacedesk to realize any device in the LAN as a computer expansion screen
受检异常和非受检异常的区别和理解
仿牛客技术博客项目常见问题及解答(三)
Mortal immortal cultivation pointer-1
MySQL lock summary (comprehensive and concise + graphic explanation)
自定义RPC项目——常见问题及详解(注册中心)
2022 Teddy cup data mining challenge question C idea and post game summary
5.MSDN的下载和使用