Java中句子中最短和最长的单词
我是Java编程新手!因此,试图解决一个问题,找到一个句子中最短和最长的单词。我的程序如下所示。我希望你们能为我的计划指明正确的方向-Java中句子中最短和最长的单词,java,word,Java,Word,我是Java编程新手!因此,试图解决一个问题,找到一个句子中最短和最长的单词。我的程序如下所示。我希望你们能为我的计划指明正确的方向- public class StringProblems { void shortAndLongWord(String str) { String sw = "", lw = ""; int s = str.length(), l = 0; String words[] = str.split(" ");
public class StringProblems {
void shortAndLongWord(String str) {
String sw = "", lw = "";
int s = str.length(), l = 0;
String words[] = str.split(" ");
for(String word:words) {
if(word.length()<s)
sw = word;
else if(word.length()>l)
lw = word;
}
System.out.println("LONGEST WORD : "+lw);
System.out.println("SHORTEST WORD : "+sw);
}
public static void main(String[] args) {
Scanner scr = new Scanner(System.in);
StringProblems obj = new StringProblems();
System.out.printf("Enter a line to get shortest and longest word:");
String str = scr.nextLine();
str += " ";
obj.shortAndLongWord(str);
}
}
公共类问题{
void shortAndLongWord(字符串str){
字符串sw=“”,lw=“”;
int s=str.length(),l=0;
字符串字[]=str.split(“”);
for(字符串字:字){
if(word.length()l)
lw=单词;
}
System.out.println(“最长单词:+lw”);
System.out.println(“最短单词:+sw”);
}
公共静态void main(字符串[]args){
扫描仪scr=新扫描仪(System.in);
StringProblems obj=新的StringProblems();
System.out.printf(“输入一行以获得最短和最长的单词:”);
字符串str=scr.nextLine();
str+=”;
短词和长词(str);
}
}
此程序的输出为:
*输入一行以获得最短和最长的单词:这是一个句子
最长单词:
最短单词:句子**
我不知道我的逻辑哪里出错了!请帮我解决 您需要不断更新
l
(最长单词的长度)和s
(最小单词的长度)
if(word.length()l)
{
lw=单词;
l=单词长度();
}
此外,还需要注意一些边界条件。
例如,如果字符串是单个单词,会发生什么情况。当输入字符串为空等时会发生什么情况。您需要不断更新
l
(最长单词的长度)和s
(最小单词的长度)
if(word.length()l)
{
lw=单词;
l=单词长度();
}
此外,还需要注意一些边界条件。
例如,如果字符串是单个单词,会发生什么情况。当输入字符串为空时会发生什么情况等。您必须不断更新当前最短和最长单词的长度:
public class StringProblems {
void shortAndLongWord(String str)
{
if (str == null)
return;
String sw="",lw="";
int s=str.length(),l=0;
String words[]=str.split(" ");
for(String word:words)
{
if(word.length()<s)
{
sw=word;
s = word.length();
}
if(word.length()>l)
{
lw=word;
l = word.length();
}
}
System.out.println("LONGEST WORD : "+lw);
System.out.println("SHORTEST WORD : "+sw);
}
public static void main(String[] args) {
Scanner scr=new Scanner(System.in);
StringProblems obj=new StringProblems();
System.out.printf("Enter a line to get shortest and longest word:");
String str=scr.nextLine();
str+=" ";
obj.shortAndLongWord(str);
}
}
公共类问题{
void shortAndLongWord(字符串str)
{
如果(str==null)
返回;
字符串sw=“”,lw=“”;
int s=str.length(),l=0;
字符串字[]=str.split(“”);
for(字符串字:字)
{
if(word.length()l)
{
lw=单词;
l=单词长度();
}
}
System.out.println(“最长单词:+lw”);
System.out.println(“最短单词:+sw”);
}
公共静态void main(字符串[]args){
扫描仪scr=新扫描仪(System.in);
StringProblems obj=新的StringProblems();
System.out.printf(“输入一行以获得最短和最长的单词:”);
字符串str=scr.nextLine();
str+=”;
短词和长词(str);
}
}
您必须不断更新当前最短和最长单词的长度:
public class StringProblems {
void shortAndLongWord(String str)
{
if (str == null)
return;
String sw="",lw="";
int s=str.length(),l=0;
String words[]=str.split(" ");
for(String word:words)
{
if(word.length()<s)
{
sw=word;
s = word.length();
}
if(word.length()>l)
{
lw=word;
l = word.length();
}
}
System.out.println("LONGEST WORD : "+lw);
System.out.println("SHORTEST WORD : "+sw);
}
public static void main(String[] args) {
Scanner scr=new Scanner(System.in);
StringProblems obj=new StringProblems();
System.out.printf("Enter a line to get shortest and longest word:");
String str=scr.nextLine();
str+=" ";
obj.shortAndLongWord(str);
}
}
公共类问题{
void shortAndLongWord(字符串str)
{
如果(str==null)
返回;
字符串sw=“”,lw=“”;
int s=str.length(),l=0;
字符串字[]=str.split(“”);
for(字符串字:字)
{
if(word.length()l)
{
lw=单词;
l=单词长度();
}
}
System.out.println(“最长单词:+lw”);
System.out.println(“最短单词:+sw”);
}
公共静态void main(字符串[]args){
扫描仪scr=新扫描仪(System.in);
StringProblems obj=新的StringProblems();
System.out.printf(“输入一行以获得最短和最长的单词:”);
字符串str=scr.nextLine();
str+=”;
短词和长词(str);
}
}
在迭代过程中,您没有更新l
和s
的值。您应该尝试以下方法:
if (word.length() < s) {
sw = word;
s = word.length();
} else if (word.length() > l) {
lw = word;
l = word.length();
}
if(word.length()l){
lw=单词;
l=单词长度();
}
在迭代过程中,您没有更新l
和s
的值。您应该尝试以下方法:
if (word.length() < s) {
sw = word;
s = word.length();
} else if (word.length() > l) {
lw = word;
l = word.length();
}
if(word.length()l){
lw=单词;
l=单词长度();
}
导入java.util.Scanner;
公共类ShortestAndLongestWordInALine{
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
系统输出打印(“输入任何句子:”);
字符串行=sc.nextLine();
sc.close();
int shortestWordLength=0;
int longestWordLength=0;
字符串最短单词=”;
字符串最长的单词=”;
int模板长度=0;
字符串[]eachWordArray=line.split(“”);
布尔值firstTime=false;
for(字符串eachWord:eachWordArray){
模板长度=每个字的长度();
if(firstTime==false){
第一次=正确;
最短字长=模板长度;
最短单词=每个单词;
longestWordLength=模板长度;
最长单词=每个单词;
}
如果(模板长度>0){
if(模板长度<最短字长){
最短字长=模板长度;
最短单词=每个单词;
}else if(模板长度>最长字长){
longestWordLength=模板长度;
最长单词=每个单词;
}
}
}
System.out.println(“shortestWordLength=“+shortestWordLength+”shortestWord=“+shortestWord”);
System.out.println(“longestWordLength=“+longestWordLength+”longestWord=“+longestWord”);
}
}
导入java.util.Scanner;
公共类ShortestAndLongestWordInALine{
公共静电
import java.util.*;
class lw
{
public static void main()
{
Scanner in=new Scanner(System.in);
String z=" ",lw="";
int q=0,o=0;
System.out.println("Enter string");
String str=in.nextLine()+" ";
int l=str.length();
for(int i=1;i<l;i++)
{
char ch=str.charAt(i);
if(ch!=' ')
z=z+ch;
else
{
q=z.length();
if(q>o)
lw=z;
o=q;
z="";
}
}
System.out.println("lw= "+lw);
System.out.println("length="+q);
}
}