在 Java 中全局化输入变量
我正在尝试用 Java 制作一个彩票游戏,它使用方法调用而不是 while 循环来实现循环目的。但是,我无法将输入变量(例如 int bet = input.nextInt())识别为其他方法的变量。我不知道如何全球化“bet”,以便它可以被所有方法使用,而不仅仅是在它所属的方法中使用。下面是我的代码的一部分,仅包括一个名为“play()”的方法
public static void play()
{
System.out.println("\nPlay? Yes = 1 No = 2 ");
Scanner input = new Scanner(System.in);
int play = input.nextInt();
if(play == 1)
{
System.out.print("Current balance is " + account);
System.out.print("\nPlace your bet: ");
int bet = input.nextInt();
if((bet <= account) && (bet > 0)){
lottery();
}
else if((bet < 0) || (bet > account)){
play();
}
}
else if(play == 2){
System.out.println("Final balance is " + account);
System.exit(0);
}
else{
System.out.println("Invalid input!");
play();
}
}
I am trying to make a lottery game in Java and it uses method calls instead of while loops for looping purposes. However, I cannot get an input variable( such as int bet = input.nextInt()) to be recognized as a variable for other methods. I do not know how to globalize "bet" so it can be used by all the methods instead of just in the method it is a part of. Here is part of my code below, including just the one method called "play()"
public static void play()
{
System.out.println("\nPlay? Yes = 1 No = 2 ");
Scanner input = new Scanner(System.in);
int play = input.nextInt();
if(play == 1)
{
System.out.print("Current balance is " + account);
System.out.print("\nPlace your bet: ");
int bet = input.nextInt();
if((bet <= account) && (bet > 0)){
lottery();
}
else if((bet < 0) || (bet > account)){
play();
}
}
else if(play == 2){
System.out.println("Final balance is " + account);
System.exit(0);
}
else{
System.out.println("Invalid input!");
play();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最简单的答案是将 play 和 bet 声明为静态类变量。
Simplest answer is to declare play and bet as static class variables.
在
play()
方法之外定义bet
。Define
bet
outside of theplay()
method.如果将 bet 定义为类中的字段,例如 :
,则 bet 在类中的所有方法中都可用,但在外部不可用。
没有必要将 play 公开,因此这可以很好地保留在 play 方法的范围内。给予变量不超过严格需要的可见性被认为是良好的做法。
在这种情况下,大多数人不会将赌注设置为字段,而是将其作为参数传递给彩票方法。在这种情况下,可见性甚至可以进一步仅限于游戏和彩票方法。
最后一点是您使用函数调用来循环。由于java不支持尾部调用优化(并且在任何情况下都不适用于此处),如果您玩得足够长,您将填满堆栈并最终死于堆栈溢出。
If you define bet as a field in the class like :
then bet is available in all methods in the class, but not outside.
It is not necessary to make play public, so this can stay nicely inside the scope of the play method. It is considered good practice to give variables no more visibility than strictly needed.
Most people would in this case not make bet a field, but pass it as a parameter to the lottery method. In this case the visibiltiy can even be further restricted to only the play and lottery methods.
A final note is you use of function calls to loop. Since java does not support tail-call-optimisation (and it would not apply here in any case) you are going to fill up the stack and finally die of a stack overflow if you play long enough.
将其作为参数在方法之间传递。 (不建议将其定义为 public。)或者,如果这是一个类,您可以将其设为成员变量(属性)。
Pass it between methods as a parameter. (Defining it as public is not recommended.) Or, if this is a class, you can make it a member variable (property).