2012-08-08 78 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