Tag: 循环

为什么Java仅在while循环的情况下识别无法访问的代码?

如果我有类似的代码 public static void main(String args[]){ int x = 0; while (false) { x=3; } //will not compile } 编译器会抱怨x=3是无法访问的代码但是如果我有代码那么 public static void main(String args[]){ int x = 0; if (false) { x=3; } for( int i = 0; i< 0; i++) x = 3; } 然后它正确编译,虽然if statement和for loop的代码是不可达的。 为什么java工作流逻辑没有检测到这种冗余? 任何用例?

如何退出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 Big O表示3嵌套循环的log(n)

对于以下嵌套循环,Big O表示法会是什么? for (int i = n; i > 0; i = i / 2){ for (int j = n; j > 0; j = j / 2){ for (int k = n; k > 0; k = k / 2){ count++; } } } 我的想法是:每个循环都是O(log2(n))所以它就像乘法一样简单 O(log2(n)) * O(log2(n)) * O(log2(n)) = O(log2(n)^3)

3个嵌套循环的大O.

另一个大O符号问题…对于代码的大O是什么: for (int i = n; i > 0; i = i / 2){ for (int j = 0; j < n; j++){ for (int k = 0; k < n; k++){ count++; } } } 我的想法:所以打破它,我认为外部循环是O(log2(n)) ,然后每个内部循环是O(n) ,这将导致O(n^2 * log2(n))问题# 1是正确的吗? 问题2:当组合嵌套循环时,它总是像每个循环的大O一样简单吗?

for循环到while循环

我使用for循环以大写forms打印向后字母表,但我想知道如何使用while循环执行相同的操作,我不是很擅长。 String alphabet = “abcdefghijklmnopqstuvwxyz”; int x = 0; for(x = alphabet.length() – 1; x > -1; x–) { System.out.print(alphabet.charAt(x)); System.out.print(alphabet.toUpperCase()); } 当它到达A时我还需要终止它。认为它是这样的,但我不知道如何让它循环向后。 我真的很感谢你的帮助! while(alphabet.length() < 26) { System.out.print(alphabet.charAt(x)); System.out.print(alphabet.toUpperCase()); if(x == A) { break; } }

使用两个for循环计算字符串中的字母

我必须读取字符串”hello world”并仅使用for循环输出每个字母的频率。 教练暗示我需要使用两个循环并给我们以下代码开始: int ch, count; for (ch =’a’; ch 0 } 编辑:我想我会解决这个问题并发布一年前我找到的解决方案,因为这个问题已经得到了相当多的点击率。 int count; int value; for (int i=65; i<91; i++) { count=0; for (int j=0; j0) System.out.println((char)i+” — “+count); }

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任何区别?

如何使帧显示50次,持续1秒,并在每次显示时消失

我需要我的JFrame在我的屏幕尺寸内显示自己,它应该显示带有消息的框架然后消失1秒然后重新出现50次这里是我的代码到目前为止: import javax.swing.*; import java.awt.*; import java.util.*; public class OurMain { public static void main(String[] args) { Dimension sSize = Toolkit.getDefaultToolkit().getScreenSize(); int w = sSize.width; int h = sSize.height; Random rand = new Random(); int z = rand.nextInt(sSize.width); int c = rand.nextInt(sSize.height); int x = (int)((Math.random()* w) – z); int y = (int)((Math.random()* h) – […]

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

嗨,我一直在尝试过去一小时从这个循环中断,继续,因为已经满足了我的条件一次。 我的应用程序几乎读取了一系列行并分析它然后打印所述的变量。 线条如何显示的示例(不包括。): 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; } }

按Enter键触发System.in.read()两次?

出于某种原因,每当我在循环中使用System.in.read()时按ENTER键,代码会立即执行两次。 如果它不在循环中,它将执行一次! 为什么是这样? 这是一个测试它的代码片段: while(true) { System.in.read(); System.out.println(“I am supposed to print once, but I print twice!”); }