当前位置:网站首页>实验七 常用类的使用(修正帖)
实验七 常用类的使用(修正帖)
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. Input and output functions (printf, scanf, getchar and putchar)

仿牛客技术博客项目常见问题及解答(二)

About the parental delegation mechanism and the process of class loading

FAQs and answers to the imitation Niuke technology blog project (III)

4. Binary search

Difference and understanding between detected and non detected anomalies

7.数组、指针和数组的关系

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
![[面試時]——我如何講清楚TCP實現可靠傳輸的機制](/img/d6/109042b77de2f3cfbf866b24e89a45.png)
[面試時]——我如何講清楚TCP實現可靠傳輸的機制

9. Pointer (upper)
随机推荐
A piece of music composed by buzzer (Chengdu)
[the Nine Yang Manual] 2016 Fudan University Applied Statistics real problem + analysis
Inaki Ading
更改VS主题及设置背景图片
编写程序,模拟现实生活中的交通信号灯。
为什么要使用Redis
7-11 机工士姆斯塔迪奥(PTA程序设计)
使用Spacedesk实现局域网内任意设备作为电脑拓展屏
[the Nine Yang Manual] 2017 Fudan University Applied Statistics real problem + analysis
Leetcode. 3. Longest substring without repeated characters - more than 100% solution
[the Nine Yang Manual] 2022 Fudan University Applied Statistics real problem + analysis
Miscellaneous talk on May 27
Floating point comparison, CMP, tabulation ideas
2.C语言矩阵乘法
It's never too late to start. The tramp transformation programmer has an annual salary of more than 700000 yuan
(original) make an electronic clock with LCD1602 display to display the current time on the LCD. The display format is "hour: minute: Second: second". There are four function keys K1 ~ K4, and the fun
Change vs theme and set background picture
C language Getting Started Guide
String ABC = new string ("ABC"), how many objects are created
vector