有人可以告訴我我的代碼有什麼問題嗎?即使我把正確的密碼,無法與爲getBalance()來訪問平衡... :(無法正確訪問getter
package home.exercises.exceptionHandling;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Manager {
private double balance = 15000.25;
public void getBalance() throws InputMismatchException {
@SuppressWarnings("resource")
Scanner s = new Scanner(System.in);
System.out.println("Enter password: ");
String password = s.nextLine();
if (password == "ManagerWantsTogetBalance") {
System.out.println("Remainning balance is " + this.balance);
} else {
System.out.println("Wrong password! Try Again..");
}
}
}
package home.exercises.exceptionHandling;
import java.util.InputMismatchException;
public class TestManager {
public static void main(String[] args) {
Manager branchManager = new Manager();
try {
branchManager.getBalance();
}
catch(InputMismatchException iex) {
System.out.println("Put password in the correct form");
}
}
}
這篇文章缺少一個例子和一個明確的問題陳述。您必須提供輸入示例,說明此代碼無法運行。 –