Java 程序逻辑错误。 IF..部分不工作/执行

标签 java if-statement

大家好,
我正在为银行门户编写代码,但遇到了一个令人毛骨悚然的错误。 我有一个 login() 函数,它是通过主函数中存在的 switch case 调用的。但是每当我输入登录详细信息并登录即使条件为真,我的其他部分也会被执行。你能检查一下出了什么问题吗?我是java编程的初学者。
我也很想听听一些关于我应该向此代码添加哪些新功能的建议。
另外,请评价我的代码质量,我很乐意听取您的意见。

package e.banksolutions;
import java.util.Scanner;
import java.util.Random;
import java.util.Date;
public class EBankSolutions {

    long accountNumber;            // Variable for Storing account number
    long accountNumberGenerator=0000;   // Variable for generating and assigning account number
    String AccountHolderName;     // Variable for Storing account holder name
    String AccountType;           // Variable for Storing account type in string
    int AccTypeNumVal;
    long accountBalance;          // Variable to Store Current Account Balance. 
    String password;                //27-06-2018
    //----------------------------------------------------------------------------//

public void createAccount() throws InterruptedException
{
    Random rand = new Random();
    Scanner scan = new Scanner(System.in); // Scanner variable for accepting value from user
    System.out.println("----------------Welcome to Account Creation Portal-------------------");
    //accountNumberGenerator++; //Incrementing the account number generator var
    accountNumber=rand.nextInt(1000);; //assigning the account number
    System.out.println("Enter Your Name(Without Space Between Name) : ");
    AccountHolderName=scan.next();
    System.out.println("Enter New Password : ");                //27-06-2018
    password=scan.next();                                       //27-06-2018
    System.out.println("Enter Your Account Type Savings/Current");
    AccountType=scan.next();

   /* if(AccountType == "Savings"){
        AccTypeNumVal=1;
    }
    else if(AccountType == "Current"){
        AccTypeNumVal=2;
    }*/

bal:System.out.println("Enter your starting balance : ");
    accountBalance=scan.nextLong();
    if(accountBalance<5000){
        System.out.println("Oops!!! Your Account balance should be minimum 5000 or more.\nYou need to fill the form again.");
        System.exit(0);
    }

    System.out.println("Account Created Successfully.\nYour Account Number is: "+accountNumber+" Please Note it Down.");
    System.out.println("------------------------------------------------------------");
    //Delay code below
    Thread.sleep(5000);
    System.out.flush();
}

public void DisplayAccount() throws InterruptedException
{
    System.out.println("Displaying Account Information for Account Number : "+accountNumber);
    System.out.println("------------------------------------------------------------");
    System.out.println("Account Number  : "+accountNumber);
    System.out.println("Account Name    : "+AccountHolderName);
    System.out.println("Account Type    : "+AccountType);
    System.out.println("Account balance : "+accountBalance);
    System.out.println("Your Password   : _Hidden_"); //27-06-2018
    System.out.println("------------------------------------------------------------");
    Thread.sleep(5000);
    System.out.flush();
}

public void DepositAmount() throws InterruptedException
{
    Scanner scan = new Scanner(System.in); // Scanner variable for accepting value from user
    long depositAmt;
    System.out.println("---------------Welcome to Deposit Portal----------------");
    System.out.println("You are Depositing amount for Account Number: "+accountNumber);
    System.out.println("\nEnter the Amount to Deposit : ");
    depositAmt=scan.nextLong();
    accountBalance=accountBalance+depositAmt;
    System.out.println("Amount Deposited Successfully.. \nUpdated Balance: "+accountBalance);
    System.out.println("------------------------------------------------------------");
    Thread.sleep(5000);
    System.out.flush();
}


public void WithdrawAmount() throws InterruptedException
{
    Scanner scan = new Scanner(System.in); // Scanner variable for accepting value from user
    long withdrawAmt;
    System.out.println("---------------Welcome to Deposit Portal----------------");
    System.out.println("You are Withdrawing amount for Account Number: "+accountNumber);
    System.out.println("\nEnter the Amount to Withdraw : ");
    withdrawAmt=scan.nextLong();
    accountBalance=accountBalance-withdrawAmt;
    System.out.println("Amount Withdrawn Successfully.. \nUpdated Balance: "+accountBalance);
    System.out.println("------------------------------------------------------------");
    Thread.sleep(5000);
    System.out.flush();
}

public void login() throws InterruptedException
{
    int ch=0;
    Scanner scan = new Scanner(System.in);
    long accNum=0;
    String passWd="0";
    System.out.println("Enter Account Number : ");
    accNum=scan.nextLong();
    System.out.println("Enter Your Password  : ");
    passWd=scan.next();           
    if (accNum==accountNumber && passWd==password) {
        System.out.println("Logged In Successfully with account number: "+accountNumber+"\n-------------------------------------");   
        System.out.println("Choose Option Number From Below Menu");
        System.out.println("1.Deposit Amount\n2.Withdraw Amount\n3.Display Account Info\n4.Close Account\n5.Exit");
        switch(ch)
        {
            case 1: DepositAmount();
                    break;
            case 2: WithdrawAmount();
                    break;
            case 3: DisplayAccount();
                    break;
            case 4: System.out.println("You cannot close your account. Feature Coming Soon...");
                    break;
            case 5: System.exit(0);
                    break;
        }
    } 
    else if(accNum!=accountNumber && passWd!=password) {
        System.out.println("You have Entered Incorrect Account Number or Password. Please Check Again.");
        Thread.sleep(5000);
        System.out.flush();
        System.exit(0);
    }
    else{
        System.out.println("Unknown Error Occured. Try Agian Later");
        Thread.sleep(5000);
        System.out.flush();
        System.exit(0);
    }
}


EBankSolutions()
{
    accountNumber=0000;
    AccountHolderName="UNDEFINED";
    AccountType="UNDEFINED";
    accountBalance=0000;
    accountNumberGenerator=0000;
    AccTypeNumVal=9;
}

public static void main(String[] args) throws InterruptedException {
    Scanner scan = new Scanner(System.in); // Scanner variable for accepting value from user
    EBankSolutions a1 = new EBankSolutions();
    int ch;
    int i=0;
    System.out.println("Welcome to Bank E Portal\n");
   while(i!=5)
   {
    System.out.println("Select any Choice Number From below menu...");
    System.out.println("1. Create Account\n2. Login\n3. Exit Portal");
    System.out.print("Enter Your Choice Code 1-4: ");
    ch=scan.nextInt();
    switch(ch)
    {
        case 1: a1.createAccount();
                break;

        case 2: a1.login();
                break; 

        case 3: System.exit(0);
                break;

    }

   }
}

}

