Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.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 复杂的条件表达式,用户容易理解。_Java_Boolean_Conditional_User Input - Fatal编程技术网

Java 复杂的条件表达式,用户容易理解。

Java 复杂的条件表达式,用户容易理解。,java,boolean,conditional,user-input,Java,Boolean,Conditional,User Input,我编写了下面的Java代码,想问一下,如果用户输入的距离小于0,我如何继续向用户询问正确的距离?我是否每次都必须创建一个新变量,或者仅当距离>=0时才可以循环整个过程和getInt?非常感谢 public class W05Practical { public static void main(String [] args) { System.out.println("Please enter the lengths/distance in meters:");

我编写了下面的Java代码,想问一下,如果用户输入的距离小于0,我如何继续向用户询问正确的距离?我是否每次都必须创建一个新变量,或者仅当距离>=0时才可以循环整个过程和getInt?非常感谢

public  class   W05Practical {
public  static  void    main(String []  args)   {
    System.out.println("Please enter the lengths/distance in meters:");
    int Distance = EasyIn.getInt();

    if (Distance >= 0) {
System.out.println("Thank you");
    }
    else { 
System.out.println("Distance can not be negative. \nPlease enter the appropriate distance:");
    }   
}
}

这是我对这个问题的建议。创建一个名为correctInput的布尔值,并让它监视输入是否正确

例如:

public  class   W05Practical {
    public  static  void   main(String[] args){
        boolean correctInput = false;

        while(!correctInput){
            System.out.println("Please enter the lengths/distance in meters:");
            int Distance = EasyIn.getInt();

            if (Distance >= 0) {
                System.out.println("Thank you");
                correctInput = true;
            }else { 
                System.out.println("Distance can not be negative. \nPlease enter the appropriate distance:");
            }  
        }    
    }
}