这段Java代码不使用数组按字母顺序对字符串进行排序,

vxqlmq5t  于 2022-12-10  发布在  Java
关注(0)|答案(2)|浏览(133)

我试着写这个Java代码,不使用数组按字母顺序排序字符串,但是它在输入字符串的第一个单词上失败了。我试着修复它,但是我无法找到解决方案。我正在学习编码,我需要一个提示。
下面的Java代码实现了一个函数,它可以不使用数组而按字母顺序对输入字符串进行排序。该函数名为sortWords,它有三个参数:phrase,它是要排序的字符串; splitBy,它是将用于将字符串拆分为单个单词的字符或字符串;和order,它是一个确定输出顺序应该是升序('〈')还是降序('〉')的字符。
sortWords使用几个帮助器函数来完成它的任务。countWords函数使用给定的字符或字符串作为分隔符来计算给定字符串中的单词数。getWord函数使用给定的字符或字符串作为分隔符来从给定的字符串中检索特定的单词。replaceWord函数将给定字符串中的特定单词替换为另一个给定的单词,使用给定的字符或字符串作为分隔符。
sortWords的主要逻辑如下:
1.使用countWords计算给定字符串中的字数。
1.循环访问字符串中的所有单词,从第一个单词到最后一个单词。
1.对于每个单词,搜索字符串中位于其前面且尚未处理的最小单词。
1.如果找到较小的单词,请使用replaceWord将其替换为原始单词,并保存原始单词以备将来使用。
1.重复此过程,直到处理完字符串中的所有单词。
1.如果指定了降序('〉'),则使用辅助字符串和getWord反转字符串中单词的顺序。
1.返回排序后的字符串。
预期结果:因为在字母表中f在g之前
观察结果:“arbol,bedel,casa,diploma,excursion,guea,factura,xilofono '.我认为它失败是因为factura是输入字符串中的第一个单词.

static Scanner sc = new Scanner(System.in);
    public static void main(String[] args){
        /* sort alphabetically this string without using arrays*/
        String phrase;
        phrase = "factura,casa,arbol,xilofono,bedel,diploma,excursion,guea";
        System.out.println(sortWords(phrase, ",", '<'));
    }

    private static String getWord(String phrase, String splitBy, int number){
        String word;
        Scanner splitter;
        int count;
        splitter = new Scanner(phrase);
        splitter.useDelimiter(splitBy);
        word = "";
        count = 1;
        while (count <= number){
            word = splitter.next();
            count++;
        }
        return word;
    }

    private static int countWords(String phrase, String splitBy){
        int count;
        int position;
        count = 0;
        position = 0;
        while(position != -1){
            if (count == 0){
                position = phrase.indexOf(splitBy);
            } else {
                position = phrase.indexOf(splitBy, position + 1);
            }
            if (position != -1){
                count++;
            }
        }
        return count + 1;
    }

    private static String sortWords(String phrase, String splitBy, char order){
        int numberOfWords;
        numberOfWords = countWords(phrase, splitBy);
        for(int count = 1 ; count <= numberOfWords; count++){
            String wordToCheck;
            String wordToSwitchWith;
            int switchWithNumber;
            wordToCheck = getWord(phrase, splitBy, count);
            wordToSwitchWith = wordToCheck;
            switchWithNumber = count;
            for(int checkVS = count -1 ; checkVS >= 1; checkVS--){
                String wordToCompareWith;
                wordToCompareWith = getWord(phrase, splitBy, checkVS);
                if(wordToCheck.compareTo(wordToCompareWith) < 0 ){
                    wordToSwitchWith =  wordToCompareWith;
                    switchWithNumber = checkVS;
                }
            }
            phrase = replaceWord(phrase, splitBy, count, wordToSwitchWith);
            phrase = replaceWord(phrase, splitBy, switchWithNumber, wordToCheck);
        }
        if (order == '>'){
            String aux;
            aux = "";
            for(int count = numberOfWords ; count >= 1; count--){
                aux += getWord(phrase, splitBy, count);
                if(count != 1){
                    aux += splitBy;
                }
            }
            phrase = aux;
        }
        return phrase;
    }

    private static String replaceWord(String phrase, String splitBy, int position, String replacement){
        String output;
        int numberOfWords;
        output="";
        numberOfWords = countWords(phrase, splitBy);
        for(int count = 1 ; count <= numberOfWords; count++){
            if(count != position){
                output += getWord(phrase, splitBy, count);
            } else {
                output += replacement;
            }
            if(count != numberOfWords){
                output += splitBy;
            }
        }
        return output;
    }
}
hvvq6cgz

