gpt4 book ai didi

java - 仅接受 8 位数字

转载 作者:行者123 更新时间:2023-11-30 05:43:23 25 4
gpt4 key购买 nike

我有这项任务,当我尝试更改销售值(value)时,我需要帮助,但它没有改变,并且当我输入销售人员的 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);
}
}
}

输入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();

}

最佳答案

使用循环来验证您的 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();
}

关于java - 仅接受 8 位数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55272798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com