需要验证时间但仍然遇到问题

时间:2017-10-23 06:01:02

标签: java if-statement while-loop boolean

我是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 个答案:

答案 0 :(得分:0)

我会将您的代码重构为以下内容:

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

答案 1 :(得分:0)

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

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