正如标题所述,我试图使用扫描仪首先打印输入的两个单词。之后,我需要另外一个单词,然后打印前两个单词的最后一个单词,输入下一个单词。这是我尝试过的:
Scanner in = new Scanner(System.in);
System.out.println("TYPE MANY WORDS, ONE PER LINE:");
String prev = in.next();
while(in.hasNext())
{
String word = in.next();
System.out.println(prev);
System.out.println(word);
}
答案 0 :(得分:0)
对于后续迭代,您需要为prev
试
Scanner in = new Scanner(System.in);
System.out.println("TYPE MANY WORDS, ONE PER LINE:");
String prev = in.next();
while(in.hasNext())
{
String word = in.next();
System.out.println(prev);
System.out.println(word);
prev = word;
if (word.equals ("q")) break;
}
答案 1 :(得分:0)
Scanner in = new Scanner(System.in);
System.out.println("TYPE MANY WORDS, ONE PER LINE:");
String prev = in.next();
while(in.hasNext())
{
String word = in.next();
System.out.println(prev);
System.out.println(word);
prev = word;
}
您需要存储上一个单词。