简体   繁体   English

Java程序逻辑错误。 IF..Part不起作用/正在执行

[英]Java Program Logical Error. The IF..Part is not working/Executing

Hello Guys, 大家好,
I was writing a code for bank portal and I stuck onto a creepy error. 我在为银行门户网站编写代码,但遇到了一个令人毛骨悚然的错误。 I have a login() Function which is called through switch case present in the main function. 我有一个login()函数,该函数通过主函数中存在的switch case进行调用。 But whenever I put login details and login My else part get executed even if the condition is true . 但是每当我输入登录详细信息并登录时, 即使条件为true,我的else部分都会执行 Can you please check whats wrong. 能否请您检查有什么问题。 I'm a beginner to java programming. 我是Java编程的初学者。
I would also love to hear some suggestions on what new functionality should I add to this code. 我也很想听听关于我应该在此代码中添加哪些新功能的一些建议。
Also, rate the quality of my code, I would love to hear from you. 另外,请评价我的代码的质量,我希望收到您的来信。

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;

    }

   }
}

} }

There's a couple problems preventing your login function from working. 有几个问题使登录功能无法正常工作。 Firstly you're checking string equality with == when you're checking the password. 首先当您检查密码时,要使用==检查字符串是否相等 This doesn't work because in Java Strings are objects and not primitives. 这是行不通的,因为在Java中,字符串是对象而不是基元。

Next you're using your scanner to scan into passWd when it looks like you mean to be scanning it into password. 接下来,当您似乎打算将其扫描为密码时,使用扫描仪扫描至passWd。 In your version password is always null. 在您的版本中,密码始终为null。

With these changes your function looks like this: 通过这些更改,您的功能将如下所示:

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

In java Strings are object you cant compare 2 objects with == operator in a meaningful way. 在java中,字符串是对象,您不能用有意义的方式将两个对象与==运算符进行比较。 You must alway use equals method for that. 您必须始终使用equals方法。 When you check equality using == it will check whether 2 variables are referring to the same object. 当使用==检查相等性时,它将检查2个变量是否引用了同一对象。

you have to change if (accNum==accountNumber && passWd==password) 您必须更改if (accNum==accountNumber && passWd==password)

to if (accNum==accountNumber && passWd.equals(password)) if (accNum==accountNumber && passWd.equals(password))

Where is the value of "ch" coming from? “ ch”的值从哪里来?

You print a list of options but you do not appear to read the users reply at any point. 您打印了一个选项列表,但在任何时候都看不到用户的答复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM