2012-08-08 80 views
3

我有這個作業的問題。該程序應首先運行registration()。如果用戶年齡在13歲以下,它會發出警報,並應該返回registration()。如果用戶超過13歲,它將運行displayProfile()並要求確認,如果答案是肯定的,它將通知它是成功的並且程序結束。如果答案是否定的,則應回到registration()並重復處理。如果答案無效,它應該繼續提示有效答覆。 問題出在registration()。在程序開始時,它工作正常,但是當我輸入一個13歲以下的用戶並返回到註冊時,它跳過姓的輸入部分,而是繼續輸出「First Name:」 。它在一個do while循環中,因爲我想驗證輸入是否只有字符和空格。對於註冊中的其他領域,一切正常。它只是在下一次運行時提示姓,它不起作用。算法有什麼問題?在java中跳過掃描器輸入內部do while循環

import java.util.Calendar; 
import java.util.Scanner; 



public class mainExercise{ 
static Scanner input = new Scanner(System.in); 
static String lastName; 
static String firstName; 
static String email; 
static String gender; 
static String bday; 
static int birthMonth; 
static int birthDay; 
static int birthYear; 
static String confirmation; 

static EmailValidator ev = new EmailValidator(); 
static Calendar cal = Calendar.getInstance(); 

public static void main(String[] args){ 

    do{ 

     do{ 
      registration(); 
     }while(validateAge(birthMonth, birthDay, birthYear) != 1); 

     displayProfile(); 

     do{ 
      confirmation = input.nextLine(); 
      }while(confirm(confirmation)== -1); 

     if(confirm(confirmation)==0){ 
      break; 
     } 

    }while(confirm(confirmation)== 1); 

    System.out.println("Thank you for registering, " + firstName); 

}//end of main 


static void registration(){ 

    System.out.println("Welcome to Old School Facebook"); 
    System.out.println("To register, please provide the following  information");    

    System.out.print("Last Name: "); 
    do{ 
     lastName = input.nextLine(); 
     }while(validateName(lastName) != 1); 

    System.out.print("First Name: "); 
    do{ 
     firstName = input.nextLine(); 
     }while(validateName(firstName) != 1); 

    System.out.print("Email: "); 
    do{ 
     email = input.nextLine(); 
     }while(validateEmail(email) != 1); 

    System.out.print("Gender: "); 
    do{ 
     gender = input.nextLine(); 
     }while(validateGender(gender) != 1); 

    System.out.println("Enter birthdate"); 
    do{ 
     System.out.print("Month: "); 
     birthMonth = input.nextInt(); 
     }while(validateBirthInput(birthMonth) != 1); 

    do{ 
     System.out.print("Day: "); 
     birthDay = input.nextInt(); 
     }while(validateBirthInput(birthDay) != 1); 

    do{ 
     System.out.print("Year: "); 
     birthYear = input.nextInt(); 
     }while(validateBirthInput(birthYear) != 1); 


} 

static int validateName(String s){ 
    int valid = 1; 
    char name[] = s.toCharArray(); 
    for(int i=0; i<name.length; i++){ 
     if(Character.isLetter(name[i]) || Character.isWhitespace(name[i])){ 
      valid = 1; 
     }else{ 
      System.out.println("Invalid input"); 
      valid = 0; 
      break; 
     } 
    } 
    return valid; 
} 

static int validateEmail(String s){ 
    int valid = 1; 
    if(ev.validate(s)){ 
     valid = 1; 
    }else{ 
     System.out.println("Invalid email"); 
     valid = 0; 
    } 
    return valid; 
} 

static int validateGender(String s){ 
    int valid = 1; 
    if(s.toLowerCase().compareTo("m") == 0 || s.toLowerCase().compareTo("f") == 0){ 
     valid = 1; 
    }else{ 
     valid = 0; 
    } 
    return valid; 
} 

static int validateBirthInput(int x){ 
    int valid = 1; 
    int birth; 
    try{ 
     birth = x; 
    }catch(Exception e){ 
     System.out.println("Enter numbers 1-12 for month, 1-31 for day, yyyy for year"); 
     valid = 0; 
    } 
    return valid; 
} 

static int validateAge(int bm, int bd, int by){ 
    int valid = 1; 
    int cm = cal.get(Calendar.MONTH); 
    int cd = cal.get(Calendar.DAY_OF_MONTH); 
    int cy = cal.get(Calendar.YEAR); 

    if((cy-by)<=13){ 
     if(bm>=cm){ 
      if(bd>cd){ 
       valid = 0; 
       System.out.println("You must be at least 13 years old"); 
       System.out.println(); 
      } 
     } 
    }else{ 
     valid = 1; 
    } 
    return valid; 
} 

static int confirm(String s){ 
    int reply; 
    if(s.toLowerCase().compareTo("yes") == 0){ 
     reply = 1; 
    }else if(s.toLowerCase().compareTo("no") >0){ 
     reply = 0; 
    }else{ 
     System.out.println("Invalid input. Type yes or no"); 
     reply = -1; 
    } 
    return reply; 
} 

static void displayProfile(){ 
    System.out.println(); 
    System.out.println("Last Name: " + lastName); 
    System.out.println("First Name: " + firstName); 
    System.out.println("Email: " + email); 
    System.out.println("Gender: " + gender); 
    System.out.printf("Birthday: %d/%d/%d\n\n", birthMonth, birthDay, birthYear); 
    System.out.println("(yes or no): "); 

} 
} 
+2

