Java程序无法正常运行。 (方法数组循环。)

时间:2018-12-03 17:51:15

标签: java arrays methods

即使我输入了“正确”的PIN码,该程序也会始终显示“错误:PIN码不正确”,

我使用“调试器”(IntelliJ)来查看错误在哪里,但什么也没看到。调试器向我展示了它们是相同的,但无论如何,它表明了它们不是相同的东西。预先感谢

public static void main (String args [])
{

    java.util.Scanner input = new java.util.Scanner (System.in);
    int acctNums [] = new int [] {12345 , 67890};
    String acctNames [] = new String []{"Mr Isk." , "Other"};
    String acctPINs [] = new String [] {"0101" , "9876"};
    double acctBalances [] = new double [] {10000.0 , 250.0};
    ATM.login(acctNums , acctNames , acctPINs , acctBalances);

}

 public static void login (int acctNums [] , String acctNames [] , String acctPINs [] , double acctBalances [])
{
    java.util.Scanner input = new java.util.Scanner(System.in);
    System.out.println("Please enter your account number");
    int acctNum = input.nextInt();
    int index = 0;
    if(findAccount(acctNums , acctNum) == -1) {
        System.out.println("ERROR: Account not found");
        System.exit(0);
    }
    else
        for(index = 0 ; index<acctNums.length; index++)

            if(findAccount(acctNums , acctNum) == index)
                break;
    System.out.println("Please enter your PIN");
    String PIN = input.next();
    if(acctPINs[index] == PIN)
        System.out.println("Hello" + acctNames[index] + "What would you like to do today?");

    else

        System.out.println("ERROR: PIN not correct");
        System.exit(0);

}

1 个答案:

答案 0 :(得分:0)

使用==比较2个String,它比较引用而不是String的值。尝试使用equals方法执行此类任务。

acctPINs [index] .equals(PIN)