hvvq6cgz1#

下面是BubbleSort的一个实现,其中包含不使用数组对字符串进行排序的算法。

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

    /* sort alphabetically this string without using arrays*/
    String phrase;
    phrase = "g,f,e,d,c,b,a";
    phrase = "creo,que,sera,brasil,contra,francia,en,el,mundial"
    phrase = "factura,casa,arbol,xilofono,bedel,diploma,excursion,guea";
    System.out.println(bubbleSort(phrase, ",", '<'));
}

private static String bubbleSort(String phrase, String splitBy, char order) {
    int numberOfWords = countWords(phrase, splitBy);
    for (int i = 0; i < numberOfWords; i++)
        for (int j = 0; j < numberOfWords - i; j++)
            if (getWord(phrase, splitBy, j).compareTo(getWord(phrase, splitBy, j + 1)) > 0)
                phrase = swapWords(phrase, splitBy, j,j + 1);
    phrase = changeOrderIfAscending(phrase, numberOfWords, splitBy, order);
    return phrase;
}

private static int countWords(String phrase, String splitBy){
    int count;
    int position;
    count = 0;
    position = 0;
    while(position != -1){
        if (count == 0){
            position = phrase.indexOf(splitBy);
        } else {
            position = phrase.indexOf(splitBy, position + 1);
        }
        if (position != -1){
            count++;
        }
    }
    return count + 1;
}

private static String getWord(String phrase, String splitBy, int number){
    String word;
    Scanner splitter;
    int count;
    splitter = new Scanner(phrase);
    splitter.useDelimiter(splitBy);
    word = "";
    count = 1;
    while (count <= number){
        word = splitter.next();
        count++;
    }
    return word;
}

private static String swapWords(String phrase, String splitBy, int number1, int number2){
    String word1 = getWord(phrase, splitBy, number1);
    String word2 = getWord(phrase, splitBy, number2);
    phrase = replaceWord(phrase, splitBy, number1, word2);
    phrase = replaceWord(phrase, splitBy, number2, word1);
    return phrase;
}

private static String replaceWord(String phrase, String splitBy, int position, String replacement){
    String output;
    int numberOfWords;
    output="";
    numberOfWords = countWords(phrase, splitBy);
    for(int count = 1 ; count <= numberOfWords; count++){
        if(count != position){
            output += getWord(phrase, splitBy, count);
        } else {
            output += replacement;
        }
        if(count != numberOfWords){
            output += splitBy;
        }
    }
    return output;
}

private static String changeOrderIfAscending(String phrase, int numberOfWords, String splitBy, char order){
    if (order == '>'){
        StringBuilder aux;
        aux = new StringBuilder();
        for(int count = numberOfWords ; count >= 1; count--){
            aux.append(getWord(phrase, splitBy, count));
            if(count != 1){
                aux.append(splitBy);
            }
        }
        phrase = aux.toString();
    }
    return phrase;
}
njthzxwz

njthzxwz2#

以下是一些更简洁的编程技巧:
1.尽量使代码易于理解,这样就不需要注解来解释各个方法的作用。如果确实需要注解,请包括注解。
1.将您的程式码分成较小的方法。下列程式码可以包含在称为的方法中:
private static String changeOrderIfAscending(String phrase, int numberOfWords, String splitBy, char order)

if (order == '>'){
     String aux;
     aux = "";
     for(int count = numberOfWords ; count >= 1; count--){
         aux += getWord(phrase, splitBy, count);
         if(count != 1){
             aux += splitBy;
         }
     }
     phrase = aux;
 }

1.在上述方法softwords的循环中,您使用+=进行字符串连接。相反,您可以使用StringBuilder:

if (order == '>'){
     StringBuilder aux;
     aux = new StringBuilder();
     for(int count = numberOfWords ; count >= 1; count--){
         aux.append(getWord(phrase, splitBy, count));
         if(count != 1){
             aux.append(splitBy);
         }
     }
     phrase = aux.toString();
 }

在replaceWord方法中,还有另外两个字符串串联+=

相关问题