首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >If语句代码问题

If语句代码问题
EN

Stack Overflow用户
提问于 2013-03-07 23:33:04
回答 3查看 163关注 0票数 1

我的教授给了我下面的代码。

代码语言:javascript
运行
复制
public static void main(String[] args) {

    Customer customer;
    Transaction transaction;
    double withdrawalAmount = 0;

    boolean finished = false;

    while (finished == false)
    {
        // Menu Display and Get user input
        int inputInt = 0;
        while (inputInt == 0)
        {
            inputInt = displayMenuAndGetInput();

                    // if the input is out of range
                    if ((inputInt < 1) || (inputInt > 8))
                    {
                            System.out.println("\nThe input is out of range!");
                            System.out.println();
                            inputInt = 0;
                    }
            } //end while

            // switch to correspondence function
            switch (inputInt)

            {
                case 1:
                    customer = createPersonalCustomer();
                    System.out.println("\nThe Personal customer has been created: \n" + newPC.toString());
                    customers.add(customer);
                    break;
                case 2:
                    customer = createCommercialCustomer();
                    System.out.println("\nThe Commercial customer has been created: \n" + newCC.toString());
                    customers.add(customer);
                    break;
                case 3:
                    transaction = recordTransaction();
                    if(transaction != null)
                        System.out.println("\nThe Transaction has been created: \n" + trans.toString());
                    else
                        System.out.println("\nThe ID could not be found.");
                    break;
                case 4:
                    withdrawalAmount = makeWithdrawal();
                    if(withdrawalAmount > 0)
                        System.out.println("\nAmount withdrawn from this account: " + moneyFormat.format(acct.getMakeWithdrawal()) + "\n");
                    else
                        System.out.println("\nThe ID could not be found.");
                    break;
                case 5:
                    displayCustomer();
                    break;
                case 6:
                    displayCustomerSummary();
                    break;
                case 7:
                    displayGrandSummary();
                    break;
                case 8:
                    // exit
                    finished = true;
                    break;
                default:
                    System.out.println("Invalid Input!");

                    break;
            } // end switch
    } // end while

}

代码语言:javascript
运行
复制
// Create a new Transaction
public static Transaction recordTransaction(){}

并创建一个在以下场景中工作的循环:

输入客户id,如果客户id在数组中不匹配,则生成在情况3中读出的错误,并显示主菜单。如果客户id有效,则用户在下面输入信息。

下面是我的代码

代码语言:javascript
运行
复制
public static Transaction recordTransaction(){

System.out.println("Enter the customer ID to create the transaction > ");
    long customerID = scan.nextLong();

    for (Customer c : customers) {
        if (c.getCustomerID() == customerID) {
            if (trans != null) {

            System.out.println("\nEnter the weight of gold > ");
                Transaction.goldWt = scan.nextDouble();

                System.out.println("\nEnter the weight of platinum > ");
                Transaction.platinumWt = scan.nextDouble();

                System.out.println("\nEnter the weight of silver > ");
                Transaction.silverWt = scan.nextDouble();
                }
        }
            return null; 
}

无论如何,我已经运行了许多方法,我的代码要么接受无效和有效的客户ID,要么不接受无效或有效的客户id。我知道我可能忽略了一些东西,这就是为什么我拼命地请求论坛的帮助。当涉及到编程时,我有OCD倾向,这是我的第一个java入门类,所以我不是很精通这门语言。在过去的两天里,我一直被这个问题卡住了。请帮帮忙。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-03-07 23:43:44

您需要在recordTransaction()方法中实例化一个new Transaction(),并在适当的时候返回它。

代码语言:javascript
运行
复制
public static Transaction recordTransaction(){

    System.out.println("Enter the customer ID to create the transaction > "); long customerID = scan.nextLong();

    for (Customer c : customers) {
        if (c.getCustomerID() == customerID) {
            Transaction trans = new Transaction();

            System.out.println("\nEnter the weight of gold > ");
            trans.goldWt = scan.nextDouble();

            System.out.println("\nEnter the weight of platinum > ");
            trans.platinumWt = scan.nextDouble();

            System.out.println("\nEnter the weight of silver > ");
            trans.silverWt = scan.nextDouble();

            return trans;
        }
    }
    return null; 
}
票数 1
EN

Stack Overflow用户

发布于 2013-03-07 23:44:09

关于您的代码的一些备注:

代码语言:javascript
运行
复制
if (trans != null) {

什么是“反式”?我猜它一定是引用了一个事务实例。

代码语言:javascript
运行
复制
Transaction.goldWt = ...

那不应该是"trans.goldWt“吗?(当然,植物和白银也是如此)。如果它真的是Transaction.goldWt,那么您总是在更改该单个值。

代码语言:javascript
运行
复制
return null;

你从不返回任何东西,所以调用代码总是写成"no ID“。你不应该说"return trans“吗?

票数 0
EN

Stack Overflow用户

发布于 2013-03-08 11:28:58

该问题是由正在创建的每个帐户的随机customerID的getter和setter方法生成的。

谢谢你,谢谢你!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15275176

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档