初学者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 尚未初始化。任何帮助,将不胜感激。