java中的所有程序都使用字符串吗?
实际上问题的标题不太正确,我在程序中想要的是,每当我运行程序时,我都会通过输入对话框从用户那里获取输入并将其存储在字符串中。我为此制定了一个方法,每当我想在程序中使用这个字符串时,我只需调用这个方法,但我的问题是,每当我调用这个方法时,它都会弹出我不想要的输入对话框。 我希望输入对话框在程序运行时出现一次,然后我可以在程序中的任何地方使用该输入。 请帮我。 谢谢
Actually the title of the question is not properly right, what I want in my program is that whenever i run my program, I take an input from the user through input dialog and store it in a string. I have made a method for this and whenever i want to use this string in my program i just call this method but my problem is that whenever i call this method, it pops up the input dialog which I dont want.
I want the input dialog to come once when the program is run and then i could use that input anywhere in my program.
Please help me.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在调用 InputDialog 的方法中添加静态布尔变量。第一次调用对话框后,将布尔变量的值设置为 true。然后添加一个检查来确定是否显示对话框。考虑以下伪;
希望你明白了。
Add a static boolean variable in the method where you are calling the InputDialog. After calling the dialog for the first time, set the boolean var's value to true. And then add a check which will determine whether to show the dialog or not. Consider the following pseudo;
Hope you got the idea.
根据字符串的值设置对话框的条件。
Make the dialog conditional based on the value of your string.
创建一个变量来存储程序中的字符串。当需要获取字符串时,首先检查变量。如果它包含一个值,则返回该值并且不提示用户。如果它不包含值,则提示用户并将他们输入的内容存储在变量中,然后再返回。
Create a variable to store the string in your program. When you need get the string, check the variable first. If it contains a value, return it and don't prompt the user. If it does not contain a value, prompt the user and store what they type in in the variable before returning it.
编写一些可以推断程序状态的代码。所以第一次它将是空状态,这将触发对话框。如果不为空,则仅使用状态值。希望这有帮助。
Write some code that can infer the program state. So first time it will be empty state which will trigger the dialog. If it is not empty, it will just use the state value. Hope this helps.