0

この割り当てでは、アルファベット順の 3 つの文字列 (つまり、文字と数字なし) の入力を要求し、辞書式に比較して、真ん中の文字列を描画します。

ここで同様の懸念が見つかりました ( 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;
    }
4

1 に答える 1

0

文字列比較ステートメントが正しくありません。チェックして書き直す必要があります。これを行う別の方法を次に示します。

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]);       
  }
}
于 2015-09-24T14:28:23.620 に答える