Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 命名布尔值在while语句中不起作用_Java_While Loop_Boolean - Fatal编程技术网

Java 命名布尔值在while语句中不起作用

Java 命名布尔值在while语句中不起作用,java,while-loop,boolean,Java,While Loop,Boolean,在试图通过命名布尔值使代码更易于阅读时,我遇到了一个问题。它没有像预期的那样正确执行,我无法找到它背后的原因。以下代码正确执行 import java.util.Scanner; public class While { public static void main (String [] args){ int count,number; System.out.println("Please enter the number you want to count

在试图通过命名布尔值使代码更易于阅读时,我遇到了一个问题。它没有像预期的那样正确执行,我无法找到它背后的原因。以下代码正确执行

import java.util.Scanner;

public class While {
    public static void main (String [] args){
        int count,number;

    System.out.println("Please enter the number you want to count upto:");

    Scanner userInput = new Scanner(System.in);
    number= userInput.nextInt();
    count = 1;

    while(count<=number)
    {
        System.out.println(count +" ,");

        count++;
    }
}
}
import java.util.Scanner;
公共课{
公共静态void main(字符串[]args){
整数计数,数字;
System.out.println(“请输入要计数的数字:”;
扫描仪用户输入=新扫描仪(System.in);
number=userInput.nextInt();
计数=1;
while(count
boolean only当=(count当您分配:

boolean onlyWhen = (count<=number);
请注意,这不是很好的代码,我可能更喜欢您最初使用的代码:

while (count <= number) {
    System.out.println(count +" ,");
    count++;
}

while(count)提取变量“
only when
”表示while循环“仅在”为真时执行。这是while循环中条件的含义。如果您不理解while循环的含义,那么变量名对您没有帮助。
boolean onlyWhen = (count<=number);
boolean onlyWhen = (count<=number);

while(onlyWhen)
{
    System.out.println(count +" ,");

    count++;
    onlyWhen = (count<=number);
}
boolean onlyWhen = (count<=number);
while (onlyWhen) {
    System.out.println(count +" ,");
    count++;
    onlyWhen = (count<=number);
}
while (count <= number) {
    System.out.println(count +" ,");
    count++;
}