我已經爲您添加了作業標籤。請將此標籤添加到與作業相關的問題。 – pb2q 2012-08-08 05:28:50

+0

@ pb2q感謝您添加標籤 – mike26 2012-08-08 05:51:27

回答

2

嘿,這一個是非常棘手。

看一看的Javadoc Scanner.nextInt()

掃描輸入的下一個標記爲int

現在比較,對我們有什麼爲Scanner.nextLine()

將此掃描儀推進到當前行並返回跳過的輸入。此方法返回當前行的其餘部分,排除末尾的任何行分隔符。該位置設置爲下一行的開頭。

所以,當你調用nextInt(),你只讀了從輸入「INT」,當你調用nextLine()你讀一路攀升到行的末尾。您的掃描儀正在讀取System.in,這有點奇怪,因爲它不會向掃描儀提供任何數據,直到您打到輸入,將您鍵入的任何內容單獨放入一行。當你問一個日期/月/年,在一些數字,但沒有用戶類型發生,直到用戶點擊進入,然後調用nextInt(),它讀取在輸入的號碼。

到目前爲止好,除了掃描儀只讀取用戶輸入的數字,而不是新行。因此,當您再次通過registration()方法時,掃描儀中有一條備用空白行,lastName = input.nextLine();會立即讀取該行,並假定其有效,然後繼續詢問名字。

由於這是您的家庭作業,請查看您正在閱讀的內容以及用戶的輸入內容,請記住,System.in不會將任何內容交給掃描儀,直到用戶點擊輸入。你也許可以看看Integer method, parseInt(String)而不是使用nextInt()

0

我更改了你的代碼。您也可以根據自己的情況進行更改.. 快樂編碼..:)

import java.util.Calendar; 
import java.util.Scanner; 

