Tag: while loop

Java,需要一个while循环来实现eof。 iewhile!eof,继续解析

我目前有一个有效的解析器。 它解析一次文件(不是我想要它做的),然后将解析后的数据输出到文件中。 我需要它继续解析并附加到相同的输出文件,直到输入文件的末尾。 看起来像这样。 try { // my code parsing the data and appending to eof of output. (works) } catch (EOFException eof){ } 除了while循环,一切都完成了。 它只在需要它时才解析一次以保持解析。 我正在寻找一个while循环函数来达到eof。 我也在使用DataInputStream。 是否有某种DataInputStream.hasNext函数? DataInputStream dis = new DataInputStream(new FileInputStream(inFile)); ie dis.read(); 。 //Need a while !eof while loop try { // my code parsing the data and appending to […]

使用扫描仪时无限循环?

boolean z = false; do { try { a = sc.nextInt(); z = true; } catch(Exception e) { } } while(!z); 尝试这个。 如果在第一次正确执行时尝试整数。 但是,如果输入错误的文本类型,即使您输入int next并跳过将布尔值赋值为true,它也会变为无限循环。 为什么是这样?

在Java while循环条件中分配变量?

我有一个方法,它做了很多检查和计算,并返回一个自定义类,returnMessage。 returnMessage有2个布尔值和一个String。 我想要做的是在我的主类的while循环中运行此方法,并在while循环最后一次终止后访问returnMessage对象。 在PHP中,这将是一个案例 while ( $returned = myObject->myMethod()->finished ) { } if( $returned -> finished == FALSE) { … } 但是尝试像这样分配给我一个布尔预期的错误java(上面可能有php错误,它已经很晚了:D)

如何在一定时间后关闭一个阶段JavaFX

我目前正在使用两个控制器类。 在Controller1中,它创建一个在主要阶段之上打开的新阶段。 Stage stage = new Stage(); Parent root = FXMLLoader.load(getClass().getResource(“Controller2.fxml”)); Scene scene = new Scene(root); stage.setScene(scene); stage.show(); 现在一旦这个阶段打开,我希望它在关闭之前保持打开约5秒钟。 在Controller2中,我尝试过实现类似的东西 long mTime = System.currentTimeMillis(); long end = mTime + 5000; // 5 seconds while (System.currentTimeMillis() > end) { //close this stage } 但是我不知道在while循环中放入什么来关闭它。 我已经尝试过所有种类,但没有任何作用。

do-while with Java8-Optional

在我的一些项目中,我经常使用do-while-checkNextForNull-getNext循环模式(不知道它是否有正式名称)。 但是在Java8中,使用Optional比在客户端代码中检查空引用更清晰。 但是当在这种循环模式中使用Optional时,代码变得有点冗长和丑陋,但因为Optional有一些方便的方法,我希望必须存在比我在下面提出的更简洁的方法。 例: 鉴于以下课程。 class Item { int nr; Item(nr) { this.nr = nr; // an expensive operation } Item next() { return …someCondition…. ? new Item(nr + 1) : null; } } 其中第一个项目始终具有nr == 1并且每个项目确定下一个项目,并且您不希望创建不必要的新项目。 我可以在客户端代码中使用以下循环do-while-checkNextForNull-getNext模式: Item item = new Item(1); do { // do something with the item …. } while ((item […]

两个while循环中的break语句

假设我有这个: while(a){ while(b){ if(b == 10) break; } } 问题: break语句是否会将我从两个循环中取出或仅从内循环中取出? 谢谢。

使用Scanner读取输入会导致Java中的无限循环

在我的程序中,我试图让用户在1-3之间输入一个int,然后根据他们键入的内容做一些事情。 如果它不是数字或不是其中一个选项,那么它将允许它们重新输入有效选项。 我遇到的问题是我无法集思广益,如何不让它无限循环,只是让它们在控制台告诉他们输入无效输入后输入一个数字。 int i = 0; while (i < 1) { try { int level = scan.nextInt(); i+=1; if (level == 1) { System.out.println("You selected level 1!"); //Start the game } else if (level == 2) { System.out.println("You selected level 2!"); //Start the game } else if (level == 3) { System.out.println("You selected level […]

在“while循环”中使用“try and catch”块时无限循环

当我在while循环中使用try和catch块时,我的程序有一个无限循环 。 import java.util.*; class Try { public static void main(String args[]) { Scanner sc=new Scanner(System.in); while(true) { try { System.out.println(“Enter a no “); int s=sc.nextInt(); } catch(Exception e) { System.out.println(“Invalid input try again”); } } } } 当我输入一个整数时,它运行正常并要求另一个输入,但是当我输入一个char时,它会进行无限循环。 为什么会这样?

Java InputMismatchException

我有这个代码,我想捕获字母exception,但它一直有这些错误: Exception in thread “main” java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:840) at java.util.Scanner.next(Scanner.java:1461) at java.util.Scanner.nextInt(Scanner.java:2091) at java.util.Scanner.nextInt(Scanner.java:2050) at exercise_one.Exercise.main(Exercise.java:17) 这是我的代码: System.out.print(“Enter the number of students: “); students = input.nextInt(); while (students <= 0) { try { System.out.print("Enter the number of students: "); students = input.nextInt(); } catch (InputMismatchException e) { System.out.print("Enter the number of students"); } }

同时,其他环

当然这在java(迄今为止)中是一个不可能的陈述,但理想情况下我想实现它,因为它是许多迭代的核心。 例如,第一次多次调用它在创建ArrayList时我正在做650,000次。 不幸的是,现实是我的实际代码没有在else循环中set ; 因此它会传递add和set命令并浪费时间。 之后我还在另一个循环中,它只执行集合,因为数据已经创建,并且这是多嵌套的,因此它是一个漫长的过程。 ArrayList dataColLinker = new java.util.ArrayList(); … … public void setLinkerAt( int value, int rowIndex) { … while(rowIndex >= dataColLinker.size()) { dataColLinker.add(value); } else { dataColLinker.set(rowIndex, value); } 任何想法或理论? 在if语句和ArrayList命令等方面,我不确定java的速度