3

我对这个作业有疑问。该程序应该首先运行registration()。如果用户未满 13 岁,它会发出警报并返回registration(). 如果用户超过 13 岁,它会运行displayProfile()并要求确认,如果答案是肯定的,它会通知它是成功的,程序结束。如果答案是否定的,它应该返回registration()并重复处理。如果答案无效,它应该继续提示有效回复。问题出在registration(). 在程序开始时,它工作正常,但是当我输入一个 13 岁以下的用户并返回注册时,它会跳过姓氏的输入部分,而是继续打印“名字:” . 它在 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): ");

}
}
4

2 回答 2

2

呵呵,这个挺难的。

查看Scanner.nextInt()的 javadocs :

将输入的下一个标记扫描为 int

现在将其与Scanner.nextLine()的内容进行比较:

将此扫描器前进到当前行并返回被跳过的输入。此方法返回当前行的其余部分,不包括末尾的任何行分隔符。位置设置为下一行的开头。

因此,当您调用时nextInt(),您只会从输入中读取一个“int”,当您调用时,您nextLine()会一直读取到行尾。您的 Scanner 正在读取,这有点奇怪,因为在您点击System.in之前它不会向 Scanner 提供任何数据,将您输入的任何内容单独放入一行。当您询问日期/月份/年份时,用户输入了一些数字,但在用户点击 之前什么都没有发生,然后您调用,它会读取输入的数字。enterenternextInt()

到目前为止一切顺利,除了扫描仪只读取用户键入的数字,而不是新行。因此,当您registration()再次执行该方法时,扫描仪中有一个备用空行,并且lastName = input.nextLine();立即读取它,假设它是有效的,然后继续询问名字。

由于这是您的作业,请查看您正在阅读的内容以及用户正在输入的内容,请记住 System.in 在用户点击 之前不会将任何内容交给 Scanner enter。您也许可以看看这个Integer 方法, parseInt(String)而不是使用nextInt().

于 2012-08-08T06:38:25.253 回答
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): ");

}
}
于 2012-08-08T07:25:00.343 回答