2017-10-23 14 views
0

私は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"); 
    } 

    } 
} 
} 

答えて

0

私はこのような何かにあなたのコードをリファクタリングします:あなたが熱心に最初のユーザーの入力を受け付けるようにしたいのでdoループを使用して

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; 

は、以前の状態に関係なく、あなたのユースケースに適していますまだ検証が失敗した場合には、ループバックする機能が必要です。 whileの条件では、ユーザー名が3文字以上で、開始時刻が指定した範囲内にあることを確認して入力を検証します。私はあなたのコードを編集した

+0

それを試してみてください?別のdoループを使用しますか?また、助けてくれてありがとう。 – ike626

+0

@ ike626私は自分の答えを更新しました。 –

0

は、ユーザーが863のような時間を入力した場合については何

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

関連する問題