if (!contains(username, ' ')) {
if (!temp.equals(username)) {
if (!map.containsKey(username)) {
if (map.containsKey(temp)) {
if (map.get(temp).equals(password))
map.put(username, map.remove(temp));
else
System.out.println("The password is incorrect.");
} else {
System.out.println("The username is incorrect.");
}
} else {
System.out.println("This username already exists.");
}
} else {
System.out.println("The usernames cannot be the same.");
}
} else {
System.out.println("Username cannot contain a space.");
}
這是我的程式中復制的示例。如您所見,有很多縮進。使代碼更難閱讀的縮進。我能做些什么來使代碼更具可讀性嗎?謝謝。
uj5u.com熱心網友回復:
反轉驗證條件并使用else if:
if (contains(username, ' ')) {
System.out.println("Username cannot contain a space.");
} else if (temp.equals(username)) {
System.out.println("The usernames cannot be the same.");
} else if (map.containsKey(username)) {
System.out.println("This username already exists.");
} else if (!map.containsKey(temp)) {
System.out.println("The username is incorrect.");
} else if (!map.get(temp).equals(password)) {
System.out.println("The password is incorrect.");
} else { // everything's valid
map.put(username, map.remove(temp));
}
但是,您可能不希望快速失敗,而是盡可能多地收集驗證錯誤,因此可以準備一個錯誤串列,else-if然后檢查它是否為空:
List<String> errors = new ArrayList<>();
if (contains(username, ' ')) {
errors.add("Username cannot contain a space.");
}
if (temp.equals(username)) {
errors.add("The usernames cannot be the same.");
}
if (map.containsKey(username)) {
errors.add("This username already exists.");
}
if (!map.containsKey(temp)) {
errors.add("The username is incorrect.");
}
if (!map.get(temp).equals(password)) {
errors.add("The password is incorrect.");
}
if (errors.isEmpty()) { // everything's valid
map.put(username, map.remove(temp));
} else {
errors.forEach(System.out::println);
}
uj5u.com熱心網友回復:
有幾種方法可以在這里使用,但 IMO 最簡單的是構建器模式。我將首先將各種測驗提取到原語中boolean并使用 aStringJoiner作為訊息構建器。喜歡,
boolean containsSpace = username.contains(" ");
boolean userExists = !temp.equals(username);
boolean userNotInMap = !map.containsKey(username);
boolean userCorrect = userNotInMap && map.containsKey(temp);
boolean passCorrect = userCorrect && map.get(temp).equals(password);
StringJoiner sj = new StringJoiner(System.lineSeparator());
if (containsSpace) {
sj.add("Username cannot contain a space.");
}
if (!userNotInMap) {
sj.add("The usernames cannot be the same.");
}
if (!userExists) {
sj.add("This username already exists.");
}
if (!userCorrect) {
sj.add("The username is incorrect.");
}
if (!passCorrect) {
sj.add("The password is incorrect.");
}
if (sj.length() == 0) {
map.put(username, map.remove(temp));
} else {
System.out.println(sj);
}
uj5u.com熱心網友回復:
對于五個輸出(“密碼不正確”等)中的每一個,構造五個輸入(!包含(用戶名,'')等)真值表。根據這些,構建卡諾圖。根據這些,構造產品總和或總和布爾方程的產品。根據這些,撰寫代碼(形式為 if((ABC...) (ABC...)...)或 (A B C)(D E F)...,其中 A ,B,C... contains(username, ' '), !contains(username, ' ')temp.equals(username),...) 僅使用 1 級縮進。
此方法還可以捕獲和解決競爭。
并非所有語言都保證為此作業所需的順序。根據您的情況,某些變數值可能無效,具體取決于其他變數的值。在這種情況下,您需要考慮訂單。例如,如果 A 為假時 B 是假的,則 if(A and B) 是可以的(如果 A 為假,則不檢查 B),但 if(B and A) 可能會失敗,即使在邏輯上兩者是等價的。
根據您的情況,可能還需要仔細選擇結果“單縮進”ifs 的順序。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/433417.html
標籤:爪哇
