Tag: while循环

如何预先计算有效的组合数而不是使用while循环?

给定dc1,dc2,dc3和机器列表的数据中心列表,如下所述h1,h2,h3,h4 – Datacenters = dc1, dc2, dc3 Machines = h1, h2, h3, h4 我想仅生成以下组合 – a) {dc1=h1, dc3=h3, dc2=h2} b) {dc1=h2, dc3=h4, dc2=h3} c) {dc1=h3, dc3=h1, dc2=h4} d) {dc1=h4, dc3=h2, dc2=h1} 每个传递中的每个数据中心都应该获得备用机器/主机。 他们不应该得到相同的机器。 例如,如上所示 – dc1 gets h1 , dc2 gets h2 , dc3 gets h3因此每个数据中心的所有机器都不同。 并且在第二次通过中,如b所示 – 现在dc1 gets h2 (因为第一次通过时dc1已经得到h1), dc2 got h3 […]

while循环在计算之前由于数据类型而退出

我的程序规格如下。 1.所有四位数字都不同2.千位数字是十位数字的三倍3.数字是奇数。 数字的总和是27.我遗漏了整个程序的一些代码。 它有一个干净的编译,但当它运行时它会自动终止。 我认为问题出在数据类型转换的某个地方。 int randomNumber = rand.nextInt(9000) + 1000; String randomString; boolean found = true; while (found) { // converting to string to find position of digits and value randomString = String.valueOf(randomNumber); // converting to char to transfer back to in while knowing the position of the digits char position0a = randomString.charAt(0); char […]

初学者Java(循环) – 缺少返回语句

import java.util.Scanner; public class Vending { public double vend(double balance) { Scanner in = new Scanner(System.in); balance = 0; System.out.print(“Enter a command = “); String command = in.nextLine(); while (in.hasNext()) { if (command.equals(“penny”)) { balance = balance + 0.01; System.out.println(“balance = ” + balance); } return balance; } } } 嗨! 我已经尝试了一切来弄清楚为什么不能识别return语句。 如果我把“返回余额”放在其他任何地方,它说system.out.println是无法访问的……你们中的任何人都可以帮助我,为什么这可能不起作用? 谢谢!!

如何退出Java循环? 在一个基本的猜谜游戏中循环

我正在尝试编写一个小游戏,但是如果他们想再次玩游戏那么如何提醒用户以及如果他们不想再玩的话如何退出循环… import java.util.Random; import java.util.Scanner; public class Guessinggame { public static void main(String[] args) { System.out.println(“Welcome to guessing game! \n” + ” You must guess a number between 1 and 100. “); while (true) { Random randomNumber = new Random(); Scanner g = new Scanner(System.in); int number = randomNumber.nextInt(100) + 1; int guess = 0; […]

比较java中的两个文件

我试图比较两个.txt文件(即它们的内容),但是当我执行这个代码时,我的应用程序进入无限循环。 为什么? public int compareFile(String fILE_ONE2, String fILE_TWO2)throws Exception { File f1 = new File(fILE_ONE2); //OUTFILE File f2 = new File(fILE_TWO2); //INPUT FileReader fR1 = new FileReader(f1); FileReader fR2 = new FileReader(f2); BufferedReader reader1 = new BufferedReader(fR1); BufferedReader reader2 = new BufferedReader(fR2); String line1 = null; String line2 = null; int flag=1; while ((flag==1) &&((line1 […]

Java上的奇怪循环为0.1到2

我试图从0.1循环到2.0,然后将输出打印到控制台..但我有这样的奇怪输出: 0.1 0.2 0.30000000000000004 0.4 0.5 0.6 0.7 0.7999999999999999 0.8999999999999999 0.9999999999999999 1.0999999999999999 1.2 1.3 1.4000000000000001 1.5000000000000002 1.6000000000000003 1.7000000000000004 1.8000000000000005 1.9000000000000006 2.0000000000000004 源代码: public class test { public static void main(String[] a) { double i = 0.1; while (i < 2.1) System.out.println(i); i+=0.1; } } } 为什么不打印确切的数字而不是像0.79999999999这样的点? 因为我不知道怎么做0.1增量,所以使用for而不是while任何区别?

如何在满足条件后从循环中断开

嗨,我一直在尝试过去一小时从这个循环中断,继续,因为已经满足了我的条件一次。 我的应用程序几乎读取了一系列行并分析它然后打印所述的变量。 线条如何显示的示例(不包括。): 10 c = 9 + 3 20 a = c + 1 30打印c 40转到20 50结束 它做的一切正确,当它到达第40行时按预期进入第20行,但我希望它转到第50行,因为已经进入第40行一次。 这是我的这部分代码: while(booleanValue) { if(aString.substring(0, 4).equals(“goto”)) { int chosenLine = Integer.parseInt(b.substring(5)); if(inTheVector.contains(chosenLine)) { analizeCommands(inTheVector.indexOf(chosenLine)); i++; } else { System.ou.println(“Line Was Not Found”); i++; } } else if(aString.substring(0, 3).equals(“end”)) { System.out.println(“Application Ended”); booleanValue = false; } }

查找给定整数的因子

我有这样的事情: int f = 120; for(int ff = 1; ff <= f; ff++){ while (f % ff != 0){ } 我的循环找到因素有什么问题吗? 我对for和while语句的工作方式感到困惑,所以很可能它们完全错了。 在此之后,我将如何为所述因素分配变量?

如何将此for循环转换为while循环?

我正在创建一个数组程序。 我正在练习如何将for循环转换for while循环,但我无法理解这个概念。 如果我有for循环: int [] list = new int [5]; for (int i = 0; i < 5; i++) { list [i] = i + 2; } 我怎么能让它成为一个循环? 这是我的尝试 int [] list = new int [5]; int i = 0; while (i<5) { list [i] = i + 2; i++; } System.out.print(list[i] + " […]

Java while循环和线程!

我有一个程序,不断轮询数据库以获取某些字段值的变化。 它在后台运行,目前使用while(true)和sleep()方法来设置间隔。 我想知道这是一个好习惯吗? 而且,实现这一点可能是一种更有效的方法? 该计划旨在始终运行。 因此,停止程序的唯一方法是对进程ID发出kill。 该程序可能处于JDBC调用的中间。 我怎么能更优雅地终止它呢? 我知道最好的选择是通过使用将由线程定期检查的标志来设计某种退出策略。 但是,我无法想到改变这个标志值的方式/条件。 有任何想法吗?