提供不是布尔值的循环条件
Provide loop conditions that are not Boolean
我需要接受一些正整数,我使用 for 循环如下:
Scanner in = new Scanner(System.in);
for(int i=0; i<n; i++) {
num = in.nextInt();
//do something with num
}
这需要我 (a) 事先知道整数的数量 n (b) 使用计数器 i
我知道 Java 不接受循环条件中的非布尔表达式。但是没有 n 和 i 我怎么能做同样的事情呢?
例如,类似于:
while( (num = in.nextInt()) ) {
//do something with num
}
任何类型的循环 (for/while/do-while) 都可以。
你可以做的是:
boolean loop = true;
while (loop) {
int num = in.nextInt();
... do something with n
if (whatever) loop = false;
}
例如。
或者您将 while (true)
与 if (whatever) break
一起使用。
换句话说:您需要一个布尔条件,但您可以控制您的循环体内的条件,如上所示。
这里有一个如何使用扫描仪的例子class:https://www.tutorialspoint.com/java/util/scanner_nextint.htm
您应该使用 hasNext()
方法结束循环并使用 hasNextInt()
方法检查整数:
public class ScannerDemo {
public static void main(String[] args) {
String s = "Hello World! 3 + 3.0 = 6.0 true ";
// create a new scanner with the specified String Object
Scanner scanner = new Scanner(s);
// find the next int token and print it
// loop for the whole scanner
while (scanner.hasNext()) {
// if the next is a int, print found and the int
if (scanner.hasNextInt()) {
System.out.println("Found :" + scanner.nextInt());
}
// if no int is found, print "Not Found:" and the token
System.out.println("Not Found :" + scanner.next());
}
// close the scanner
scanner.close();
}
}
I know Java does not accept non-Boolean expressions in loop conditions.
据我所知,没有任何编程语言允许这样做。循环要么继续,要么不继续,这是一个布尔决定,需要一个布尔条件。没有"the loop maybe continues, we don't know".
话虽如此,Java - 当然 - 需要一个布尔条件来判断是否继续。你需要回答的问题是:循环什么时候结束?
共有三个选项:
循环永远继续
while (true)
循环在特殊输入值处停止
while ((num = in.readInt()) != 0)
循环从外部中断
while (running) {
// ...
}
public void stopLoop() {
running= false;
}
循环直到输入结束-或-非整数输入(例如"exit",空白行):
while(in.hasNextInt()) {
int num = in.nextInt();
}
如果您在 IntelliJ 中进行测试并希望明确指示 EOF:Ctrl+D or ⌘+D
如果你想读取一个文件作为你的输入:java MyClass < numbers.txt
我需要接受一些正整数,我使用 for 循环如下:
Scanner in = new Scanner(System.in);
for(int i=0; i<n; i++) {
num = in.nextInt();
//do something with num
}
这需要我 (a) 事先知道整数的数量 n (b) 使用计数器 i
我知道 Java 不接受循环条件中的非布尔表达式。但是没有 n 和 i 我怎么能做同样的事情呢? 例如,类似于:
while( (num = in.nextInt()) ) {
//do something with num
}
任何类型的循环 (for/while/do-while) 都可以。
你可以做的是:
boolean loop = true;
while (loop) {
int num = in.nextInt();
... do something with n
if (whatever) loop = false;
}
例如。
或者您将 while (true)
与 if (whatever) break
一起使用。
换句话说:您需要一个布尔条件,但您可以控制您的循环体内的条件,如上所示。
这里有一个如何使用扫描仪的例子class:https://www.tutorialspoint.com/java/util/scanner_nextint.htm
您应该使用 hasNext()
方法结束循环并使用 hasNextInt()
方法检查整数:
public class ScannerDemo {
public static void main(String[] args) {
String s = "Hello World! 3 + 3.0 = 6.0 true ";
// create a new scanner with the specified String Object
Scanner scanner = new Scanner(s);
// find the next int token and print it
// loop for the whole scanner
while (scanner.hasNext()) {
// if the next is a int, print found and the int
if (scanner.hasNextInt()) {
System.out.println("Found :" + scanner.nextInt());
}
// if no int is found, print "Not Found:" and the token
System.out.println("Not Found :" + scanner.next());
}
// close the scanner
scanner.close();
}
}
I know Java does not accept non-Boolean expressions in loop conditions.
据我所知,没有任何编程语言允许这样做。循环要么继续,要么不继续,这是一个布尔决定,需要一个布尔条件。没有"the loop maybe continues, we don't know".
话虽如此,Java - 当然 - 需要一个布尔条件来判断是否继续。你需要回答的问题是:循环什么时候结束?
共有三个选项:
循环永远继续
while (true)
循环在特殊输入值处停止
while ((num = in.readInt()) != 0)
循环从外部中断
while (running) {
// ...
}
public void stopLoop() {
running= false;
}
循环直到输入结束-或-非整数输入(例如"exit",空白行):
while(in.hasNextInt()) {
int num = in.nextInt();
}
如果您在 IntelliJ 中进行测试并希望明确指示 EOF:Ctrl+D or ⌘+D
如果你想读取一个文件作为你的输入:java MyClass < numbers.txt