简体   繁体   English

从另一个方法调用方法?

[英]Calling method from another method?

I'm semi-new to programming and I am having trouble calling a method inside another method. 我是编程的新手,在调用另一个方法内的方法时遇到麻烦。 I'm not completely done writing my code but was checking to make sure it is working as intended as I go. 我编写代码还没有完全完成,但是正在检查以确保它按照我的预期工作。 So there are other errors in the code but I'm assuming it shouldn't matter for the part I have below. 因此,代码中还存在其他错误,但我认为对于下面的内容,这应该无关紧要。

JOptionPane.showInputDialog(null, "Press n to add a new account\n Press d to add a deposit\n "
            + "Press w to withdraw from an account\n Or press x to exit");        
    String choice = input.next();        
    if(choice.equals("n")){
            Bank n = new Bank();
            n.newAccount();
    }

public void newAccount() {
    JOptionPane.showInputDialog(null, "Enter the account number");

So my problem is that it prompts me to enter n/d/w/x as it should in the first part, but then nothing else happens when I enter "n". 所以我的问题是,它提示我像在第一部分中那样输入n / d / w / x,但是当我输入“ n”时没有其他反应。 Where as I thought after entering n it would prompt me for the account number. 在输入n之后,我以为我会在哪里提示我输入帐号。

FYI: the name of the class is Bank. 仅供参考:班级的名称是银行。 Also the code is a little lengthy so I only pasted a portion of it (there are more if statements for entering d/w/x and there are other methods to the program as well. 而且代码有点冗长,所以我只粘贴了一部分(如果要输入d / w / x的语句还有更多,程序还有其他方法。

You just need to throw this in a debugger and it will be very clear. 您只需要把它扔到调试器中就可以了。

One thing to check is to make sure is the input you are getting is really 'n'. 要检查的一件事是确保获得的输入确实是'n'。

Perhaps put a print statement right after variable choice is initialized. 也许在初始化变量选择之后立即放置一条打印语句。

System.out.println(String.format("Choice '%s'", choice));

为什么不通过打印choice值自己尝试一下,或者如果您正在使用任何IDE,然后以调试模式启动服务器,然后通过在代码中应用调试点来检查要检查的内容,那么为什么不尝试。

You need to set the "choice" variable to the return from the input dialog, as it returns the user's input as a string, like so: 您需要将“ choice”变量设置为输入对话框的返回值,因为它以字符串形式返回用户的输入,如下所示:

String choice = (String)JOptionPane.showInputDialog(null, "Press n to add a new account\n Press d to add a deposit\n "
            + "Press w to withdraw from an account\n Or press x to exit"); 

Check out this tutorial for more info: How to Make Dialogs 查看本教程以获取更多信息: 如何制作对话框

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

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