Java显示字符串比较的输出

Java显示字符串比较的输出,java,compareto,lexicographic,Java,Compareto,Lexicographic,作业要求输入三个字母串(即字母和数字),然后按字典顺序进行比较并画出中间的一个 我在这里发现了一个类似的问题(),但无法评论添加我的问题。我(暂时)对如何正确返回输出进行了排序,但现在代码没有给出任何输出,我不知道我做错了什么 public static void main(String[] args) { printHeading(); String topString; String middleString; String bottomString;

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

我在这里发现了一个类似的问题(),但无法评论添加我的问题。我(暂时)对如何正确返回输出进行了排序,但现在代码没有给出任何输出,我不知道我做错了什么

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;
    }
publicstaticvoidmain(字符串[]args)
{
打印标题();
弦上弦;
字符串中间字符串;
串底串;
扫描仪输入;
in=新扫描仪(System.in);
System.out.println(“请输入第一个单词:”);
而(!in.hasNext(“[A-Za-z]+”)
{
System.out.println(“请仅使用字母值”);
System.out.println(“请输入第一个单词:”);
in.nextLine();//捕获第一个单词
}
字符串firstWord=in.nextLine();
System.out.println(“请输入第二个单词:”);
而(!in.hasNext(“[A-Za-z]+”)
{
System.out.println(“请仅使用字母值”);
System.out.println(“请输入第二个单词:”);
in.nextLine();//捕获第二个单词
}
字符串secondWord=in.nextLine();
System.out.println(“请输入第三个单词:”);
而(!in.hasNext(“[A-Za-z]+”)
{
System.out.println(“请仅使用字母值”);
System.out.println(“请输入第三个单词:”);
in.nextLine();//捕获第三个单词
}
字符串thirdWord=in.nextLine();
if(第一个字等信号案例(第二个字)和第二个字等信号案例(第三个字))
{
System.out.println();
System.out.println(“单词相同!请重试”);
}
if(firstWord.compareTo(secondWord)>0&&firstWord.compareTo(thirdWord)>0)
{ 
上串=第一个字;
}
else if(firstWord.compareTo(secondWord)<0&&firstWord.compareTo(thirdWord)>0)
{
中间字符串=第一个字;
System.out.println();
System.out.println(“词典顺序中的第二个单词是:“+middleString”);
}
其他的
{
底部字符串=第一个字;
}
if(secondWord.compareTo(firstWord)>0&&secondWord.compareTo(thirdWord)>0)
{
上串=第二个字;
}
else if(secondWord.compareTo(firstWord)<0&&secondWord.compareTo(thirdWord)>0)
{
中间字符串=第二个单词;
System.out.println();
System.out.println(“词典顺序中的第二个单词是:“+middleString”);
}
其他的
{
底部字符串=第二个字;
}
if(第三个字比较(第二个字)>0和第三个字比较(第一个字)>0)
{
上串=第三个字;
}
else if(第三个字比较(第二个字)<0和第三个字比较(第一个字)>0)
{
中间字符串=第三个字;
System.out.println();
System.out.println(“词典顺序中的第二个单词是:“+middleString”);
}
其他的
{
底部字符串=第三个字;
}

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

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]);       
  }
}
import java.util.Scanner;
公共类findmddleword
{
公共静态void main(字符串[]args)
{
String[]wordArray=新字符串[3];
扫描仪输入;
in=新扫描仪(System.in);
System.out.println(“请输入第一个单词:”);
而(!in.hasNext(“[A-Za-z]+”)
{
System.out.println(“请仅使用字母值”);
System.out.println(“请输入第一个单词:”);
in.nextLine();//捕获第一个单词
}
字符串firstWord=in.nextLine();
字数组[0]=第一个字;
System.out.println(“请输入第二个单词:”);
而(!in.hasNext(“[A-Za-z]+”)
{
System.out.println(“请仅使用字母值”);
System.out.println(“请输入第二个单词:”);
in.nextLine();//捕获第二个单词
}
字符串secondWord=in.nextLine();
字数组[1]=第二个字;
System.out.println(“请输入第三个单词:”);
而(!in.hasNext(“[A-Za-z]+”)
{
System.out.println(“请仅使用字母值”);
System.out.println(“请输入第三个单词:”);
in.nextLine();//捕获第三个单词
}
字符串thirdWord=in.nextLine();
字数组[2]=第三个字;
字符串温度;
int i,j=0;
对于(i=0;i
请给出示例输入以及预期和实际输出。是否有充分的理由(例如分配中的限制)不只是将元素写入数组,对数组进行排序,然后选择中间的元素?基本上,分配是使用if/else/elseif和逻辑来使用我们已知的代码。太“超越”了不鼓励严格意义上的赋值。示例输入类似于“abcd”“Glad”“zulu”(没有特定顺序);按照正常的顺序系统,顺序是Glad,abcd,zulu。我(假设)实际的预期顺序应该是“abcd,Glad,zulu”,并让程序返回“Glad”。我做到了(就像这样,我非常确定)然后尝试将“IgnoreCase”位添加到所有的“.compareTo”,但它突然决定不返回任何内容。我很想使用数组函数,但不幸的是,它超出了赋值的范围。比较语句怎么会错?您能指出我犯错误的地方吗?