仅接受8位数字

时间:2019-03-21 02:08:57

标签: java

我有此分配,并且当我尝试更改销售值时它不会更改,并且当我输入销售人员ID时必须获得或多或少接受8位数字,我需要帮助 我尝试了.....但我在这里停了下来。

else {
               System.out.print("Please enter your ID : ");
               String ID = scanner.nextLine();
               //Declaration
               boolean exists = checkIfIDExists(ID);
               if(exists) {
                   System.out.print("Please enter new sales value : $");
                   float newSalesValue = scanner.nextFloat();

                   Iterator<Salesperson> iterator = salespersons.iterator();
                   while(iterator.hasNext()) {
                       Salesperson salesperson = iterator.next();
                       if(salesperson.getID().equals(ID)) {
                           salesperson.setAnnualSales(newSalesValue);
                       }
                   }
               }

capcapet 8位数字

}
           else {
               System.out.print("Please enter your ID : ");
               String ID = scanner.nextLine();

               boolean ifValid = validateID(ID, salespersons);

               if(MAX_ID_CHAR_COUNT != ID.toCharArray().length) {
               } else {
                   System.out.print("Please enter eight digits of your ID : ");
                   ID = scanner.nextLine();

                   // Declaration and loop
               }
              //if = validateID(ID, salespersons);
               if(ifValid) {                      
                   System.out.print("Duplicate digits. Please enter eight digits of your ID : ");
                      ID = scanner.nextLine();

               }

1 个答案:

答案 0 :(得分:1)

使用循环来验证您的ID是否有效,即代码

   public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        boolean ifValid=false;
        String ID;
        //using loop to validate this id is valid and length is 8
        do{
            System.out.print("Please enter your ID : ");
            ID = scanner.nextLine();
            ifValid= validateID(ID, salespersons);
            if (!ifValid){
                System.out.print("Duplicate digits. Please enter eight digits of your ID : ");
            }
        }while (!ifValid);
        System.out.print("Please enter eight digits of your ID : ");
        ID = scanner.nextLine();
    }

    private static boolean validateID(String id, Object salespersons) {
        return id!=null&&8==id.length();
    }