我需要一些帮助,这里与我的java学校工作。 我们被告知要提示用户输入五个单词,然后从中确定最长的单词并打印以控制最长的单词以及其中的字符数。Java SE数组帮助需要请
现在,我只设法通过显示最长的字符数来排列它们,但我不知道如何显示这个词本身。有人可以帮助我,请记住我是编程的总新手,我的进步仍然只是在基础知识,所以请尽量不要让我太复杂。另外,随时查找那些冗余代码,因为我知道我有很多。 :) 谢谢!
import java.util.Scanner;
import java.util.Arrays;
class LongestWord
{
public static void main(String [] args)
{
Scanner theInput = new Scanner(System.in);
System.out.println("Please enter your five words");
String fWord = theInput.next();
String sWord = theInput.next();
String tWord = theInput.next();
String fhWord = theInput.next();
String ffWord = theInput.next();
System.out.println(fWord + sWord + tWord + fhWord + ffWord);
int [] wordCount = new int[5];
wordCount[0] = fWord.length();
wordCount[1] = sWord.length();
wordCount[2] = tWord.length();
wordCount[3] = fhWord.length();
wordCount[4] = ffWord.length();
Arrays.sort(wordCount);
System.out.println(wordCount[4]);
}
}
您不需要排序只是为了找到最长的单词,也不需要将所有单词存储在内存中。逐一阅读,只存储到目前为止最长的单词。 – Henry
你做得很好,只是多想一点,你应该可以自己编写代码! – Abubakkar