【问题标题】:Java Displaying output of string comparisonJava显示字符串比较的输出
【发布时间】:2015-09-23 22:10:14
【问题描述】:

作业要求输入三个按字母顺序输入的字符串(即字母和无数字),然后按字典顺序比较并画出中间的一个。

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

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;
    }

【问题讨论】:

  • 请给出示例输入以及预期和实际输出。
  • 是否有充分的理由(例如分配中的限制)不简单地将元素写入数组,对数组进行排序,然后选择中间元素?
  • 基本上赋值就是使用if/else/elseif和逻辑来使用我们已经知道的代码。不鼓励“超出”严格的作业范围。示例输入类似于“abcd”“Glad”“zulu”(无特定顺序);在正常的序数系统中,顺序是 Glad、abcd、zulu。我(假设)实际的预期顺序应该是“abcd, Glad, zulu”并且让程序返回“Glad”。我有它(就像这样,我相当确定)然后尝试将“IgnoreCase”位添加到所有 '.compareTo' ,但它决定突然不返回任何东西。

标签: java compareto lexicographic


【解决方案1】:

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

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]);       
  }
}

【讨论】:

  • 我很想使用数组函数,但不幸的是,它超出了任务的范围。比较语句怎么错了?你能指出我犯错的地方吗?
猜你喜欢
  • 2017-04-14
  • 1970-01-01
  • 2014-11-11
  • 2012-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多