【问题标题】:need to validate the times but keep running into issues需要验证时间,但不断遇到问题
【发布时间】:2018-04-03 14:48:48
【问题描述】:

我是 Java 新手,我正在尝试弄清楚如何让用户输入的时间得到验证。如果用户输入低于 800 的数字或高于 1900 的数字,他们应该会收到正确的错误消息。我还希望用户在输入 860 等时间时收到错误消息……我已经在这部分纠结了太久,我肯定需要帮助。

import java.util.Scanner;


public class FugonIsaac06 {

 public static void main(String[] args) {


  boolean keepAsking = true;


  Scanner reader = new Scanner(System.in);


  String userInput = "";
  int input_Start = 0;
  int input_End = 0;



  while (keepAsking) {

   System.out.print("Please enter your name: ");
   userInput = reader.nextLine();
   if (userInput.length() >= 3) {

    keepAsking = false;

   } else {
    System.out.println("Name must be at least 3 characters long.");

   }

  }
  keepAsking = true;
  //You will need to somehow separate the hours and minutes from the input.
  //Use integer division and modulus to separate hours and minutes.
  //Hours = Input / 100
  //Minutes = Input % 100
  //To convert the minutes to parts of an hour divide the minutes by 60.0.
  //Parts of an hour = Minutes / 60.0
  int hours_Start = input_Start / 100;
  int minutes_Start = input_Start % 100;
  while (keepAsking) {
   System.out.print("Enter start time: ");
   input_Start = reader.nextInt();
   if ((input_Start > hours_Start) &&
    (input_Start < minutes_Start)) {
    keepAsking = true;



    System.out.println("Start time should be between 800 and 1900");


   } else {


    System.out.println("Time is malformed, minutes should be between 0 and 59");
   }

  }
 }
}

【问题讨论】:

    标签: java if-statement while-loop boolean


    【解决方案1】:

    我会将你的代码重构为这样的:

    Scanner reader = new Scanner(System.in);
    String username = "";
    
    do {
        username = reader.nextLine();
    } while(username.length() < 3);
    
    int timeStart;
    String timestamp = "";
    
    // enter a timestamp in the form of hoursminutes
    // hours = 0 (or 00) to 23
    // minutes = 0 (or 00) to 59
    do {
        timestamp = reader.nextLine();
    } while(!timestamp.matches("(?:2[0-3]|[0-1][0-9])[0-5][0-9]"));
    
    // parse out the hours and minutes components
    int hours = Integer.parseInt(timeStart) / 100;
    int minutes = Integer.parseInt(timeStart) % 100;
    

    使用do 循环非常适合您的用例,因为您希望急切地接受初始用户输入,而不管之前的状态如何,但您希望能够在验证失败时再次循环。 while 条件验证输入,检查用户名是否包含 3 个或更多字符,并且时间开始在您指定的范围内。

    【讨论】:

    • 如果用户输入863这样的时间呢?我是否使用另一个 do 循环?也感谢您的帮助。
    【解决方案2】:

    我已经编辑了你的代码试试看

    import java.util.Scanner;
    
    
    public class Demo {
    
     public static void main(String[] args) {
      boolean keepAsking = true;
      Scanner reader = new Scanner(System.in);
      String userInput = "";
      int input_hours =0;
      int input_End = 0;
      String time;
    
      while (keepAsking) {
       System.out.print("Please enter your name: ");
       userInput = reader.nextLine();
       if (userInput.length() >= 3) {
        break;
       // keepAsking = false;
       } else {
        System.out.println("Name must be at least 3 characters long.");
       }
      }
      keepAsking = true;
    
    
      //You will need to somehow separate the hours and minutes from the input.
      //Use integer division and modulus to separate hours and minutes.
      //Hours = Input / 100
      //Minutes = Input % 100
      //To convert the minutes to parts of an hour divide the minutes by 60.0.
      //Parts of an hour = Minutes / 60.0
      int hours_Start = 0;
      int minutes_Start = 0;
      while (keepAsking) {
       System.out.print("Enter start time HH:MM : ");   
       time = reader.next();   
       String []timeArr = time.split(":");   
        input_hours =   Integer.parseInt(timeArr[0]);   
        minutes_Start =  Integer.parseInt(timeArr[1]);     
    
       if (input_hours > 19 || input_hours <  8) {
        keepAsking = true;
        System.out.println("Start time should be between 800 and 1900");
       } else if(minutes_Start<0 || minutes_Start>59){
    
    
        System.out.println("Time is malformed, minutes should be between 0 and 59");
       }else{
           System.out.println("You have entered correct time");
        break;   
        }
    
      }
     }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-31
      • 2015-12-26
      相关资源
      最近更新 更多