Java 无限时循环
出于某种原因,我的循环永远不会结束,尽管用户特别指定了扫描器读取的值。当用户键入0时,它将继续Java 无限时循环,java,loops,do-while,Java,Loops,Do While,出于某种原因,我的循环永远不会结束,尽管用户特别指定了扫描器读取的值。当用户键入0时,它将继续 public static void InseretToDB() throws IOException { IO init = new IO(); Scanner scan = init.getScanner(); BufferedWriter bWriter = init.getWriter(); int j; do { System.out
public static void InseretToDB() throws IOException {
IO init = new IO();
Scanner scan = init.getScanner();
BufferedWriter bWriter = init.getWriter();
int j;
do {
System.out.println("Name");
String name = scan.nextLine();
bWriter.write(name);
bWriter.write(" ");
System.out.println("Ocuppation");
String occupation = scan.nextLine();
bWriter.write(occupation);
bWriter.newLine();
System.out.println(" Input 1 to continue, 0 to end");
j = (int) Integer.parseInt(scan.nextLine());
} while (j != 0);
}
当我键入0时,循环确实结束了,但我将程序更改为以下内容
Scanner scan = new Scanner(System.in);
BufferedWriter bWriter = new BufferedWriter(new OutputStreamWriter(System.out));
希望这有帮助,将扫描仪初始化为
Scanner scan = new Scanner(System.in);
从中替换当前代码
j = (int) Integer.parseInt(scan.nextLine());
到
当j=0时,循环将结束…您在哪里调用InseretToDB方法?在主类的switch语句中使用scanner而不定义字符集,由于某种原因工作。那是一些严重的恶作剧!请注意:以后输入数字时不要更改字符集
try{
j = scan.nextInt();
}catch(Exception e){
j = -1;
}