【问题标题】:Complex conditional expressions with user EasyIn.使用用户 EasyIn 的复杂条件表达式。
【发布时间】:2013-10-20 20:39:50
【问题描述】:

我编写了以下 Java 代码,想问如果用户输入 distance =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:");
    }   
}
}

【问题讨论】:

    标签: java boolean conditional user-input


    【解决方案1】:

    这是我对这个问题的建议。创建一个名为 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:");
                }  
            }    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-17
      • 2015-09-20
      • 1970-01-01
      • 1970-01-01
      • 2013-03-08
      • 2017-08-16
      相关资源
      最近更新 更多