[英]Is it possible to assign a variable in a while loop?
我正在寻找一种在 while 循环中使用扫描仪而不前进两次的方法。
String desc = "";
while (!scanner.next().equals("END")) {
desc = desc + scanner.next();
}
如您所见,当在 while 循环的条件中和 while 循环本身内部调用scanner.next()
时。 我希望它只推进扫描仪一次。 有没有办法做到这一点?
是的,这是可能的。 您还应该检查Scanner
的输入中是否有更多令牌
String desc = "";
String next = null;
while (scanner.hasNext() && !(next = scanner.next()).equals("END")) {
desc = desc + next;
}
String temp = "";
do {
desc = desc + temp;
temp = scanner.next();
} while(!temp.equals("END"))
您可以使用 do-while 进行循环后条件检查
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.