public class mainExercise{ 
static Scanner input = new Scanner(System.in); 
static String lastName; 
static String firstName; 
static String email; 
static String gender; 
static String bday; 
static int birthMonth; 
static int birthDay; 
static int birthYear; 
static String confirmation; 

//static EmailValidator ev = new EmailValidator(); 
static Calendar cal = Calendar.getInstance(); 

public static void main(String[] args){ 

    do{ 

     do{ 
      registration(); 
     }while(validateAge(birthMonth, birthDay, birthYear) != 1); 

     displayProfile(); 
// this is coming "Invalid input. Type yes or no" because ist time input.nextLine() return ("") thats why. 
     do{ 
      confirmation = input.nextLine(); 
      }while(confirm(confirmation)== -1 || confirmation.equals("")); 

     if(confirm(confirmation)==1){ 
      break; 
     } 

    }while(confirm(confirmation)== 1); 

    System.out.println("Thank you for registering, " + firstName); 

}//end of main 


static void registration(){ 

    System.out.println("Welcome to Old School Facebook"); 
    System.out.println("To register, please provide the following  information");    

    System.out.print("Last Name: "); 
    do{ 
     lastName = input.nextLine(); 
     }while(validateName(lastName) != 1); 

    System.out.print("First Name: "); 
    do{ 
     firstName = input.nextLine(); 
     }while(validateName(firstName) != 1); 

    System.out.print("Email: "); 
    do{ 
     email = input.nextLine(); 
     }while(validateEmail(email) != 1); 

    System.out.print("Gender: "); 
    do{ 
     gender = input.nextLine(); 
     }while(validateGender(gender) != 1); 

    System.out.println("Enter birthdate"); 
    do{ 
     System.out.print("Month: "); 
     birthMonth = input.nextInt(); 
     }while(validateBirthInput(birthMonth) != 1); 

    do{ 
     System.out.print("Day: "); 
     birthDay = input.nextInt(); 
     }while(validateBirthInput(birthDay) != 1); 

    do{ 
     System.out.print("Year: "); 
     birthYear = input.nextInt(); 
     }while(validateBirthInput(birthYear) != 1); 


} 

static int validateName(String s){ 
    int valid = 1; 
    char name[] = s.toCharArray(); 
    for(int i=0; i<name.length; i++){ 
     if(Character.isLetter(name[i]) || Character.isWhitespace(name[i])){ 
      valid = 1; 
     }else{ 
      System.out.println("Invalid input"); 
      valid = 0; 
      break; 
     } 
    } 
    return valid; 
} 

static int validateEmail(String s){ 
    int valid = 1; 
    // this is showing error. You have to first initialize ev variable . 
    if(ev.validate(s)){ 
     valid = 1; 
    }else{ 
     System.out.println("Invalid email"); 
     valid = 0; 
    } 
    return valid; 
} 

static int validateGender(String s){ 
    int valid = 1; 
    // you have to check whith 1st char of that string othrewise it always show false 
    if(s.toLowerCase().substring(0, 1).compareTo("m") == 0 || s.toLowerCase().substring(0, 1).compareTo("f") == 0){ 
     valid = 1; 
    }else{ 
     valid = 0; 
    } 
    return valid; 
} 

static int validateBirthInput(int x){ 
    int valid = 1; 
    int birth; 
    try{ 
     birth = x; 
    }catch(Exception e){ 
     System.out.println("Enter numbers 1-12 for month, 1-31 for day, yyyy for year"); 
     valid = 0; 
    } 
    return valid; 
} 

static int validateAge(int bm, int bd, int by){ 
    int valid = 1; 
    int cm = cal.get(Calendar.MONTH); 
    int cd = cal.get(Calendar.DAY_OF_MONTH); 
    int cy = cal.get(Calendar.YEAR); 
// this should also need to change like following 
     if ((cy - by) > 13) 
     { 
     return valid; 
     } 
     else if ((cy - by) == 13) 
     { 
     if (bm>cm) 
     { 
      return valid; 
     } 
     else if (bm==cm && bd>cd) { 
      return valid; 
     } 
     } 
     System.out.println("You must be at least 13 years old"); 
     System.out.println(); 
     return 0; 

} 

static int confirm(String s){ 
    int reply; 
    if(s.toLowerCase().compareTo("yes") == 0){ 
     reply = 1; 
    }else if(s.toLowerCase().compareTo("no") >0){ 
     reply = 0; 
    }else{ 
     System.out.println("Invalid input. Type yes or no"); 
     reply = -1; 
    } 
    return reply; 
} 

static void displayProfile(){ 
    System.out.println(); 
    System.out.println("Last Name: " + lastName); 
    System.out.println("First Name: " + firstName); 
    System.out.println("Email: " + email); 
    System.out.println("Gender: " + gender); 
    System.out.printf("Birthday: %d/%d/%d\n\n", birthMonth, birthDay, birthYear); 
    System.out.println("(yes or no): "); 

} 
} 
+0

不知道這是如何幫助。當我運行這個(至少它現在編譯,因爲'EmailValidator'被註釋掉了),但我仍然看到相同的問題。 – 2012-08-08 07:30:15

+0

是的,我評論它,因爲有一個變量「ev」,我不知道它來自哪裏,所以我發表評論說,他的工作是以正確的方式完成它。 – 2012-08-08 07:36:01

+0

@JonLin我試圖創建兩個掃描儀對象,一個用於使用scanner.nextLine()的字符串輸入,另一個用於使用scanner.nextInt()的整數(生日字段),現在它正在工作。我想問題是我正在使用的功能,而不是在我做的循環中的算法 – mike26 2012-08-08 08:12:06