将字符串中的所有字母转换为小写,然后按字母顺序排列它们

时间:2016-11-16 16:57:57

标签: java

我的输出首先获得大写字母,并将这些大写字母按字母顺序排列,而不是考虑小写字母,并按字母顺序排列。我想首先将大写字母转换为小写字母,然后根据所需的输出将它们按字母顺序排列,并且应该仅在collectLetters()方法中进行更改。有人可以帮帮我吗?提前谢谢。

public class QuizIID
 {

public static String collectLetters(String text)
{
    char[] charArray = text.toCharArray();
  Arrays.sort(charArray);
  String sortedString = new String(charArray);
 return sortedString;
}

// runTest方法

private static boolean runTest(int testNum, String p0, boolean hasAnswer, String p1) 
{
    System.out.print("Test #" + testNum + ": [" + "\"" + p0 + "\"");
    System.out.println("]");
    String answer;
    answer = collectLetters(p0);
    boolean res;
    res = true;

    if (hasAnswer) {
        System.out.println("Desired answer:");
        System.out.println("\t" + "\"" + p1 + "\"");
    }
    System.out.println("Your answer:");
    System.out.println("\t" + "\"" + answer + "\"");
    if (hasAnswer) {
        res = answer.equals(p1);
    }
    if (!res) {
        System.out.println("DOESN'T MATCH!!!!");
    } else if (hasAnswer) {
        System.out.println("Correct!");
    }
    System.out.println();
    return res;
}

//主要方法

public static void main(String[] args) {
    int count = 0;

    String p0;
    String p1;

    // ----- test 1 -----
    p0 = "She sells sea shells";
    p1 = "aeeeehhllllssssss";
    if( runTest(1, p0, true, p1) ) count++;
    // ------------------

    // ----- test 2 -----
    p0 = "ReMemBEr To IgnoRE CASe";
    p1 = "abceeeeegimmnoorrrst";
    if( runTest(2, p0, true, p1) ) count++;
    // ------------------

    // ----- test 3 -----
    p0 = "12345";
    p1 = "";
    if( runTest(3, p0, true, p1) ) count++;
    // ------------------

    // ----- test 4 -----
    p0 = "Programming III is fun";
    p1 = "afggiiiiimmnnoprrsu";
    if( runTest(4, p0, true, p1) ) count++;
    // ------------------

    // ----- test 5 -----
    p0 = "Knights are better than Bishops";
    p1 = "aabbeeeghhhiiknnoprrssstttt";
    if( runTest(5, p0, true, p1) ) count++;
    // ------------------

    System.out.print( count + " out of 5" );
    if (count == 5) 
  {
        System.out.println("!");
    }
  else 
  {
     System.out.println(".");
  }
}
}   

//所需的输出

  ----jGRASP exec: java QuizIID -Xlint:unchecked
Test #1: ["She sells sea shells"]
Desired answer:
    "aeeeehhllllssssss"
Your answer:
    "aeeeehhllllssssss"
Correct!

Test #2: ["ReMemBEr To IgnoRE CASe"]
Desired answer:
    "abceeeeegimmnoorrrst"
Your answer:
    "abceeeeegimmnoorrrst"
Correct!

Test #3: ["12345"]
Desired answer:
    ""
Your answer:
    ""
Correct!

Test #4: ["Programming III is fun"]
Desired answer:
    "afggiiiiimmnnoprrsu"
Your answer:
    "afggiiiiimmnnoprrsu"
Correct!

Test #5: ["Knights are better than Bishops"]
Desired answer:
    "aabbeeeghhhiiknnoprrssstttt"
Your answer:
    "aabbeeeghhhiiknnoprrssstttt"
Correct!

5 out of 5!

 ----jGRASP: operation complete.

//我的输出

  ----jGRASP exec: java QuizIID
 Test #1: ["She sells sea shells"]
 Desired answer:
"aeeeehhllllssssss"
 Your answer:
"   Saeeeehhllllsssss"
 DOESN'T MATCH!!!!

 Test #2: ["ReMemBEr To IgnoRE CASe"]
 Desired answer:
 "abceeeeegimmnoorrrst"
 Your answer:
 "ABCEEIMRRSTeeegmnoor"
 DOESN'T MATCH!!!!

Test #3: ["12345"]
Desired answer:
""
 Your answer:
 "12345"
 DOESN'T MATCH!!!!

  Test #4: ["Programming III is fun"]
  Desired answer:
  "afggiiiiimmnnoprrsu"
  Your answer:
   "   IIIPafggiimmnnorrsu"
   DOESN'T MATCH!!!!

  Test #5: ["Knights are better than Bishops"]
  Desired answer:
   "aabbeeeghhhiiknnoprrssstttt"
  Your answer:
   "    BKaabeeeghhhiinnoprrssstttt"
  DOESN'T MATCH!!!!

   0 out of 5.

  ----jGRASP: operation complete.

1 个答案:

答案 0 :(得分:-2)

替换

char[] charArray = text.toCharArray();

通过

char[] charArray = text.toLowerCase().replaceAll( "[^\\p{Lower}]","" ).toCharArray();

这使用regular expression

可运行的例子:

https://ideone.com/WSvUZy