Java 编写一个程序,在不使用数组的情况下查找第二长单词

Java 编写一个程序,在不使用数组的情况下查找第二长单词,java,Java,我需要一个java程序来查找句子中第二长的单词(不使用数组) 这是我目前掌握的代码: import java.io.*; class Second_longest_Trial { public static void main(String args[]) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print

我需要一个java程序来查找句子中第二长的单词(不使用数组)

这是我目前掌握的代码:

import java.io.*;
class Second_longest_Trial {
  public static void main(String args[]) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the sentence");
    String s = in .readLine();
    s = s.trim() + " ";
    String longest = s.substring(0, s.indexOf(' '));
    String sec = longest;
    int l = s.length();
    String temp = " ", str = " ";

    for (int i = s.indexOf(' ') + 1; i < l; i++) {
      char ch = s.charAt(i);

      if (ch != ' ')
        temp = temp + ch;
      else {
        if (temp.length() > longest.length()) {
          sec = longest;
          longest = temp;
        } else if (temp.length() > sec.length()) {
          sec = temp;
        }
        temp = " ";
      }
    }

    System.out.println("Longest word is " + longest);
    System.out.println("Second Longest word is " + sec);
  }
}
import java.io.*;
第二类最长的审判{
公共静态void main(字符串args[])引发IOException{
BufferedReader in=新的BufferedReader(新的InputStreamReader(System.in));
System.out.println(“输入句子”);
字符串s=in.readLine();
s=s.trim()+“”;
字符串最长=s.substring(0,s.indexOf(“”));
字符串秒=最长;
int l=s.长度();
字符串temp=“”,str=“”;
对于(int i=s.indexOf(“”)+1;i最长长度()){
秒=最长;
最长=温度;
}else if(临时长度()>秒长度()){
秒=温度;
}
温度=”;
}
}
System.out.println(“最长单词为”+最长);
System.out.println(“第二长单词为“+sec”);
}
}
当我提供输入时-

萨扬回家了

这个输出-

最长的单词是Sayan 第二长的单词是Sayan

我应该得到如下输出-

最长的单词是Sayan 第二长的单词是goes


放弃你的不稳定初始设置
最长
。按以下方式创建它们和
temp

String longest="";
String sec="";
String temp="";

for(int i = 0; i < l; i++) {
    ...
字符串最长=”;
字符串sec=“”;
字符串temp=“”;
对于(int i=0;i
为什么要将
longest
sec
都设置为第一个单词?猜猜如果第一个单词是句子中最长的,会发生什么

然后,您的代码生成输出:

最长的单词是Sayan
第二长的单词是“家”


这比你现在拥有的更正确,但仍然不是你所期望的……因为在句子的末尾有一个
,你必须注意-也许检查
ch!=''
更复杂一点,并检查
我把它留给你去弄清楚如何正确地做到这一点.

以下是查找第二长单词的基本代码:

       class Second
       {
            public static void main(String str)//str has the required sentence
            {
                String m="",q="";
                int lar=0;
                str=str+" ";
                for(int i=1;i<=2;i++)
                {
                    for(int k=0;k<str.length();k++)
                    {
                       char ch=str.charAt(k);
                       if(ch!=' ')
                       m=m+ch;
                       else
                       {
                           if(m.length()>lar)
                           {
                              q=m;
                              lar=q.length();
                           }
                           m="";
                       }
                     }
                     if(i==1)
                     {
                        System.out.println("The longest word is: "+q);
                        str=str.replace(q," ");
                        lar=0;
                     }
                }
                System.out.println("The second longest word is: "+q);
             }
       }
第二类
{
publicstaticvoidmain(stringstr)//str具有必需的语句
{
字符串m=“”,q=“”;
int-lar=0;
str=str+“”;

对于(int i=1;i这是因为您已将最长的元素初始化为第一个单词,将sec也初始化为最长的元素(即Sayan本身)。现在是longest=“Sayan”和sec=“Sayan”,您进入了数组中,但从未找到任何大于最长或秒的单词。

到目前为止,您尝试了什么?这样您将获得大量的反对票。我们不是来做您的家庭作业的。欢迎使用堆栈溢出!请拿着,四处看看,并通读,尤其是将您的作业倾倒在我们身上是不可能的金:一个好问题。雇佣一个自由职业者,我们不是一个代码工厂。哎哟,我想简单地加上“尽快”会让你预期的反对票增加一倍。我建议你加上评论(
//这是x、y和z
),向提问者解释一切都在做什么