Java:三个字符串,按字典顺序 [英] Java: Three strings, lexicographic order

查看:299
本文介绍了Java:三个字符串,按字典顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

beginner Java programmer here. I am trying to compare three strings to each other, and have the system spit out the second/middle word in lexicographic order.

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

This does not compile, and tells me that middleString has not been initialized. Any help would be appreciated.

推荐答案

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

The Java compiler does not know which branch of an if statement will be executed. That means that if you initialize a variable in one branch but not the other, the variable is not guaranteed to have a value assigned to it. In your code, all of the variables will of course be initialized, but the compiler has no way of knowing this, hence your error. You can just initialize the three to null or an empty string. Replace String topString, middleString, bottomString; with

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

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

Additionally, you may want to use some of Java's built-in sorting functionality to do the sorting for you:

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).

这篇关于Java:三个字符串,按字典顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