2014-12-08 48 views
0

对于这个程序,我需要有用户输入字符串,然后将它们与反向的对应字符串放在一个数组中。整个阵列将被按字母顺序排列。因此,它应该像这样工作:Alphabetizing字符串和颠倒的字符串?

  • 输入:草莓香蕉,苹果,葡萄

  • 输出:苹果,ananab,香蕉,elppa,葡萄,separg,草莓,yrrebwarts

我有这个代码,它在一定程度上有效。但是,它只是将反转和正常但分开的按字母顺序排列。因此,它结束了看起来像这样:

  • 输出:ananab,elppa,separg,yrrebwarts,苹果,香蕉,葡萄,草莓

正如你可以看到它字母排序的话有些学位,但不是它应该如何。这里是我的代码:

import java.util.Scanner; 

public class WordGame { 

    public static void main(String[] args) { 
    String reverseInput = " "; //creates an empty string where the reverse will be stored before  putting it into the array 
    String[] wordArray = new String[1000]; //creates an array that allows for 500 words, and 500  reverses of said words 
    int s = 0; 
    Scanner sc = new Scanner(System.in); 
    System.out.println("Please enter the words you would like to reverse. To finish entering words enter a blank line:"); 
    String userInput = sc.nextLine(); //allows for user to input words 

    int length = userInput.length(); 
    int i = 0; 

    while (!userInput.equals("")){ //runs until the user enters a blank line 
     reverseInput = " "; 
     for(i = length - 1; i >= 0; i--) 
     reverseInput += userInput.charAt(i); 
     wordArray[s] = userInput; //reverses user inputted strings by taking the last letter and putting it in front, repeating until the whole word is reversed 
     wordArray[s + 1] = reverseInput; 
     s += 2; 

     userInput = sc.nextLine(); 
     length = userInput.length(); 
    } 

    for(int j = 0; j < s-1; j++){ //beginning of alphabetical sorting 
     for(int k = 0; k < s-1-j; k++){ 
     int l = 0; 
     while((int)wordArray[k].charAt(l) == (int)wordArray[k+1].charAt(l)) 
      l++; 
     if ((int)wordArray[k].charAt(l) > (int)wordArray[k+1].charAt(l)){ 
      String holder = wordArray[k]; 
      wordArray[k] = wordArray[k+1]; 
      wordArray[k+1] = holder; 
     } 
     } 

    } 

     for(i = 0; i < wordArray.length; i++){ 
      if (wordArray[i]!= null){ 
      System.out.print(wordArray[i] + " "); //prints out contents of array 
     } 
     } 
     } 
    } 

我不确定是什么问题。任何帮助将非常感激。谢谢!

+1

对您可以使用什么有任何限制吗?即没有列表或您不被允许使用内置的排序功能? – vandale 2014-12-08 00:06:21

+0

@vandale我们不能使用内置的排序函数或列表。我在这里使用的一切都是我们可以使用的程度。 – throwaway298 2014-12-08 00:09:55

回答

1

据我可以看到你在“reverseInput”前加上一个空格reverseInput = " "; 因为你的反转字符串不会以你认为的字符开始(它以空白开始),结果不是你真的想要。尝试删除空白并重试您的代码。

+0

试过这个,不幸的是没有任何改变。 – throwaway298 2014-12-08 00:23:44

+0

,只是我的建议:即使你的for语句使用一行,也要尽量使用括号。因为'wordArray [s] = userInput;'是缩进的,所以可以认为它是for语句的一部分,而不是。 进一步'创建一个空字符串,其中反向将存储之前'是错误的。你创建了一个正确的字符串,但是你不用“+ =”修改这个字符串。 Java使用每个= +执行一个新的字符串与连接并将其存储在reverseInput中。 – 2014-12-08 00:27:29

+0

它应该工作。您需要在您的while块开始后更改该行。你写了'reverseInput =“”;“。将其更改为'reverseInput =“”;并且您的代码应该可以工作 – 2014-12-08 00:34:37

0

确实有更简单的方法可以做到这一点,这取决于您是否想使用Java的内置类来实现这种功能。我只是写了这个,它完成了你想要的颠倒的字符串。

import java.util.ArrayList; 
import java.util.Collections; 
import java.util.List; 

public class Solution { 
    public static void main(String args[]) { 
     ArrayList<String> values = new ArrayList<String>(); 
     values.add("strawberry"); 
     values.add("banana"); 
     values.add("apple"); 
     values.add("grapes"); 
     Solution s = new Solution(); 
     values = s.sortList(values); 
     int itemCt = 1; 
     for (String item : values) { 
      System.out.println(itemCt + ": " + item); 
      itemCt++; 
     } 
    } 

    public ArrayList<String> sortList(List<String> strings) { 
     ArrayList<String> combinedList = new ArrayList<String>(); 
     for (String str : strings) { 
      combinedList.add(str); 
      combinedList.add(new StringBuilder(str).reverse().toString()); 
     } 
     Collections.sort(combinedList); 
     return combinedList; 
    } 

}