将字符串转换为char并按降序排序(ascii) [英] Converting string to char and sort in descending order (ascii)

查看:284
本文介绍了将字符串转换为char并按降序排序(ascii)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个程序,它将使用户输入整数(一个接一个),存储在数组中,并按降序显示整数。程序还要求用户输入一个字符串将其转换为char使用 string.toCharArray()。我已经正确完成显示整数的降序。



这是整数的代码:

  for(i = 0; i  System.out.print(为索引[+ i + - >); 
nums [i] = Integer.parseInt(br.readLine());}

while(pass for(i = 0; i < (nums.length-pass); i ++){
if(nums [i] temp = nums [i]
nums [i] = nums [i + 1];
nums [i + 1] = temp;
}
}
pass ++;
}
System.out.println(\\\
\\\
按降序排序的数字为:\\\
);
for(i = 0; i System.out.println(nums [i]);

这是字符串到数组的代码。这是我有问题的地方。我没有运行程序的错误只是我不知道如何正确地做。

  System.out .print(Input a string); 
String strValue = br.readLine();
char [] chrValues;
chrValues = strValue.toCharArray();

while(flag< chrValues.length){
for(c = 0; c <(chrValues.length- flag); c ++){
if(chrValues [c] < chrValues [i + 1]){
tempo = chrValues [c];
chrValues [c] = chrValues [c + 1];
chrValues [c + 1] = tempo;}
}
}
标志++;
System.out.println(\\\
\\\
以字符转换并按降序排序的字符串为:\\\
);
for(c = 0; i System.out.println(chrValues [c]);}
解决方案

p $ p>

您可以尝试

  String str =AbCdEf; 
char [] arr = str.toCharArray();

for(int i = 0; i System.out.println();

Arrays.sort(arr); //按升序排序

//向后打印,即按降序排列。
for(int i = arr.length-1; i> = 0; i--)System.out.print(arr [i]);
System.out.println();

print

  AbCdEf 
fdbECA


I am creating a program which will make the user input integer (one after another), stored in array and display the integers in descending order. The program also ask to the user to input a string convert it to char using string.toCharArray(). I've correctly done displaying the integer into descending order. The problem is I don't know how to display the char descendingly.

This is the code for the integer:

for(i=0;i<nums.length;i++){
        System.out.print("Enter value for index["+i+"] here --> ");
        nums[i]=Integer.parseInt(br.readLine());}

while(pass<nums.length){
        for(i=0;i<(nums.length-pass);i++){
            if(nums[i]<nums[i+1]){
                temp=nums[i];
                nums[i]=nums[i+1];
                nums[i+1]=temp;
            }
        }
        pass++;
}
System.out.println("\n\nThe numbers when sorted descendingly are :\n");
    for(i=0;i<nums.length;i++){
        System.out.println(nums[i]);

This is the code for the string to array. This is where i'm having problems. I don't have errors in running the program just it's just I don't how to do it correctly.

System.out.print("Input a string");
        String strValue= br.readLine();
        char[] chrValues;
        chrValues=strValue.toCharArray( );

while(flag<chrValues.length){
   for (c=0; c< (chrValues.length- flag); c++){
        if(chrValues[c]<chrValues[i+1]){
         tempo=chrValues[c];
                chrValues[c]=chrValues[c+1];
                chrValues[c+1]= tempo;}
   }
}
 flag++; 
 System.out.println("\n\nThe String when converted in character and sorted descendingly     are :\n");    
    for(c=0;i<chrValues.length;c++){
        System.out.println(chrValues[c]);}

By the way, I used flag as a temporary array storage.

解决方案

You can try

String str = "AbCdEf";
char[] arr = str.toCharArray();

for(int i = 0; i < arr.length; i++) System.out.print(arr[i]);
System.out.println();

Arrays.sort(arr); // sorted in ascending order

// print them backwards i.e. descending order.
for(int i = arr.length - 1; i >= 0; i--) System.out.print(arr[i]);
System.out.println();

prints

AbCdEf
fdbECA

这篇关于将字符串转换为char并按降序排序(ascii)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