我覺得這是檢查的時間越長,繁瑣和低效的方式:如何有效檢查兩個字符串是相同的,它們的非空
- 該密碼字段不爲空
- 那這兩個密碼相匹配
- 兩個密碼是不一樣的
private void checkPasswordSame() {
String first = password1.getText();
String second = password2.getText();
if (first.equals("")) {
System.out.println("Password can't be empty");
if ("".equals(second)) {
System.out.println("Second password is empty");
}
} else if (first.equals(second)) {
System.out.println("Passwords same");
} else {
System.out.println("Passwords not the same");
}
}
有沒有辦法以較少的行數來做到這一點?
爲什麼所有的等於(「」)在這個問題和答案? isEmpty()方法有什麼問題?有沒有我不知道的一些魔術迷你優化技巧? – Gimby 2014-09-05 07:58:25