Tag: do while

Java循环效率

我正在比较Java中嵌套for,while和do-while循环的效率,并且我遇到了一些我需要帮助理解的奇怪结果。 public class Loops { public static void main(String[] args) { int L = 100000; // number of iterations per loop // for loop double start = System.currentTimeMillis(); long s1 = 0; for (int i=0; i < L; i++) { for (int j = 0; j < L; j++) { s1 += 1; } } […]

Java SE执行while循环问题

当我编译我的代码时,我的java应用程序有问题,它说找不到符号roomLength 。 应用程序应该做的是提示用户输入房间名称然后如果它“退出”则必须关闭程序。 否则会提示房间的长度,宽度和高度。 如果这些尺寸中的任何一个为零或更小,它将再次重新调整尺寸。 class Room { private String name; private double length; private double width; private double height; public Room(String r, double l, double w, double h) { name = r; length = l; width = w; height = h; } public String getName() { return name; } public double getLength() { return […]

而“!b.equals(x)|| !b.equals(y)“是一个无限循环?

嘿伙计这是我的代码,它正在做的是循环,但它想要做的是如果用户键入借用然后它将询问用户它做了多少但然后他们输入一个数字,它将再次问他们你想借还是卖,它是无限循环的。 case 3: do{ System.out.println(“What would you like to do? Please type borrow to borrow money or sell to sell assets: “); b = scan.nextLine().toLowerCase(); if(b.equals(“borrow”)){ System.out.print(“how much would you like to borrow Remmber if you go over 50000 debt its game over.”); try { input = scan.nextInt(); } catch (Exception e) { System.err.println(“That is […]

Java do-while循环不起作用

我希望我的程序继续问这个问题,直到得到它可以使用的响应,特别是从0到20的数字。我在这个类上有很多其他的东西,所以这里有一个小的摘录,其中do-while是(我已经将变量和所有内容命名为一切。 public static void main(String[] args) { do { halp = 1; System.out.println(“What level is your fort?”); Scanner sc = new Scanner(System.in); try { fortLevel = Integer.parseInt(sc.nextLine()); } catch(NumberFormatException e){System.out.println(“Numbers only, 0-20”); halp = 0; } if(halp 1) { work = true; } while(work = false); }

do-while循环的目的是什么?

我知道它做了什么,以及它如何与while循环合作,但是不会有一个while循环代码是相同的,无论是否存在?

Java做while循环使字符串测试行为不同

我对使用||的行为感到非常困惑 .equals函数的运算符。 有没有理由我不能在字符串或其他东西上使用它? 这工作: do{ System.out.println(“Play again? [Y/N]”); //input: Y play = in.nextLine(); play = play.toUpperCase(); } while(!”Y”.equals(input) ); //breaks out of loop (as it should) 为什么这不起作用?! do{ System.out.println(“Play again? [Y/N]”); //input: Y play = in.nextLine(); play = play.toUpperCase(); } while( !”Y”.equals(input) || !”N”.equals(input) ); //infinite loop

扫描仪输入接受字符串跳过while循环内的所有其他输入。

好吧,所以代码很简单。 通用类ourSet,它接受一些元素,将它放在LinkedList中,并在两个集合上执行一些函数。 我的问题实际上与项目的一般概念完全无关,它更多地出现在我创建的“用户输入界面”中。 我希望它接受一些字符串并将其添加到集合中,然后在接收字符串“EXIT”(全部大写)时退出循环,并为下一个集合执行相同操作。 发生的事情是do while循环只发送所有奇数的第1,第3,第5 …… package set.pkgclass; import java.util.Scanner; import java.util.LinkedList; public class SetClass { public static void main(String[] args) { ourSet set1 = new ourSet(); ourSet set2 = new ourSet(); Scanner input = new Scanner(System.in); System.out.println(“Please enter a string to put in set 1, ” + “type EXIT (in all caps) to […]