Code:
import java.util.Scanner;
public class SchleifenUebung {
public void zeilenAusgeben(int Zeilenanzahl) {
int index = 1;
while (Zeilenanzahl >= 0) {
System.out.println("Zeile z" + index);
index++;
}
}
public void ausgabeQuadratZahlen() {
System.out.println("Eingabe Grenze:");
Scanner scanner = new Scanner(System.in);
String line;
int n;
int index = 1;
do {
System.out.println("Eingabe Grenze:");
line = scanner.nextLine();
n = Integer.parseInt(line);
index++;
} while (n >= 1 && n <= 100);
while (index <= n) {
System.out.println(n + "^2=" + index * index);
index++;
}
}
}
Exception:
java.util.NoSuchElementException: No line found at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
Apparently you try to scan input until a number between 1 and 100 is entered, but with while(n >=1 && n <=100)
you do the opposite, and if no number outside that range appears before the end of the input stream, the shown exception arises. Change to while (n < 1 || 100 < n)
.
您可以使用,Scanner 正在证明可以将 Inputs 直接转换为 int 的方法,它可以从您身边进行解析,您不必手动进行
scanner.nextInt();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.