当前位置:网站首页>從鍵盤輸入一串字符,輸出不同的字符以及每個字符出現的次數。(輸出不按照順序)運用String類的常用方法解題
從鍵盤輸入一串字符,輸出不同的字符以及每個字符出現的次數。(輸出不按照順序)運用String類的常用方法解題
2022-07-23 06:03: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+"次");
}
}
}
輸出無次序。
边栏推荐
猜你喜欢

嵌入式系统移植【3】——uboot的烧写及使用

Difference between get request and post request

get请求和post请求的区别

利用“HiFolw”快捷制作高校学生返校名单信息生成

资产测绘流程

UNIX Programming - network socket

第四次作业:关于cat,grep,cut,sort,uniq,vim,tr等命令的用法

The difference between get request and post request and packet capturing

浅谈LoRa,LoRaWAN,NB-IoT三类物联网技术

Day 2 summary and test case operation
随机推荐
迷茫的五月
Unix programming project - the client based on raspberry pie regularly obtains the temperature and reports it to the server
在局域网内配置LoRaWAN的私有ChirpStack
信息收集的基础要素
嵌入式系统移植【2】——交叉开发环境的搭建
01. Introduction to large Internet Architecture
防火墙粗略了解
DNS域名解析服务
最简单的scull设备驱动
读刘润《底层逻辑》摘录
[untitled]
判断Set中课程是否存在,Contains方法
The difference between get request and post request and packet capturing
编程入门3——求最大值
第四次作业:关于cat,grep,cut,sort,uniq,vim,tr等命令的用法
CSDN陪伴了我四年本科生活,自己也开始好好记录一下啦
Apply collections Sort() implements list sorting
Basic elements of information collection
Disk management operations
输入两个字符串 str1、str2,统计字符串 str2 出现在 str1 中的次数。