Java 在字符串中查找中间词的输出不符合预期?

Java 在字符串中查找中间词的输出不符合预期?,java,string,Java,String,MyApproach 为了获得中间词,我首先找到了每个字符串的长度,并检查了获取单词的多个条件,例如 如果这两个字的长度相等,如果没有一个字的长度相等,如果最后两个字的长度相等,我会逐个字符检查它们,并使用compareTo作为剩余逻辑 但我无法满足一个测试用例 有谁能指导我在代码中做错了什么 公共字符串中间词(字符串字1、字符串字2、字符串字3) { 布尔b1=假; 布尔b2=假; 布尔b3=假; int l1=word1.length(); int l2=word2.length(); in

MyApproach

为了获得中间词,我首先找到了每个字符串的长度,并检查了获取单词的多个条件,例如

如果这两个字的长度相等,如果没有一个字的长度相等,如果最后两个字的长度相等,我会逐个字符检查它们,并使用compareTo作为剩余逻辑

但我无法满足一个测试用例

有谁能指导我在代码中做错了什么

公共字符串中间词(字符串字1、字符串字2、字符串字3)
{
布尔b1=假;
布尔b2=假;
布尔b3=假;
int l1=word1.length();
int l2=word2.length();
int l3=word3.length();
如果((l1>=l2)和&(l1>l3))
{
int p1=word1。比较(word2);
系统输出打印LN(p1);
如果(p1=l3)和(l2>l1))
{
int p1=word2.compareTo(word3);
系统输出打印LN(p1);
如果(p1=l1)和(l3>l2))
{
int p1=word1.比较(word3);
系统输出打印LN(p1);
如果(p1l2)和(l1>l3))
{
如果(l2>l3)
{
b2=真;
}
否则如果(l3>l2)
{
b3=真;
}
其他的
{
int p1=word2.compareTo(word3);
系统输出打印LN(p1);
如果(p1l3)和(l2>l1))
{
如果(l1>l3)
{
b1=真;
}
否则如果(l3>l1)
{
b3=真;
}
其他的
{
int p1=word1.比较(word3);
系统输出打印LN(p1);
如果(p1l1)和(l3>l2))
{
如果(l1>l2)
{
b1=真;
}
else if(l2>l1)
{
b2=真;
}
其他的
{
int p1=word1。比较(word2);
系统输出打印LN(p1);

if(p1对于中间词(“他”、“她”、“这里”)的具体情况,您正在使用第三个else-if子句:

else if((l3>=l1)&&(l3>l2))
{
    int p1=word1.compareTo(word3);
    System.out.println(p1);

    if(p1<0)
    {
        b1=true;
    }
    else
    {
        b3=true;
    }
}
是的,你的逻辑是错误的。看起来你是先比较字符串的长度,然后按字典顺序比较。在这种情况下,你可以覆盖compareTo函数并对它们进行排序。然后返回第二个元素,这是一个更干净的解决方案


似乎您不需要比较长度,只需要按字典顺序进行比较,而且您不能使用Collections或Arrays排序方法。在这种情况下,您可以使用Java字符串的函数

public static String middleWord1(String word1, String word2, String word3) {
    if (word1.compareTo(word2) == 0 || word1.compareTo(word3) == 0)
        // word1 == word2 or word1 == word3
        return word1;
    else if (word2.compareTo(word1) == 0 || word2.compareTo(word3) == 0)
        // word2 == word1 or word2 == word3
        return word2;
    else if (word3.compareTo(word1) == 0 || word3.compareTo(word2) == 0)
        // word3 == word1 or word3 == word2
        return word3;
    else if ((word2.compareTo(word1) < 0 && word1.compareTo(word3) < 0) ||
        (word3.compareTo(word1) < 0 && word1.compareTo(word2) < 0))
        // word2 < word1 < word3 or word3 < word1 < word2
        return word1;
    else if ((word1.compareTo(word2) < 0 && word2.compareTo(word3) < 0) ||
        (word3.compareTo(word2) < 0 && word2.compareTo(word1) < 0))
        // word1 < word2 < word3 or word3 < word2 < word1
        return word2;
    else
        // word1 < word3 < word2 or word2 < word3 < word1
        return word3;
}
公共静态字符串middleWord1(字符串word1、字符串word2、字符串word3){
if(word1.compareTo(word2)==0 | | word1.compareTo(word3)==0)
//word1==word2或word1==word3
返回单词1;
else if(word2.compareTo(word1)==0 | | word2.compareTo(word3)==0)
//word2==word1或word2==word3
返回单词2;
else if(word3.compareTo(word1)==0 | | word3.compareTo(word2)==0)
//word3==word1或word3==word2
返回单词3;
else if((word2.compareTo(word1)<0&&word1.compareTo(word3)<0)||
(word3.compareTo(word1)<0和&word1.compareTo(word2)<0))
//word2
使用Vector类可能是个好主意,因为它会自动按排序顺序存储。找到大小并打印出中间的一个是可能的。

很抱歉,您的方法有太多问题,我认为设置它是浪费时间。
例如,考虑您的整个
if语句

1: if      ( (l1>=l2)&&(l1>l3) ) {}
2: else if ( (l2>=l3)&&(l2>l1) ) {}
3: else if ( (l3>=l1)&&(l3>l2) ) {}      
4: else if ( (l1 >l2)&&(l1>l3) ) {}
5: else if ( (l2 >l3)&&(l2>l1) ) {}
6: else if ( (l3 >l1)&&(l3>l2) ) {}
7: else if ( (l1==l2)&&(l1==l3)) {}
第4-6行完全没有必要,因为第1-3行完全涵盖了这些情况。这段代码永远不会到达。
因此,我不想修改您的代码,而是想向您展示如何以专业的方式解决您的问题。

Java内置解决方案
Java类库提供了对单词进行排序所需的一切。将单词放入列表中,然后调用
集合。排序
。之后,您可以通过从列表中获取第二个条目来找到中间的单词

List<String> list = new LinkedList<>();
list.add("her");
list.add("here");
list.add("he");
Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
String middleWord = list.get(1); // "her"
其次,我将向您介绍一种方法,该方法采用两个
字符串
值,并以字典形式对它们进行比较。如果第一个单词在第二个之前,则结果为
-1
,如果第二个单词在第一个之前,则结果为
+1
,如果它们相等,则结果为
0

此方法使用上面的方法获取循环中每个位置的每个字符的ascii值,并进行比较

public static int compare(String s1, String s2){
    int runs = Math.max(s1.length(), s2.length());
    int index = 0;
    while( index < runs){
        int c1 = getInsensitiveCharAt(s1, index);
        int c2 = getInsensitiveCharAt(s2, index);
        if(c1 < c2) return -1;
        if(c1 > c2) return 1;
        index++;
    }
    return 0;
}
就这样。现在你可以通过打电话来表达你的意思了

String[] list = sort(new String[] {"her","here","he"});
String middleWord = list[1]; // "her"

小的改进
这个手工制作的解决方案还为您提供了根据您的特殊需要进行调整的机会。这意味着如果您真的对第二个单词感兴趣,您可以在特定点停止排序。只需修改排序方法,在
n-ths
停止排序,然后返回该单词

public static String getSortedWordAt(String[] list, int pos){
    for (int i = 0; i < pos; i++) {
        [...]
    }
    return list[pos - 1];
}

您正在试图找到中间的单词(完整的单词/字符串?)三个单词中的一个?你为什么要比较字符串的长度?你必须找到具有中间数量字符的单词吗?如果传入的三个单词是
'her''he''here'
@EricG是的,我正在尝试从这三个单词中找出完整单词的中间单词。我比较了字符串的长度,因此无论哪个单词是第二个最小的,我都能得到它。对于输入她的“他”,我得到的是他作为实际输出。当我在笔记本上干运行它时,我只得到她。对于输入的“她”,“他”在我的程序中
public static int compare(String s1, String s2){
    int runs = Math.max(s1.length(), s2.length());
    int index = 0;
    while( index < runs){
        int c1 = getInsensitiveCharAt(s1, index);
        int c2 = getInsensitiveCharAt(s2, index);
        if(c1 < c2) return -1;
        if(c1 > c2) return 1;
        index++;
    }
    return 0;
}
public static String[] sort(String[] list){
    for (int i = 0; i < list.length - 1; i++) {
        for (int j = i + 1; j < list.length; j++) {         
            if (compare(list[j], list[i]) == -1) {
                String temp = list[j];
                list[j] = list[i];
                list[i] = temp;
            }
        }
    }
    return list;
}
String[] list = sort(new String[] {"her","here","he"});
String middleWord = list[1]; // "her"
public static String getSortedWordAt(String[] list, int pos){
    for (int i = 0; i < pos; i++) {
        [...]
    }
    return list[pos - 1];
}
String middleWord = getSortedWordAt(new String[] {"here","he","her"}, 2);