0

我必须创建一个程序,它需要 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 ");
    }
}
4

2 回答 2

1

如果您只想使用compareTo()简单的if/else语句,那么您可以设置另一个字符串变量并比较单词。例如:

String first, second, third, result;
System.out.println("Type three words: ");
first = scan.next();
second = scan.next();
third = scan.next();

if (first.compareTo(second) > 0)
    result = second;
else
    result = first;

if (result.compareTo(third) > 0)
    result = third;
System.out.println("The smallest word lexicographically is " + result);

您还可以使用三元表达式代替 if 语句:

result = first.compareTo(second) > 0 ? (second.compareTo(third) > 0 ? third : second) : (first.compareTo(third) > 0 ? third : first);

我还建议在使用扫描仪时使用try-with-resources以便它自动关闭,所以:

try (Scanner scan = new Scanner(System.in)) {
    // rest of the code here
}

编辑:

正如 Andy Turner 在他的评论中提到的,如果从System.in. 只有当它从文件中读取时,您才会这样做。

于 2018-09-03T17:59:08.117 回答
0

您说您必须按字典顺序对 3 个字符串进行排序。我认为这意味着您必须按顺序输出 3 个字符串,从低到高,而不是只找到最小的。

下面是一些 Java 代码,用于说明以最少的比较次数执行此操作所需的逻辑:

public static void main(String[] args)
{
    String[][] tests = {
            {"a", "b", "c"},
            {"a", "c", "b"},
            {"b", "a", "c"},
            {"b", "c", "a"},
            {"c", "a", "b"},
            {"c", "b", "a"},
            };

    for(String[] s : tests)
    {
        order(s[0], s[1], s[2]);
    }
}

static void order(String first, String second, String third)
{
    int firstSecond = first.compareTo(second);
    int firstThird = first.compareTo(third);
    int secondThird = second.compareTo(third);

    if(firstSecond < 0)
    {
        if(firstThird < 0)
        {
            if(secondThird < 0)
            {
                print(first, second, third);
            }
            else
            {
                print(first, third, second);
            }
        }
        else
        {
            print(third, first, second);
        }
    }
    else if(secondThird < 0)
    {
        if(firstThird < 0)
        {
            print(second, first, third);
        }
        else
        {
            print(second, third, first);
        }
    }
    else
    {
        print(third, second, first);
    }
}

private static void print(String... arr)
{
    System.out.println(Arrays.toString(arr));
}

输出:

[a, b, c]
[a, b, c]
[a, b, c]
[a, b, c]
[a, b, c]
[a, b, c]
于 2018-09-03T18:10:17.147 回答