最佳答案

有几个问题导致您的登录功能无法正常工作。首先你要检查string equality with ==当你检查密码时。这是行不通的,因为在 Java 中字符串是对象而不是基元。

接下来,当您似乎打算将其扫描为密码时,您将使用扫描仪扫描到 passWd。在您的版本中密码始终为空。

通过这些更改,您的函数如下所示:

public void login() throws InterruptedException
{
    //snip
    System.out.println("Enter Your Password  : ");
    password=scan.next();           
    if (accNum==accountNumber && passWd.equals(password)) {
        //snip
    }
}

关于Java 程序逻辑错误。 IF..部分不工作/执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51066548/

相关文章:

java - Java OutOfMemoryError 时如何识别问题?

java - 对于来自 Visual Studio/C# 背景并想学习 Java 的人 - 我应该得到 : Netbeans or Eclipse?

java - Android中从本地隐藏文件夹下载图像并显示

java - Eclipse 中的“恢复默认值”首选项页面功能

Java "Variable Name"无法解析,IF 语句中的变量错误

C:如果语句不起作用

java - 有没有办法让 Java 应用程序获得 root 权限?

java - 在没有变量的 if 语句中验证扫描仪用户输入

stored-procedures - Firebird 和存储过程 : if exists then else

MySQL if then/case 语句