~java代码如下~
package one; //包名随意
import java.u; //系统自动导入
public class GetWord { 类名随意
public static void main(String[] args) {
String[] words = null;
words = getWords().toString().split("_");
for (int i = 0; i < words.length-1; i++)
{
Sy(words[i]);
}
int total=-1;
for (int i = 0; i < words.length; i++){
if(words[i]!="end")
total++;}
Sy(total+"个单词");
}
public static StringBuffer getWords() { //方法名随意
Scanner in = new Scanner);
String word = null;
StringBuffer sbf = new StringBuffer();
Sy("请输入单词(以end来结束):");
do {
word = in.next();
(word + "_");
} while (!word.equals("end"));
return sbf;
}
}
控制台输入及结果
注明:另一种方法如果包含结束输入判断语句 ch=in.read())!=-1 ,按键为:在windows下就是用ctrl+z,在linux就用ctrl+d,即代表了-1(EOF)。