Java 编写一个程序来打印每个字母表,其中包含用户输入中出现的字母数
我必须编写一个程序来接受字符串作为输入,作为输出,我必须打印每个字母,以及每个字母在用户输入中出现的次数。有一些限制:Java 编写一个程序来打印每个字母表,其中包含用户输入中出现的字母数,java,Java,我必须编写一个程序来接受字符串作为输入,作为输出,我必须打印每个字母,以及每个字母在用户输入中出现的次数。有一些限制: 我不能使用内置函数和集合 打印结果应按出现值排序 例如,使用此输入: abbbcccdddzz 我希望这一产出: a-1、z-2、b-3、c-4、d-5 这就是我到目前为止所做的: public static void isCountChar(String s) { char c1[] = s.toCharArray(); int c3[] = new in
- 我不能使用内置函数和集合
- 打印结果应按出现值排序
public static void isCountChar(String s) {
char c1[] = s.toCharArray();
int c3[] = new int[26];
for (int i = 0; i < c1.length; i++) {
char c = s.charAt(i);
c3[c - 'a']++;
}
for (int j = 0; j < c3.length; j++) {
if (c3[j] != 0) {
char c = (char) (j + 'a');
System.out.println("character is:" + c + " " + "count is: " + c3[j]);
}
}
}
publicstaticvoidiscountchar(字符串s){
char c1[]=s.toCharArray();
int c3[]=新int[26];
对于(int i=0;i
但是我不知道如何排序。某种排序:如果编码不容易理解,那么很容易
- 从第一个元素循环到最后-1:K元素
- 比较元素K和元素K+1:如果元素K>元素K+1,则将其反转
- 继续循环
- 如果你做了一个改变,重做
- 从第一个元素循环到最后-1:K元素
- 比较元素K和元素K+1:如果元素K>元素K+1,则将其反转
- 继续循环
- 如果你做了一个改变,重做
- 首先,你不被允许使用图书馆或馆藏。如果这不是一个约束条件,我会建议使用
,其中字符作为键,int作为值,这样排序就很容易了HashMap
- 第二个约束是按值排序。这里的大多数人都建议使用类似于
的排序方法,我同意这一点,但它对当前代码不起作用,因为它将按BubbleSort
排序,而不是按字母字符
排序输出值
键
-数组和值
-数组来伪造键值
配对,并同时对它们进行排序(使用类似于泡泡排序
-算法的方法)。代码如下:
private static final int ALPHABET_SIZE = 26;
public static void isCountChar(String s)
{
// Convert input String to char-array (and uppercase to lowercase)
char[] array = s.toLowerCase().toCharArray();
// Fill the keys-array with the alphabet
char[] keys = new char[ALPHABET_SIZE];
for (int i = 0; i < ALPHABET_SIZE; i++)
{
keys[i] = (char)('a' + i);
}
// Count how much each char occurs in the input String
int[] values = new int[ALPHABET_SIZE];
for (char c : array)
{
values[c - 'a']++;
}
// Sort both the keys and values so the indexes stay the same
bubbleSort(keys, values);
// Print the output:
for (int j = 0; j < ALPHABET_SIZE; j++)
{
if (values[j] != 0)
{
System.out.println("character is: " + keys[j] + "; count is: " + values[j]);
}
}
}
private static void bubbleSort(char[] keys, int[] values)
{
// BUBBLESORT (copied from http://www.java-examples.com/java-bubble-sort-example and modified)
int n = values.length;
for(int i = 0; i < n; i++){
for(int j = 1; j < (n - i); j++){
if(values[j-1] > values[j]){
// Swap the elements:
int tempValue = values[j - 1];
values[j - 1] = values[j];
values[j] = tempValue;
char tempKey = keys[j - 1];
keys[j - 1] = keys[j];
keys[j] = tempKey;
}
}
}
}
输出:
character is: e; count is: 1
character is: g; count is: 1
character is: i; count is: 1
character is: n; count is: 1
character is: r; count is: 1
character is: s; count is: 2
character is: t; count is: 3
首先,给你的下一个问题一个提示:你在评论中所陈述的内容更适合作为你问题的编辑。尽量清楚地说明当前的结果是什么,以及预期的结果应该是什么 尽管如此,这是一个有趣的问题,因为有两个限制
- 首先,你不被允许使用图书馆或馆藏。如果这不是一个约束条件,我会建议使用
,其中字符作为键,int作为值,这样排序就很容易了HashMap
- 第二个约束是按值排序。这里的大多数人都建议使用类似于
的排序方法,我同意这一点,但它对当前代码不起作用,因为它将按BubbleSort
排序,而不是按字母字符
排序输出值
键
-数组和值
-数组来伪造键值
配对,并同时对它们进行排序(使用类似于泡泡排序
-算法的方法)。代码如下:
private static final int ALPHABET_SIZE = 26;
public static void isCountChar(String s)
{
// Convert input String to char-array (and uppercase to lowercase)
char[] array = s.toLowerCase().toCharArray();
// Fill the keys-array with the alphabet
char[] keys = new char[ALPHABET_SIZE];
for (int i = 0; i < ALPHABET_SIZE; i++)
{
keys[i] = (char)('a' + i);
}
// Count how much each char occurs in the input String
int[] values = new int[ALPHABET_SIZE];
for (char c : array)
{
values[c - 'a']++;
}
// Sort both the keys and values so the indexes stay the same
bubbleSort(keys, values);
// Print the output:
for (int j = 0; j < ALPHABET_SIZE; j++)
{
if (values[j] != 0)
{
System.out.println("character is: " + keys[j] + "; count is: " + values[j]);
}
}
}
private static void bubbleSort(char[] keys, int[] values)
{
// BUBBLESORT (copied from http://www.java-examples.com/java-bubble-sort-example and modified)
int n = values.length;
for(int i = 0; i < n; i++){
for(int j = 1; j < (n - i); j++){
if(values[j-1] > values[j]){
// Swap the elements:
int tempValue = values[j - 1];
values[j - 1] = values[j];
values[j] = tempValue;
char tempKey = keys[j - 1];
keys[j - 1] = keys[j];
keys[j] = tempKey;
}
}
}
}
输出:
character is: e; count is: 1
character is: g; count is: 1
character is: i; count is: 1
character is: n; count is: 1
character is: r; count is: 1
character is: s; count is: 2
character is: t; count is: 3
那么您必须实施排序算法吗?我建议您查找它们,最容易实现的(虽然不是最充分的)可能是冒泡排序。您想排序什么?是否要按降序显示字母数?你想达到什么目的还不清楚,请在你的问题中说清楚。你能举一个输入和输出的例子吗!输出为:m-1、r-1、t-1、h-1、i-1、o-2关于良好实践的建议:用有意义的名称命名变量。c1、c3、c和s不是很好的变量,所以必须实现排序算法?我建议您查找它们,最容易实现的(虽然不是最充分的)可能是冒泡排序。您想排序什么?是否要按降序显示字母数?你想达到什么目的还不清楚,请在你的问题中说清楚。你能举一个输入和输出的例子吗!输出为:m-1、r-1、t-1、h-1、i-1、o-2关于良好实践的建议:用有意义的名称命名变量。c1、c3、c和s不是好的变量names@Moorthi嗯,你的问题中没有提到任何大写字母。。。无论如何,如果替换
char[]array=s.tocharray()代码>带有char[]数组=s.toLowerCase().tocharray()
它将首先使输入小写,然后计算每个字母(因此a
和a
都被计算为a
)@Moorthi好吧,你的问题中没有说明任何关于大写字母的内容。。。无论如何,如果替换char[]array=s.tocharray()代码>带有char[]数组=s.toLowerCase().tocharray()
它将首先使输入小写,然后计数每个字母(因此a
和a
都被计为a
)