有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java需要验证时间,但会不断遇到问题

我是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");
   }

  }
 }
}

共 (2) 个答案

  1. # 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个或更多字符,以及开始时间是否在指定的范围内

  2. # 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;   
        }
    
      }
     }
    }
    

    enter image description here