好的,我的代碼應該打印輸入的單詞總量,最長和最短的單詞。我似乎無法弄清楚如何讓它打印出最短的單詞,總的和最長的工作。我似乎無法讓我的程序打印最短的單詞
public void stringInput()
{
Scanner keyboard = new Scanner(System.in);
int wordcount =0;
String word ="";
String longest = "";
String shortest = "";
while(! word.equals("DONE"))
{
System.out.print("Please enter a word or DONE to exit: ");
word = keyboard.next();
if(!word.equals("DONE"))
{
wordcount ++;
if (word.length() < shortest.length())
{
shortest = word;
}
if (word.length() > longest.length())
{
longest = word;
}
}
}
System.out.println("Thank you for entering "+wordcount+" words.");
System.out.println("Longest word :"+ longest);
System.out.println("Shortest word :"+ shortest);
}
所以我會用字替換「」? – TheCompile
您可以通過我建議的三種方式(或者augustoccesar建議的方式)來解決這個問題。但是,如果根本沒有詞語,用詞替換「」將不起作用。 –
我很困惑,爲什麼你使用wordcount,如果只是加入了輸入的單詞總量? – TheCompile