函数只是将输入的顺序反转,而不是按降序进行排序。

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

Function is just reversing the order of the input instead of sorting it in descending order

问题

需要创建一个程序,该程序将从用户接收字符串输入并将其存储在数组中。然后,需要创建一个函数,该函数首先:按降序对每个字符串{按字符}进行排序,其次:按降序对所有字符串输入进行排序{字符串}。

package com.company;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    static String sortString(String str)
    {
        
        char[] chArr = str.toCharArray();
        String SortString = "";

        // For sorting each individual strings character by character
        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;
                }
            }
        }

        //converting all of the character into a single string
        for (int k = 0; k < chArr.length; k++)
        {
            SortString = SortString + chArr[k];
        }

        //Assigning the current String Sortstring to an array
        String[] OldArray = new String[5];
        for (int counter = 0; counter < 5; counter++)
        {
            OldArray[counter] = SortString;
        }

        //sorting all of the strings in descending order
        for (int i = 0; i < OldArray.length; i++)
        {
            for (int j = i+1; j < OldArray.length; j++)
            {
                if(OldArray[i].compareTo(OldArray[j]) > 0)
                {
                    String temp = OldArray[i];
                    OldArray[i] = OldArray[j];
                    OldArray[j] = temp;
                }
            }
        }

        return OldArray[0];
    }

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

        String[] names = new String[5];

         // will take a String user input from a user and store it in an array
        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);
        }

        //will print the assorted array
        for(int i = 4; i >= 0; i--)
        {
            System.out.println((sortString(names[i])));
        }


    }

}

输入:

Input String #1: Stackoverflow
Input String #2: Java
Input String #3: ZZrot
Input String #4: coding
Input String #5: sorting

输出:

tsronig
onigdc
zztro
vaja
wvtsroolfecka

预期输出:

zztro
wvtsroolfecka
vaja
tsronig
onigdc
英文:

I need to make a program that will take string inputs from user and store it in an array. I will then need to make a function that first: sorts each String {character by character} in descending order and second: will sort all String input in descending order {Strings}.

package com.company;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static String sortString(String str)
{
char[] chArr = str.toCharArray();
String SortString = &quot;&quot;;
// For sorting each individual strings character by character
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;
}
}
}
//converting all of the character into a single string
for (int k = 0; k&lt;chArr.length;k++)
{
SortString = SortString + chArr[k];
}
//Assigning the current String Sortstring to an array
String[] OldArray = new String[5];
for (int counter = 0; counter&lt;5; counter++)
{
OldArray
0
+
网站访问量
= SortString; } //sorting all of the strings in descending order for (int i = 0; i&lt; OldArray.length;i++) { for (int j = i+1; j&lt; OldArray.length;j++) { if(OldArray[i].compareTo(OldArray[j]) &gt; 0) { String temp = OldArray[i]; OldArray[i] = OldArray[j]; OldArray[j] = temp; } } } return OldArray[0]; } public static void main(String[] args) { Scanner UserInput = new Scanner (System.in); String[] names = new String[5]; // will take a String user input from a user and store it in an arra 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); } //will print the assorted array for(int i = 4; i &gt;= 0; i--) { System.out.println((sortString(names[i]))); } } }

Input:

Input String #1: Stackoverflow
Input String #2: Java
Input String #3: ZZrot
Input String #4: coding
Input String #5: sorting

Output

tsronig
onigdc
zztro
vjaa
wvtsroolkfeca

Expected Output:

zztro
wvtsroolkfeca
vjaa
tsronig
onigdc

Sorry for the question I honestly don't know what to do

答案1

得分: 1

你离解决方案非常近了。

sortString 中无法对字符串数组进行排序,因为它只能访问你传入的单个字符串。将数组排序代码移到一个独立的方法中,然后你可以在传递整个数组的同时调用它:

static String 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;
            }
        }
    }

    // 将所有字符合并成单个字符串
    for (int k = 0; k < chArr.length; k++) {
        SortString = SortString + chArr[k];
    }

    return SortString;
}

