首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java显示字符串比较的输出

Java显示字符串比较的输出
EN

Stack Overflow用户
提问于 2015-09-24 06:10:14
回答 1查看 127关注 0票数 0

作业要求按字母顺序输入三个字符串(即字母而不是数字),然后按字典顺序进行比较并绘制中间的一个。

我在这里(Java: Three strings, lexicographic order)发现了类似的问题,但无法评论来添加我的问题。我(暂时)对如何适当地返回输出进行了排序,但现在代码没有给出任何输出,我不知道我做错了什么。

代码语言:javascript
复制
public static void main(String[] args)
  {
    printHeading();

    String topString;
    String middleString;
    String bottomString;

    Scanner in;
    in = new Scanner(System.in);
    System.out.println("Please enter a first word:");
    while (!in.hasNext("[A-Za-z]+"))
    {
      System.out.println("Please use only alphabetic values.");
      System.out.println("Please enter a first word:");
      in.nextLine();  // Captures the first word
    }
    String firstWord = in.nextLine();

    System.out.println("Please enter a second word:");
    while (!in.hasNext("[A-Za-z]+"))
    {
      System.out.println("Please use only alphabetic values.");
      System.out.println("Please enter a second word:");
      in.nextLine();  // Captures the second word
    }
    String secondWord = in.nextLine();

    System.out.println("Please enter a third word:");
    while (!in.hasNext("[A-Za-z]+"))
    {
      System.out.println("Please use only alphabetic values.");
      System.out.println("Please enter a third word:");
      in.nextLine();  // Captures the third word
    }
    String thirdWord = in.nextLine();

    if (firstWord.equalsIgnoreCase(secondWord) && secondWord.equalsIgnoreCase(thirdWord))
    {
      System.out.println(); 
      System.out.println("The words are the same! Please try again.");     
    }

    if (firstWord.compareTo(secondWord) > 0 && firstWord.compareTo(thirdWord) > 0) 
    { 
      topString = firstWord; 
    }

    else if (firstWord.compareTo(secondWord) < 0 && firstWord.compareTo(thirdWord) > 0)
    {
      middleString = firstWord;
      System.out.println();
      System.out.println("The second word in lexicographic order is: " + middleString);
    }

    else
    {
      bottomString = firstWord;
    }

    if (secondWord.compareTo(firstWord) > 0 && secondWord.compareTo(thirdWord) > 0)
    {
      topString = secondWord;
    }

    else if (secondWord.compareTo(firstWord) < 0 && secondWord.compareTo(thirdWord) > 0)
    {
      middleString = secondWord;
      System.out.println();
      System.out.println("The second word in lexicographic order is: " + middleString);
    }

    else
    {
      bottomString = secondWord;
    }

    if (thirdWord.compareTo(secondWord) > 0 && thirdWord.compareTo(firstWord) > 0)
    {
      topString = thirdWord; 
    }

    else if (thirdWord.compareTo(secondWord) < 0 && thirdWord.compareTo(firstWord) > 0)
    {
      middleString = thirdWord;
      System.out.println();
      System.out.println("The second word in lexicographic order is: " + middleString);
    }

    else
    {
      bottomString = thirdWord;
    }
EN

回答 1

Stack Overflow用户

发布于 2015-09-24 22:28:24

您的字符串比较语句不正确-您需要检查它们并重写它。这是另一种方法:

代码语言:javascript
复制
import java.util.Scanner;

public class FindMiddleWord 
{
public static void main(String[] args)
{

    String[] wordArray = new String[3];

    Scanner in;
    in = new Scanner(System.in);
    System.out.println("Please enter a first word:");
    while (!in.hasNext("[A-Za-z]+"))
    {
      System.out.println("Please use only alphabetic values.");
      System.out.println("Please enter a first word:");
      in.nextLine();  // Captures the first word
    }
    String firstWord = in.nextLine();
    wordArray[0] = firstWord;

    System.out.println("Please enter a second word:");
    while (!in.hasNext("[A-Za-z]+"))
    {
      System.out.println("Please use only alphabetic values.");
      System.out.println("Please enter a second word:");
      in.nextLine();  // Captures the second word
    }
    String secondWord = in.nextLine();
    wordArray[1] = secondWord;

    System.out.println("Please enter a third word:");
    while (!in.hasNext("[A-Za-z]+"))
    {
      System.out.println("Please use only alphabetic values.");
      System.out.println("Please enter a third word:");
      in.nextLine();  // Captures the third word
    }
    String thirdWord = in.nextLine();
    wordArray[2] = thirdWord;

    String temp;
    int  i,j = 0;
    for (i = 0; i < wordArray.length; i++) {
        for (j = 0; j < wordArray.length; j++) {
            if (wordArray[i].compareToIgnoreCase(wordArray[j]) < 0) {
                temp = wordArray[i];
                wordArray[i] = wordArray[j];
                wordArray[j] = temp;
            }
        }
    }       
    System.out.println("The Middle Word is : "+wordArray[1]);       
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32750337

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档