Java编程——输入一段字符串,然后按字母顺序输出。并查询某个字母在这段字符串中出现的次数
3个回答
展开全部
import java.util.Arrays;
import java.util.Scanner;
class Method{
public void _Sort(String str){
char[] ch=new char[str.length()];
for(int i=0;i<str.length();i++)
ch[i]=(char)str.charAt(i);
Arrays.sort(ch);
for(int j=0;j<str.length();j++)
System.out.print(ch[j]);
}
public int _Search(String str,char c){
int count=0;
char[] ch=new char[str.length()];
for(int i=0;i<str.length();i++)
ch[i]=(char)str.charAt(i);
for(int i=0;i<str.length();i++){
if(ch[i]==c)
count++;
}
return count;
}
}
public class Test_01{
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.print("请给定字符串:");
String s=input.nextLine();
System.out.print("请给定需要检查的字母:");
String ss=input.next();
char c=ss.charAt(0);
Method mh=new Method();
System.out.println("排序前:"+s);
System.out.print("排序后:");
mh._Sort(s);
System.out.println();
System.out.println(c+"字母共出现了"+mh._Search(s,c)+"次");
}
}
import java.util.Scanner;
class Method{
public void _Sort(String str){
char[] ch=new char[str.length()];
for(int i=0;i<str.length();i++)
ch[i]=(char)str.charAt(i);
Arrays.sort(ch);
for(int j=0;j<str.length();j++)
System.out.print(ch[j]);
}
public int _Search(String str,char c){
int count=0;
char[] ch=new char[str.length()];
for(int i=0;i<str.length();i++)
ch[i]=(char)str.charAt(i);
for(int i=0;i<str.length();i++){
if(ch[i]==c)
count++;
}
return count;
}
}
public class Test_01{
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.print("请给定字符串:");
String s=input.nextLine();
System.out.print("请给定需要检查的字母:");
String ss=input.next();
char c=ss.charAt(0);
Method mh=new Method();
System.out.println("排序前:"+s);
System.out.print("排序后:");
mh._Sort(s);
System.out.println();
System.out.println(c+"字母共出现了"+mh._Search(s,c)+"次");
}
}
展开全部
import java.util.Scanner;
import java.util.Arrays;
class MyTest
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
String str = in.next();
char[] c = str.toCharArray();
Arrays.sort(c);
str = String.valueOf(c);
System.out.println(str);
System.out.println("输入你要查询的字符:");
String s = in.next();
int i = 0;
int index = 0;
while((index = str.indexOf(s, index + 1)) != -1)
{
i++;
}
System.out.println("字符" + s + "出现" + i + "次。");
}
}
import java.util.Arrays;
class MyTest
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
String str = in.next();
char[] c = str.toCharArray();
Arrays.sort(c);
str = String.valueOf(c);
System.out.println(str);
System.out.println("输入你要查询的字符:");
String s = in.next();
int i = 0;
int index = 0;
while((index = str.indexOf(s, index + 1)) != -1)
{
i++;
}
System.out.println("字符" + s + "出现" + i + "次。");
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
排序
String str = "AdaaaqweafgGddddynMmmCdddXZ";
byte[] bytes = str.getBytes();
Arrays.sort(bytes);
for(byte b:bytes){
System.out.print((char)b);
}
统计
Map<Character,Integer> map = new HashMap<Character,Integer>();
for(int i=0, len=str.length(); i<len; i++){
char ch = str.charAt(i);
Integer n = map.get(ch);
map.put(ch, n==null ? 1 : n+1);
}
System.out.println(map);
String str = "AdaaaqweafgGddddynMmmCdddXZ";
byte[] bytes = str.getBytes();
Arrays.sort(bytes);
for(byte b:bytes){
System.out.print((char)b);
}
统计
Map<Character,Integer> map = new HashMap<Character,Integer>();
for(int i=0, len=str.length(); i<len; i++){
char ch = str.charAt(i);
Integer n = map.get(ch);
map.put(ch, n==null ? 1 : n+1);
}
System.out.println(map);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询