簡體   English   中英

沒有打印出所有書面陳述

[英]Not printing out all of the statements written

樣品運行:

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

我的代碼是:import java.util.Scanner; 公開課五{

    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");
    }

 }

它只會輸出:

License Validator
Enter last name:

我對為什么其他字符串方法不輸出感到困惑? 另外,lastName的if語句已經刪除了“ isSpace”,並表示已棄用。

輸入姓氏后,它將僅打印年份提示,輸入年份后,僅顯示許可證編號提示。

Scanner.nextLine()掃描所有用戶輸入,直到按回車。 因此,只有當您按下Enter鍵時,它才會停止讀取輸入內容並繼續執行下一行代碼。

並且由於您已經對每個輸入進行了驗證檢查,因此在輸入的值有效之前,它將不接受您的輸入。 輸入有效輸入並按Enter后,它將繼續進行下一個方法。

編輯:

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

代碼的上面部分檢查字符是否為字母,如果不是字母/字母,則再次打印提示。 因此,每次遇到您的姓氏中的空格時,它都會失敗檢查,並提示您再次輸入姓氏。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM