方法的返回值不会返回到main

时间:2014-10-31 21:34:46

标签: java eclipse

大家好我是编程和Stackoverflow的新手(第一篇文章)[对不起,如果我没有正确格式化],这是我使用JAVA的第一堂课的作业。我知道这有很多错误,所以请随意指出,所以我可以学习。我想学习!我很难识别逻辑错误以及如何修复它们。

我将对此有多个问题,但首先我必须让方法checkID()返回main()以验证用户acct信息是否匹配。

我无法在checkID()方法中更改字符串a,b和c的编码,我的教授故意编码。

我不再收到错误消息谢谢!!!但是现在如果我输入错误或正确的信息,我会陷入无限循环。 menu()方法没有被调用并打印到屏幕上,它也没有再次调用checkID()方法。

非常感谢任何帮助!在此先感谢!!!

/*
 This program simulates a simple ATM machine.
 User can withdraw and deposit money by entering correct acct number and password.*/

import java.util.*;

public class ATM {

    public static Scanner kbd; 

    public static void main(String[] args) 
    {
        Scanner kbd = new Scanner(System.in);
        String acctNum, pwd, verifyID;
        int choice, attempts=0;
        double acctBal, depositAmount, withdrawAmount;

        System.out.println( "Enter account number: ");
        acctNum=kbd.nextLine();
        System.out.println("Enter password: ");
        pwd=kbd.nextLine();

        verifyID=checkID(acctNum, pwd); 
        //calls checkID method and returns result to verify

        while(verifyID.equals("error") && attempts<=3) 
            //checkID returns "error" and the attempts are less than or equal to 3.
        {
            if (attempts==4) //If Maximum attempts reached display message, then exit. 
            {
                System.out.print("Maxium attemps reached.");
                System.exit(1);
            }
        }
        acctBal=Double.parseDouble(verifyID); 
        //Changing the string from checkID method to double *(acct balance is returned)

        choice=menu();//Calls menu() method.

        switch (choice)
        {
        case(1)://user enters balance option.
            displayBalance(acctBal); //Call method displayBalance.
        break;
        case(2)://User enters deposit option.
            System.out.println("Enter amount to deposit: ");
            depositAmount=kbd.nextDouble();
            acctBal=deposit(acctBal,depositAmount);//call method deposit().
            System.out.printf("Your new account balance is: $%.2f"+acctBal); //Print new acctBal to screen.
        break;      
        case (3)://User enters withdraw option.
            System.out.println("Enter amout to withdraw: ");
            withdrawAmount=kbd.nextDouble();
            acctBal=withdraw(acctBal, withdrawAmount);//call method withdraw().
            System.out.printf("Your new account balance is: $%.2f"+acctBal);
        break;
        case(4)://User enters log-out
            System.out.println("You are logged out.");
            System.exit(1);
        }
    }

    public static void displayBalance(double acctBal)
    {
        System.out.printf("Your current balance is : $%.2f" + acctBal);
    }

    /*The checkID method determines if acctNum is a valid account number
    and pwd is the correct password for the account.  If the account information
    is valid, the method returns the current account balance, as a string.
    If the account information is invalid, the method returns the string "error".*/

    public static String checkID(String acctNum, String pwd)
    {
        String result = "error";

        /* Strings a, b, and c contain the valid account numbers and passwords.
        For each string, the account number is listed first, followed by
        a space, followed by the password for the account, followed by a space,
        followed by the current balance.*/
        String a = "44567-5 mypassword 520.36";
        String b = "1234567-6 anotherpassword 48.20";
        String c = "4321-0 betterpassword 96.74";

        if(acctNum.equals(a.substring(0, a.indexOf(" "))) && 
                pwd.equals(a.substring(a.indexOf(" "), a.lastIndexOf(" " +1))))
        {
            result=a.substring(a.lastIndexOf(" ") +1, -1);
        }
        else if (acctNum.equals(b.substring(0, b.indexOf(" "))) && 
                pwd.equals(b.substring(b.indexOf(" "), b.lastIndexOf(" " +1))))
        {
            result=b.substring(b.lastIndexOf(" ") +1, -1);
        }
        else if (acctNum.equals(c.substring(0, c.indexOf(" "))) && 
                pwd.equals(c.substring(c.indexOf(" "), c.lastIndexOf(" " +1))))
        {
            result=c.substring(c.lastIndexOf(" ") +1, -1);
        }
        return result;

        // insert code here to determine if acctNum is a valid account number
        // and pwd is the correct password for the account.
    }

    public static double deposit(double acctBal, double depositAmount)
    {   
        return acctBal=acctBal + depositAmount;
    }
    public static double withdraw(double acctBal, double withdrawAmount)
    {
        if (acctBal<=withdrawAmount)
        {
            System.out.println("Insuffienct funds.");
            return acctBal;
        }
        else
        {
            return acctBal-withdrawAmount;
        }   
    }

    public static int menu()
    {
        int input=0; 
        while (input>=0 || input<=5) //check side
            //Checking input from user is between 1-4.
        {
            System.out.println("Main Menu\n1. Display Balance\n\n2. Deposit\n\n"
                    + "3. Withdraw\n\n4. Log Out\n\n(Please enter 1, 2, 3, or 4):");
            input=kbd.nextInt();

            if (input<=0 ||input>=5)
                //If input is not between 1-4 print error.
            {
                System.out.println("Invaid input.");
            }
            else 
            {
                return input;
            }   
        }
        return input;
    }
}

6 个答案:

答案 0 :(得分:1)

c.substring(..., -1);

我知道像Python这样的一些库允许负面索引表示类似于#34;反之亦然#34;但它对Java无效。

在Java中你需要做

result = c.substring(c.lastIndexOf(" ") + 1, c.length());

或者只是

result = c.substring(c.lastIndexOf(" ") + 1);

另请参阅substring(int)substring(int, int)

答案 1 :(得分:0)

使用

acctNum=kbd.next();

pwd=kbd.next();

而不是nextLine!

答案 2 :(得分:0)

我建议你重构checkID方法。首先,您可以分别编写三种实用工具方法来获取余额,帐号和密码。其次,您的帐户可能应该在一个数组中。

private static Double getBalance(String str) {
    return Double.valueOf(str.split("\\s+")[2]);
}

private static String getAcctNum(String str) {
    return str.split("\\s+")[0];
}

private static String getPassword(String str) {
    return str.split("\\s+")[1];
}

private static String[] accts = { "44567-5 mypassword 520.36",
        "1234567-6 anotherpassword 48.20", "4321-0 betterpassword 96.74" };

最后,您可以使用for-each loop来迭代accts并在找到匹配项时返回余额,

public static Double checkID(String acctNum, String pwd) {
    for (String account : accts) {
        if (acctNum.equals(getAcctNum(account))) {
            if (pwd.equals(getPassword(account))) {
                return getBalance(account);
            }
        }
    }
    return null;
}

答案 3 :(得分:0)

我认为你应该替换

pwd.equals(c.substring(c.indexOf(" "), c.lastIndexOf(" " +1))))

pwd.equals(c.substring(c.indexOf(" "), c.lastIndexOf(" ") +1)))

也适用于a和b

答案 4 :(得分:0)

if(acctNum.equals(a.split(" ")[0]) && pwd.equals(a.split(" ")[1]))
    {
        result=a.split(" ")[2];
    }
    else if (acctNum.equals(b.split(" ")[0]) && pwd.equals(b.split(" ")[1]))
    {
        result=a.split(" ")[2];
    }
    else if (acctNum.equals(c.split(" ")[0]) && pwd.equals(c.split(" ")[1]))
    {
        result=a.split(" ")[2];
    }
return result;

这将消除任何java.lang.StringIndexOutOfBoundsException

您遇到的下一个错误是java.lang.NullPointerException,这是因为您需要实例化扫描仪。把它放在菜单()中:kbd = new Scanner(System.in);input=kbd.nextInt();之前)

答案 5 :(得分:0)

首先你需要增加循环中的attemts,否则它就会继续;

while(verifyID.equals("error") && attempts<=3) 
        //checkID returns "error" and the attempts are less than or equal to 3.
    {
        attemts++;
        if (attempts==4) //If Maximum attempts reached display message, then exit. 
        {
            System.out.print("Maxium attemps reached.");
            System.exit(1);
        }
    }

并且在checkID中你已经使你的子串正确的方式

if(acctNum.equals(a.substring(0, a.indexOf(" "))) 
            && pwd.equals(a.substring(a.indexOf(" ")+1, a.lastIndexOf(" "))))
    {
        result=a.substring(a.lastIndexOf(" ")+1);
    }

并在ex中使用System.out.format。 displaybalance

System.out.format("Your current balance is : %5.2f\n",acctBal);