初学者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);
}
}
这不编译,并告诉我中间字符串尚未初始化。任何帮助将不胜感激。
答案 0 :(得分: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)
。
答案 1 :(得分:0)
您尚未初始化变量middleString
并在pgrm的末尾使用它
System.out.println("The second string in lexicographic order: " + middleString);
简单地初始化变量如下所示,它将编译。
String middleString ="";
答案 2 :(得分:0)
将你的3字符串topString,middleString,bottomString初始化为空,如下所示:
String topString = "";
String middleString = "";
String bottomString = "";
必须编译。
答案 3 :(得分: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
。你应该做同样的事情,看看每个案例四个。)
答案 4 :(得分:0)
如果我理解你的场景,你只对字符串感兴趣,你可以利用自然字符串顺序并使用JDK类来帮助你:
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();
}
}
当我在Eclipse中运行它(继续,粘贴并尝试它!)使用John,JOHN和Kevin作为名称时,我得到了这个结果:
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'