主类:

package BankingSystem;


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Bank {

  public static void main (String [] args){

      //User verifies Account
        SignIn Valid = new SignIn();
        Valid.AccountLogin();
        Scanner choice = new Scanner (System.in);  //allow user to choose menu option

        int option = 0; //Menu option is set to 0


                   // Menu For User


        do{
            System.out.println("Welcome");
            System.out.println();
            System.out.println("1:Deposit Cash");
            System.out.println("2: Withdraw Cash");
            System.out.println("3: View Current Account Balance");
            System.out.println("4: View Saving Account Balance");


            System.out.println("5: Cancel"); //When the User Quits the system prints out GoodBye

            System.out.println("Please choose");
            option= choice.nextInt();



  }while (option < 6);
}


  }


AccountLoginClass:

package BankingSystem;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class SignIn {
    public void AccountLogin (){
          List<String> AccountList = new ArrayList<String>();
          AccountList.add("45678690");

          Scanner AccountInput = new Scanner(System.in);
         System.out.println("What is your account number?");
          AccountInput.next();
          boolean isExist = false;

          for  (String item : AccountList){
              if (AccountInput.equals(AccountList.get(0))){
                  System.out.println("Hi");
                  isExist = true;
                  break;
              }

          }

          if (isExist){
              //Found In the ArrayList
          }
          }



      }


我正在尝试创建一个相当复杂的银行系统。首先,我打算让用户输入希望与Array List匹配的帐号,当它与ArrayList中的编号匹配时,将显示菜单,即提现,存款等。
这里的问题是,我不确定如何才能在将菜单与AccountLoginClass链接之前创建了一个对象,但这并不能真正起作用。

最佳答案

您在这里的错误与其他问题相同。

AccountInput.equals(AccountList.get(0))


您正在将类java.lang.String的实例与类java.util.Scanner的实例进行比较。你的意思是:

(AccountInput.nextLine()).equals(AccountList.get(0))


这将起作用,并且您可以将帐号与ArrayList的元素匹配(而不是您自己写的List本身)

Java中的所有类均源自类Object(java.lang.Object)。因此,有时您可能不会收到异常,因为它们具有不同的签名。有时,即使它们在任何常识上都不相等,它们也可以相等。在好的时候,您会收到一个异常,该异常将使您的程序崩溃。

通常,您需要确保没有将苹果与橙子进行比较。

它非常容易检查:只需查看您要比较的声明即可

Orange or1, or2;
Apple ap1;
...
or1.equals(ap1)     // BAD
or1.equals(or2)     // Good if equals() implemented for class Orange in
                    // in the way it satisfies you.

10-02 04:21
查看更多