【发布时间】: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