2

初学者Java程序员在这里。我正在尝试将三个字符串相互比较,并让系统按字典顺序吐出第二个/中间词。

import java.util.*;

public class Ordered2
{
public static void main(String[] args)
{
    String firstString, secondString, thirdString;

    Scanner keyboard = new Scanner(System.in);

    System.out.println("Enter three different strings.");
    System.out.println("The string in the middle order lexicographically will be displayed.");
    firstString = keyboard.nextLine();
    secondString = keyboard.nextLine();
    thirdString = keyboard.nextLine();

    String topString, middleString, bottomString;

    if (firstString.compareTo(secondString) > 0 && (firstString.compareTo(thirdString) > 0)) 
    { topString = firstString; }
    else if (firstString.compareTo(secondString) < 0 && (firstString.compareTo(thirdString) > 0)) {
    middleString = firstString; }
    else { bottomString = firstString; }

    if (secondString.compareTo(firstString) > 0 && (secondString.compareTo(thirdString) > 0)) {
    topString = secondString; }
    else if (secondString.compareTo(firstString) < 0 && (secondString.compareTo(thirdString) > 0)) {
    middleString = secondString; }
    else { bottomString = secondString; }

    if (thirdString.compareTo(secondString) > 0 && (thirdString.compareTo(firstString) > 0)) {
    topString = thirdString; }
    else if (thirdString.compareTo(secondString) < 0 && (thirdString.compareTo(firstString) > 0)) {
    middleString = thirdString; }
    else { bottomString = thirdString; }

    System.out.println("The second string in lexicographic order: " + middleString);
    }
}

这不会编译,并告诉我 middleString 尚未初始化。任何帮助,将不胜感激。

4

5 回答 5

4

Java 编译器不知道if将执行语句的哪个分支。这意味着如果您在一个分支中初始化一个变量而不是另一个分支,则不能保证该变量具有分配给它的值。在您的代码中,所有变量当然都会被初始化,但是编译器无法知道这一点,因此您的错误。您可以将三个初始化为null或一个空字符串。替换String topString, middleString, bottomString;

String topString = null;
String middleString = null;
String bottomString = null;

此外,您可能希望使用 Java 的一些内置排序功能为您进行排序:

import java.util.*;

public class Ordered2
{
public static void main(String[] args)
{
    String firstString, secondString, thirdString;

    Scanner keyboard = new Scanner(System.in);

    System.out.println("Enter three different strings.");
    System.out.println("The string in the middle order lexicographically will be displayed.");
    firstString = keyboard.nextLine();
    secondString = keyboard.nextLine();
    thirdString = keyboard.nextLine();

    String[] array = new String[] {firstString, secondString, thirdString};

    Arrays.sort(array);

    System.out.println("The second string in lexicographic order: " + array[1]);
}
}

Arrays.sort()为您排序字符串。从排序数组中取出第二个(索引 1)字符串会给你中间的字符串。如果要使用不区分大小写的顺序进行排序,可以使用Arrays.sort(array, String.CASE_INSENSITIVE_ORDER).

于 2014-01-30T19:08:51.480 回答
0

If I understand your scenario, you are only interested in Strings, you can take advantage of the Natural Order of Strings and use JDK classes to help you out:

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

public class StringSorter {

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

    System.out.println("Enter the FIRST string:");
    String first = scanner.nextLine();
    System.out.println("Enter the SECOND string:");
    String second = scanner.nextLine();
    System.out.println("Enter the THIRD string:");
    String third = scanner.nextLine();

    List<String> strings = new ArrayList<String>();

    strings.add(first);
    strings.add(second);
    strings.add(third);

    System.out.println("Before sort:");
    for (String s : strings) {
      System.out.println(s);
    }

    Collections.sort(strings);

    System.out.println("After sort:");
    for (String s : strings) {
      System.out.println(s);
    }

    System.out.println("The Middle String is '" + strings.get(1) + "'");

    scanner.close();

  }

}

When I ran this in Eclipse (go ahead, paste it in and try it!) using John, JOHN and Kevin as the names, I got this result:

Enter the FIRST string:
John
Enter the SECOND string:
JOHN
Enter the THIRD string:
Kevin
Before sort:
John
JOHN
Kevin
After sort:
JOHN
John
Kevin
The Middle String is 'John'
于 2014-01-30T19:35:56.017 回答
0

这里的逻辑是错误的(我已经重新格式化了一下):

if (firstString.compareTo(secondString) > 0 && (firstString.compareTo(thirdString) > 0)) 
    { topString = firstString; }
else if (firstString.compareTo(secondString) < 0 && (firstString.compareTo(thirdString) > 0)) 
    { middleString = firstString; }
else 
    { bottomString = firstString; }

(我同意你的方法,我认为可以通过一些调整来工作。)我将调用字符串 S1、S2、S3。假设所有字符串都不相等,则需要考虑四种情况。我已经列出了这些,以及上面的代码在做什么:

S1 > S2 and S1 > S3    S1 is the top string   
S1 > S2 and S1 < S3    S1 is the bottom string
S1 < S2 and S1 > S3    S1 is the middle string
S1 < S2 and S1 < S3    S1 is the bottom string

其中之一是错误的。看见?

(我还没有检查其他两个if。你应该做同样的事情,每个看四个案例。)

于 2014-01-30T19:28:45.900 回答
0

您尚未初始化变量middleString并在 pgrm 末尾使用它作为

System.out.println("The second string in lexicographic order: " + middleString);

如下简单初始化变量,它将编译。

String  middleString ="";
于 2014-01-30T19:04:53.857 回答
0

只需将您的 3 个字符串 topString、middleString、bottomString 初始化为空,如下所示:

String topString = "";
String middleString = "";
String bottomString = "";

必须编译。

于 2014-01-30T19:05:00.170 回答