我正在嘗試使用 Java 將用戶添加到 LinkedList 中,但我想知道是否有更短的方法來檢查多個字串是否為空,而不是每次輸入新字串時都使用 if 陳述句
這是我想出的方法
public void addUser() {
int error = 0;
do {
Users user = new Users();
String name = JOptionPane.showInputDialog(null, "Enter your name");
if (name.isEmpty()) {
error = 1;
} else {
String password = JOptionPane.showInputDialog(null, "Enter a password");
if (password.isEmpty()) {
error = 1;
} else {
// more string inputs
}
}
} while (error != 0);
}
uj5u.com熱心網友回復:
實作一個單獨的方法來讀取輸入資料,直到提供有效的字串,并使用自定義提示呼叫此方法:
private static String readInput(String prompt) {
String input;
do {
input = JOptionPane.showInputDialog(null, prompt);
} while (input == null || input.isEmpty());
}
public void addUser() {
String name = readInput("Enter your name");
String password = readInput("Enter a password");
// ...
User user = new User(name, password, ...);
// ...
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/516511.html
標籤:爪哇细绳选项窗格是空的
