2016-11-06 6 views
-1

なぜ私の最初のwhileループはユーザーからの入力の有効性をチェックしますが、他の2つのwhileループは月と年の間にスキップされます(無視されます)。ユーザーに日付を正しく入力させ、不可能な値を入力させないようにしようとしています。あなたの最初のループ条件以来私のプログラムで3つのwhileループのうち2つがスキップされているのはなぜですか?

// A read() method to read in the account details from the user 
boolean success = false; 
public void read() 
{ 
    Scanner keyboardIn = new Scanner(System.in);   
    System.out.println("ENTER ACCOUNT DETAILS: "); 
    System.out.print("User Title: "); 
    String title = keyboardIn.nextLine(); 
    name.setTitle(title); 
    System.out.print("User First name: "); 
    String firstname = keyboardIn.nextLine(); 
    name.setFirstName(firstname); 
    System.out.print("User Second name: "); 
    String secondname = keyboardIn.nextLine(); 
    name.setSurname(secondname); 
    System.out.print("Account Address: "); 
    address = keyboardIn.nextLine(); 
    // To make sure day is entered correctly (1 - 31) 
    while(!success) 
    { 
     try 
     { 
      System.out.print("Enter the day the account opened: "); 
      int d = keyboardIn.nextInt(); 
      dateOpened.setDay(d); 
      success = true; 
     }catch(Exception e) 
     { 
      System.out.println(e.getMessage()); 
     } 
    } 

    // To make sure month is entered correctly (1 - 12) 
    while(!success) 
    { 
     try 
     { 
      System.out.print("Enter the month the account opened: "); 
      int m = keyboardIn.nextInt(); 
      dateOpened.setMonth(m); 
      success = true; 
     }catch(Exception e) 
     { 
      System.out.println(e.getMessage()); 
     } 
    } 

    // To make sure year is entered correctly (< 1900 not permitted) 
    while(!success) 
    { 
     try 
     { 
      System.out.print("Enter the year the account opened: "); 
      int y = keyboardIn.nextInt(); 
      dateOpened.setYear(y); 
      success = true; 
     }catch(Exception e) 
     { 
      System.out.println(e.getMessage()); 
     } 
    } 

    System.out.print("Enter the initial balance: "); 
    balance = keyboardIn.nextDouble(); 
    System.out.print("Enter the overdraft amount: "); 
    overdraftAmount = keyboardIn.nextDouble(); 
    System.out.println("Account number: " + accountNo); 
    System.out.println(); 
} 

答えて

1

ループが唯一の成功がtrueの場合、成功はループしながら、他の二つのためにtrueになり、彼らが実行されることはありません終了している間。

"成功"をループ条件として使用することはお勧めしません。あなたのような何かを行うことができます

System.out.print("Enter the day the account opened: "); 
int d = keyboardIn.nextInt(); 

while(d < 1 || d>31){ 

System.out.print("Enter the day the account opened: "); 
d = keyboardIn.nextInt(); 

} 

//set d as day in object 
+0

//これは私の出力です。何らかの理由でDateフィールドが0/0/0になります。検証は今でもうまくいきます。私はあなたの提案が何であるかに変更しました。 – Daniel

+0

名前:ミスタージョーBloggs 住所:1つのメインストリート 日付が開か:0/0/0 バランス:利用可能€100.0 当座貸越:€200.0 – Daniel

+0

は、あなたが質問 – Solace

0

それは可能性が2つの理由のために、最初のものは、ユーザが入力したデータが、その可能性、InputMismatchException例外またはdateOpened.setDay(d)が失敗したとのラインでのスロー整数、ではないということですdateOpenedヌルあるかどうなる方法setDayが失敗した場合にどのしませんから成功変数セット。

0

最初のwhileループの後、ループ条件 "success"をリセットしないでください。これは単に一方小さい方法でそれを実現し、その方法にローカル変数の条件があまりにもそれを解決する製造上の各ループ

後にそれをリセットすることで解決する

一つの方法。

public void read() 
{ 
    Scanner keyboardIn = new Scanner(System.in);   
    System.out.println("ENTER ACCOUNT DETAILS: "); 
    System.out.print("User Title: "); 
    String title = keyboardIn.nextLine(); 
    name.setTitle(title); 
    System.out.print("User First name: "); 
    String firstname = keyboardIn.nextLine(); 
    name.setFirstName(firstname); 
    System.out.print("User Second name: "); 
    String secondname = keyboardIn.nextLine(); 
    name.setSurname(secondname); 
    System.out.print("Account Address: "); 
    address = keyboardIn.nextLine(); 
    // To make sure day is entered correctly (1 - 31) 
    readDay(keyboardIn); 

    // To make sure month is entered correctly (1 - 12) 
    readMonth(keyboardIn); 

    // To make sure year is entered correctly (< 1900 not permitted) 
    readYear(keyboardIn); 

    System.out.print("Enter the initial balance: "); 
    balance = keyboardIn.nextDouble(); 
    System.out.print("Enter the overdraft amount: "); 
    overdraftAmount = keyboardIn.nextDouble(); 
    System.out.println("Account number: " + accountNo); 
    System.out.println(); 

} 

private void readDay(Scanner keyboardIn){ 
    boolean success = false; 
    while(!success) 
    { 
     try 
     { 
      System.out.print("Enter the day the account opened: "); 
      int d = keyboardIn.nextInt(); 
      dateOpened.setDay(d); 
      success = true; 
     }catch(Exception e) 
     { 
      System.out.println(e.getMessage()); 
      keyboardIn.next(); 
     } 
    } 
} 
private void readMonth(Scanner keyboardIn){ 
    boolean success = false; 
    while(!success) 
    { 
     try 
     { 
      System.out.print("Enter the month the account opened: "); 
      int m = keyboardIn.nextInt(); 
      dateOpened.setMonth(m); 
      success = true; 
     }catch(Exception e) 
     { 
      System.out.println(e.getMessage()); 
      keyboardIn.next(); 
     } 
    } 
} 
private void readMonth(Scanner keyboardIn){ 
    boolean success = false; 
    while(!success) 
    { 
     try 
     { 
      System.out.print("Enter the year the account opened: "); 
      int y = keyboardIn.nextInt(); 
      dateOpened.setYear(y); 
      success = true; 
     }catch(Exception e) 
     { 
      System.out.println(e.getMessage()); 
      keyboardIn.next(); 
     } 
    } 
} 
+0

[OK]を、これは私のために完璧に動作中のソースコードを更新してください可能性をありがとうございます。誰かが6.5または12.5のような整数でない値を入力したり、A-Zのような文字を入力したりするとどうなりますか?どのように私はそれを行うからユーザーを戦うことができます。 – Daniel

+0

手紙を入力するたびに私のコードがクラッシュするので、私は文字列の入力を拒否する問題を助けてもらえますか? – Daniel

+0

[リンク](http://stackoverflow.com/a/3572233/1964912)に記載されているように、keyboardIn.next()を呼び出す必要があります。 catch節でスキャナが保持する値を消費する – matte

関連する問題