我必须创建一个程序,它需要 3 个字符串并按字典顺序对它们进行排序。我发现为此你必须使用该compareTo()
方法,问题是当我尝试执行 if 语句时,我发现它们是 int 而不是字符串,我什至不知道如何显示哪一个是最小的有很多不同的选择。使用该方法是否有更简单的方法(不允许使用数组或任何东西)?
import java.util.Scanner;
public class SetAQuestion2
{
public static void main (String[] args)
{
Scanner scan = new Scanner (System.in);
String first, second, third;
System.out.print("Type three words: ");
first = scan.next();
second = scan.next();
third = scan.next();
int oneA = (first. compareTo (second));
int oneB = (first.compareTo (third));
int secondA = (second. compareTo (first));
int secondB = (second.compareTo(third));
int thirdA = (third.compareTo(first));
int thirdB = (first.compareTo (second));
System.out.println("The smallest word lexicographically is ");
}
}