static void sortArray(String[] OldArray) {
    // 对所有字符串进行降序排序
    for (int i = 0; i < OldArray.length; i++) {
        for (int j = i + 1; j < OldArray.length; j++) {
            if (OldArray[i].compareTo(OldArray[j]) > 0) {
                String temp = OldArray[i];
                OldArray[i] = OldArray[j];
                OldArray[j] = temp;
            }
        }
    }
}

主方法也需要做一个小改变:在对数组排序之前,字符串中的字符必须进行排序。这里在读取输入时对字符进行排序,然后通过一次调用 sortArray 对数组进行排序:

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("输入字符串 #" + (counter + 1) + ": ");
            names[counter] = sortString(UserInput.next().toLowerCase());
        } while (names[counter].length() > 25);
    }

    sortArray(names);

    // 打印排序后的数组
    for (int i = 4; i >= 0; i--) {
        System.out.println(names[i]);
    }
}
英文:

You're very close to the solution.

It's impossible to sort the array of strings in sortString because it only has access to the one string you pass in. Move the array sorting code to a separate method, and then you can call it while passing it the entire array:

static String sortString(String str) {
char[] chArr = str.toCharArray();
String SortString = &quot;&quot;;
// For sorting each individual strings character by character
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;
}
}
}
//converting all of the character into a single string
for (int k = 0; k &lt; chArr.length; k++) {
SortString = SortString + chArr[k];
}
return SortString;
}
static void sortArray(String[] OldArray) {
//sorting all of the strings in descending order
for (int i = 0; i&lt; OldArray.length;i++)
{
for (int j = i+1; j&lt; OldArray.length;j++)
{
if(OldArray[i].compareTo(OldArray[j]) &gt; 0)
{
String temp = OldArray[i];
OldArray[i] = OldArray[j];
OldArray[j] = temp;
}
}
}
}

The main method needs a small change too: the characters in the strings have to be sorted before you sort the array. Here, the characters are sorted while reading the input, and then the array is sorted with one call to sortArray:

public static void main(String[] args)
{
Scanner UserInput = new Scanner (System.in);
String[] names = new String[5];
// will take a String user input from a user and store it in an arra
for (int counter = 0; counter&lt;5; counter++)
{
do
{
System.out.print(&quot;Input String #&quot; + (counter+1) + &quot;: &quot;) ;
names
0
+
网站访问量
= sortString(UserInput.next().toLowerCase()); }while(names
0
+
网站访问量
.length() &gt; 25); } sortArray(names); //will print the assorted array for(int i = 4; i &gt;= 0; i--) { System.out.println(names[i]); } }

答案2

得分: 1

只对你的代码进行了一些更改。sortString() 函数正常工作。

只对 main() 方法进行了更改:

得到了预期的输出,尝试这样写:

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("输入字符串 #" + (counter+1) + ": ");
            names[counter] = UserInput.next().toLowerCase();
        } while(names[counter].length() > 25);
    }

    // 打印排序后的数组
    String[] namesReversed = new String[names.length];
    for(int i = 0; i < names.length; i++){
        namesReversed[i] = sortString(names[i]);
    }
    Arrays.sort(namesReversed, String::compareToIgnoreCase);
    for(int i = namesReversed.length - 1; i >= 0; i--)
    {
        System.out.println(namesReversed[i]);
    }
}
英文:

Just made some changes to your code. sortString() was working fine.

Made only changes to main() method:

Got expected output, Try this:

public static void main(String[] args)
{
Scanner UserInput = new Scanner (System.in);
String[] names = new String[5];
// will take a String user input from a user and store it in an arra
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); } //will print the assorted array String[] namesReversed = new String[names.length]; for(int i=0;i&lt;names.length;i++){ namesReversed[i]=sortString(names[i]); } Arrays.sort(namesReversed, String::compareToIgnoreCase); for(int i = namesReversed.length-1; i&gt;=0; i--) { System.out.println(namesReversed[i]); } }

huangapple
  • 本文由 发表于 2020年8月26日 01:30:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63584191.html
匿名

发表评论

匿名网友

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

确定