我正在寻找一种在 while 循环中使用扫描仪的方法,而无需前进两次。
String desc = "";
while (!scanner.next().equals("END")) {
desc = desc + scanner.next();
}
如您所见,当 scanner.next()
在 while 循环的条件和 while 循环本身内部调用时。我希望它只推进扫描仪一次。有什么办法吗?
最佳答案
是的,这是可能的。您还应该检查 Scanner
在其输入中是否有更多标记
String desc = "";
String next = null;
while (scanner.hasNext() && !(next = scanner.next()).equals("END")) {
desc = desc + next;
}
关于java - 是否可以在 while 循环中分配变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19129573/