我遇到这个问题,我想用id标识客户端。我希望该ID仅是一个数字。有任何想法吗?预先感谢。
int id = IBIO.inputInt("Enter the clients id:");
boolean Clientexists = clientsAdm.existsClient(id) > 0;
if (Clientexists != true) {
String name = IBIO.inputString("Enter the client name:");
String surname = IBIO.inputString("Enter the client surname:");
String address = IBIO.inputString("Enter the client address:");
String phone = IBIO.inputString("Enter the client phone:");
String CreditCard = IBIO.inputString("Enter the type of card the client has: green, red or gold"); //asegurar que solo se ingrese RED, GREEN or GOLD
Client c = new Client(name, surname, address, phone, CreditCard, id);
clientsAdm.addClient(c);
} else {
IBIO.output("Error, this ID already exists.");
}
IBIO.output("Your client has been added.");
}
答案 0 :(得分:0)
这可能有效,如果您给我更多详细信息,也许我可以帮您打个招呼
cleartool lsvtree {my file} | findstr CHECKEDOUT
否则,您可以尝试以下操作:
int id = IBIO.inputInt("Enter the clients id:");
boolean Clientexists = clientsAdm.existsClient(id) > 0;
if (Clientexists != true) {
String name = IBIO.inputString("Enter the client name:");
String surname = IBIO.inputString("Enter the client surname:");
String address = IBIO.inputString("Enter the client address:");
String phone = IBIO.inputString("Enter the client phone:");
String CreditCard = IBIO.inputString("Enter the type of card the client has: green, red or gold"); //asegurar que solo se ingrese RED, GREEN or GOLD
Client c = new Client(name, surname, address, phone, CreditCard, id);
if(id instanceof Integer) {
clientsAdm.addClient(c);
} else {
IBIO.output("Id is not valid");
}
} else {
IBIO.output("Error, this ID already exists.");
}
IBIO.output("Your client has been added.");
}
答案 1 :(得分:0)
如果ID的非法输入为零,则可以检查ID是否为零,因为当IBIO.inputInt(String prompt)
被赋予非整数时,prompt
返回零。
如果ID的合法输入为零,则不能使用IBIO.inputInt(String prompt)
。当将非整数传递给IBIO.inputInt
时,将屏蔽该异常,并返回零。相反,您将需要定义自己的方法来读取用户输入。您可以执行以下操作:
static int inputIntWithRetry(String prompt) {
int result = 0;
int numberOfRetry = 5; // holds the number of times to reprompt the user
boolean isInt = false; // flag for exiting the loop early if user provides a int
for(int i = 0; i < numberOfRetry || !isInt; i++) {
try {
result = Integer.valueOf(
input(prompt).trim()).intValue();
isInt = true;
} catch (Exception e) {
System.out.println("Id is not valid");
}
}
if (isInt) {
return result;
} else {
// handle not receiving an int after 5 tries
}
}