简体   繁体   English

在Java中循环

[英]Looping while in Java

Consider my codes are below : 考虑我的代码如下:

int number = 0;
while ((number >=0.5) && (number <= 27.5)) {
    if (number % 1.25 == 0) {
        number +=0.75;
    } else {
        number++;
    }
}
}

my question is what is the problem in the codes above? 我的问题是上面的代码有什么问题? and if i want to print out the result, the variable number should has the following number of 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 during the process of looping. 如果要打印结果,则在循环过程中,变量号应具有以下数字:6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28。 how do i do that? 我怎么做?

It will never enter the loop because 0 is not >= 0.5 它永远不会进入循环,因为0不是> = 0.5

It's not at all clear why you are dealing with quantities such as 0.5, 1.25, 27.5, what exactly are you trying to accomplish? 目前还不清楚为什么要处理诸如0.5、1.25、27.5之类的数量,您到底想达到什么目的? To print the numbers 6 to 28 doesn't need any of those fractional values. 要打印数字6到28,不需要任何这些小数值。

You need to include the purpose of your code for others to figure out what is the problem with it. 您需要包括代码的目的,以供其他人找出问题所在。

The issues I see are: 我看到的问题是:

Your while loop would never start because the initial assignment for "number" is out of range. 您的while循环永远不会开始,因为“数字”的初始分配超出范围。

Also, I see you increment "number" by 1, so your "% 1.25" will never execute. 另外,我看到您将“数字”增加了1,因此“%1.25”将永远不会执行。

If you just need to loop from 6 to 28 using the code similar to yours, should be something like this: 如果您只需要使用类似于您的代码从6到28循环,则应如下所示:

int number = 6;
while (number < 29) {
  System.out.println(number);
  number++;
}

Try something like ... 尝试类似...

int number = 5;
while ((number >=5) && (number <= 27.5)) {
  System.out.println(""+number++);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM