当前位置:网站首页>从键盘输入一串字符,输出不同的字符以及每个字符出现的次数。(输出不按照顺序)运用String类的常用方法解题
从键盘输入一串字符,输出不同的字符以及每个字符出现的次数。(输出不按照顺序)运用String类的常用方法解题
2022-07-22 18:11:00 【百分之七.】
package labreport7;
import java.util.Scanner;
//从键盘输入一串字符,输出不同的字符以及每个字符出现的次数。
public class test2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
String s1=input.nextLine();
int count=0;//用于记录每个字符出现的次数
for(int i=0;i<s1.length();i++) {
count=0;//每统计完一个字符后清零
String s2 = s1.substring(i,i+1);//依次截取字符串中的每个字符
int t=s1.indexOf(s2);//返回指定字符在此字符串中出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
while(t!=-1) {//统计该字符出现的次数,找不到了则返回-1
count++;//每找到一次则加一
t=s1.indexOf(s2,t+1);//从t+1处往后找,并且找到后返回下标值,找不到返回-1
}
if(s1.indexOf(s2,i+1)!=-1) {//若出现重复的字符,则直接跳过,不进行重复输出
continue;
}
System.out.println(s2+ "字符出现"+count+"次");
}
}
}
输出无次序。
边栏推荐
猜你喜欢
随机推荐
Common problems of multiple processes - how to lock the same parent thread variable (critical resource) when creating multiple threads so that the shared parent thread variable is not repeatedly modif
Contains方法,查看序列中是否包含某个元素
Source code compilation and installation lamp
Disk management operations
RAID磁盘阵列
zstuAcm学生信息库的建立(用链表完成)
嵌入式系统移植【2】——交叉开发环境的搭建
RAID disk array
Operation of numerical variables and special variables
Unittest framework learning (I)
Collections的Comparable,Comparator
资产测绘流程
源码编译安装LAMP
jmeter常见问题
读《机器学习-周志华》内容摘要
防火墙粗略了解
重置root密码
01. Introduction to large Internet Architecture
The difference between get request and post request
IA笔记1









