在Java中将数组按降序进行排序。

huangapple go评论73阅读模式
英文:

Sorting an array in a decreasing order in Java

问题

package com.company;

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    static void sortString(String str) {
        char[] chArr = str.toCharArray();
        String SortString = "";
        for (int i = 0; i < chArr.length; i++) {
            for (int j = 0; j < chArr.length; j++) {
                if (chArr[i] > chArr[j]) {
                    char temp = chArr[i];
                    chArr[i] = chArr[j];
                    chArr[j] = temp;
                }
            }
        }
        String[] SortedString = new String[5];

        for (int k = 0; k < chArr.length; k++) {
            SortString = SortString + chArr[k];
        }
        Arrays.sort(SortedString);
        for (int counter = 0; counter < 5; counter++) {
            System.out.println(SortedString[counter]);
        }
    }
    
    public static void main(String[] args) {
        Scanner UserInput = new Scanner(System.in);

        String[] names = new String[5];

        for (int counter = 0; counter < 5; counter++) {
            do {
                System.out.print("Input String #" + (counter + 1) + ": ");
                names[counter] = UserInput.next().toLowerCase();
            } while (names[counter].length() > 25);

        }
        UserInput.close();
        Arrays.sort(names);
        for (int counter = 0; counter < 5; counter++) {
            sortString(names[counter]);
        }
    }
}
英文:

I'm trying to code a program that will sort the given strings one by one first and then sorting all the strings both in decreasing order I managed to sort the string character by character but I am having trouble in sorting all the sorted (character by character) strings. I tried using the Array.sort() but it does not sort it decreasingly and it only sorts the first input not the already sorted array

    package com.company;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static void sortString(String str)
{
char[] chArr = str.toCharArray();
String SortString = &quot;&quot;;
for (int i = 0; i&lt; chArr.length; i++)
{
for (int j = 0; j&lt; chArr.length; j++)
{
if(chArr[i] &gt; chArr[j])
{
char temp = chArr[i];
chArr[i] = chArr[j];
chArr[j] = temp;
}
}
}
String[] SortedString = new String[5];
for (int k = 0; k&lt;chArr.length;k++)
{
SortString = SortString + chArr[k];
}
Arrays.sort(SortedString);
for (int counter = 0; counter&lt;5; counter++)
{
System.out.println(SortedString
0
+
网站访问量
); } } public static void main(String[] args) { Scanner UserInput = new Scanner (System.in); String[] names = new String[5]; for (int counter = 0; counter&lt;5; counter++) { do { System.out.print(&quot;Input String #&quot; + (counter+1) + &quot;: &quot;) ; names
0
+
网站访问量
= UserInput.next().toLowerCase(); }while(names
0
+
网站访问量
.length() &gt; 25); } UserInput.close(); Arrays.sort(names); for (int counter = 0; counter&lt;5; counter++) { sortString(names
0
+
网站访问量
); } } }

答案1

得分: 1

static String sortString(String str)
{
    char[] chArr = str.toCharArray();
    for (int i = 0; i < chArr.length; i++)
    {
        for (int j = 0; j < chArr.length; j++)
        {
            if(chArr[i] > chArr[j])
            {
                char temp = chArr[i];
                chArr[i] = chArr[j];
                chArr[j] = temp;
            }
        }
    }
    
    return new String(chArr);
}

public static void main(String[] args)
{
    Scanner UserInput = new Scanner (System.in);

    String[] names = new String[5];

    for (int counter = 0; counter < 5; counter++)
    {
        do
        {
            System.out.print("Input String #" + (counter+1) + ": ");
            names[counter] = UserInput.next().toLowerCase();
        } while(names[counter].length() > 25);

    }
    UserInput.close();
    // Arrays.sort(names);   No point sorting here
    
    String[] strings = new String[5];
    for (int counter = 0; counter < 5; counter++)
    {
        strings[counter] = sortString(names[counter]);
    }

     Arrays.sort(strings);

      // increasing order:
     for(String s : strings) {
         System.out.println(s);
     }
     
     // decreasing order:        
     for(int i = 4; i >= 0; i--) {
         System.out.println(strings[i]);
     }
}
英文:
static String sortString(String str)
{
char[] chArr = str.toCharArray();
for (int i = 0; i&lt; chArr.length; i++)
{
for (int j = 0; j&lt; chArr.length; j++)
{
if(chArr[i] &gt; chArr[j])
{
char temp = chArr[i];
chArr[i] = chArr[j];
chArr[j] = temp;
}
}
}
return new String(chArr);
}
public static void main(String[] args)
{
Scanner UserInput = new Scanner (System.in);
String[] names = new String[5];
for (int counter = 0; counter&lt;5; counter++)
{
do
{
System.out.print(&quot;Input String #&quot; + (counter+1) + &quot;: &quot;) ;
names
0
+
网站访问量
= UserInput.next().toLowerCase(); }while(names
0
+
网站访问量
.length() &gt; 25); } UserInput.close(); // Arrays.sort(names); No point sorting here String[] strings = new String[5]; for (int counter = 0; counter&lt;5; counter++) { strings
0
+
网站访问量
= sortString(names
0
+
网站访问量
); } Arrays.sort(strings); // increasing order: for(String s : strings) { System.out.println(s); } // decreasing order: for(int i = 4; i &gt;= 0; i--) { System.out.println(strings[i]); } }

huangapple
  • 本文由 发表于 2020年8月23日 16:45:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63545045.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定