简体   繁体   English

没有打印出所有书面陈述

[英]Not printing out all of the statements written

Sample run: 样品运行:

License Validator
Enter a last name: ab    bA
Enter a last name: ab bA
Enter year of birth: a12w
Enter year of birth: 1979
Enter driver's license number: A123-4567-9801-23
Consistent

My code is: import java.util.Scanner; 我的代码是:import java.util.Scanner; public class five { 公开课五{

    private static String getLastName(Scanner sc)
    {
         System.out.println("License Validator");
        System.out.print("enter last name: ab bA");
        String lastname=sc.nextLine().trim();
        if(lastname.length()==0)
            lastname=getLastName(sc);
        int numspaces=0;
        for(int i=0;i<lastname.length();i++)
            {
                if(Character.isSpace(lastname.charAt(i)))
                    {
                        numspaces++;
                    }
                else if(!Character.isLetter(lastname.charAt(i)))
                    {
                        lastname=getLastName(sc);
                    }

                if(numspaces>2)
                    {
                        lastname=getLastName(sc);
                    }      
            }
        return lastname;
    }
    private static boolean Character(char charAt) {
        return false;
    }
    public static String getYearOfBirth(Scanner sc)
    {
        System.out.print("Enter year of birth: 1979");
        String year=sc.nextLine().trim();
        if(year.length()==0)
            year=getYearOfBirth(sc);
        try
        {
        int yearint=Integer.parseInt(year);
            if(yearint<1900||yearint>1999)
            {
                year=getYearOfBirth(sc);
            }  
        }
        catch(NumberFormatException e)
        {
            year=getYearOfBirth(sc);
        }
        return year;
    }
    public static String getlicensenumber(Scanner sc)
    {
        System.out.print("Enter driver's license number:");
        String licencenum=sc.nextLine().trim();
        if(licencenum.length()!=17||licencenum.length()==0)
        {
            licencenum=getlicensenumber(sc);
        }
        String arr[]=licencenum.split("-");
        if(arr.length!=4)
        {

            licencenum=getlicensenumber(sc);
        }
        if(Character.isLetter(arr[0].charAt(0)))
        {
            if(Character.isLowerCase(arr[0].charAt(0)))  
            {

                    licencenum=getlicensenumber(sc);
            }
        }
        else
        {

            licencenum=getlicensenumber(sc);
        }
        try
        {
            Long.parseLong(arr[0].substring(1)+arr[1]+arr[2]+arr[3]);
        }
        catch(NumberFormatException e)
        {
            licencenum=getlicensenumber(sc);
        }
        return licencenum;
    }
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        String lastname=getLastName(sc);
        String yearOfBirth=getYearOfBirth(sc);
        String licensenumber=getlicensenumber(sc);
        if(licensenumber.substring(0, 1).equalsIgnoreCase(lastname.substring(0, 1))&&(licensenumber.substring(8, 9)+licensenumber.substring(10, 11)).equals(yearOfBirth.substring(2)))
            {
                System.out.println("Consistent");
            }
        else
            System.out.println("fraudulent");
    }

 }

It will only output: 它只会输出:

License Validator
Enter last name:

I'm confused as to why the other string methods are not outputting? 我对为什么其他字符串方法不输出感到困惑? Also, the if statement for lastName has crossed out the "isSpace" and says that it is depreciated. 另外,lastName的if语句已经删除了“ isSpace”,并表示已弃用。

It will only print the prompt for year after you enter the last name and the prompt for license number after you input the year. 输入姓氏后,它将仅打印年份提示,输入年份后,仅显示许可证编号提示。

Scanner.nextLine() scans all the user input till he hits enter. Scanner.nextLine()扫描所有用户输入,直到按回车。 So only when you hit enter will it stop reading for input and move on to the next line of code. 因此,只有当您按下Enter键时,它才会停止读取输入内容并继续执行下一行代码。

And since you have put in validation checks for each input, it will not accept your input till the entered value is valid. 并且由于您已经对每个输入进行了验证检查,因此在输入的值有效之前,它将不接受您的输入。 Once you enter a valid input and hit enter it will move on to the next method. 输入有效输入并按Enter后,它将继续进行下一个方法。

EDIT: 编辑:

  else if (!Character.isLetter(lastname.charAt(i))) {
            lastname = getLastName(sc);
  }

The above part in your code checks if the character is a letter or not and prints the prompt again if it is not a letter/alphabet. 代码的上面部分检查字符是否为字母,如果不是字母/字母,则再次打印提示。 So, every time it encounters a space in your last name, it fails the check and prompts you to enter the last name again. 因此,每次遇到您的姓氏中的空格时,它都会失败检查,并提示您再次输入姓氏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM