在 Java 中全局化输入变量

发布于 2024-09-18 06:21:08 字数 903 浏览 3 评论 0原文

我正在尝试用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一抹微笑 2024-09-25 06:21:08

最简单的答案是将 play 和 bet 声明为静态类变量。

public class Game {
   private static int play = 0;
   private static int bet = 0;

   public static void play() {
      System.out.println("\nPlay? Yes = 1 No = 2 ");
       Scanner input = new Scanner(System.in);
       play = input.nextInt();
       if(play == 1) {

        System.out.print("Current balance is " + account);
        System.out.print("\nPlace your bet: ");
        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();
    }
}

Simplest answer is to declare play and bet as static class variables.

public class Game {
   private static int play = 0;
   private static int bet = 0;

   public static void play() {
      System.out.println("\nPlay? Yes = 1 No = 2 ");
       Scanner input = new Scanner(System.in);
       play = input.nextInt();
       if(play == 1) {

        System.out.print("Current balance is " + account);
        System.out.print("\nPlace your bet: ");
        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();
    }
}
别再吹冷风 2024-09-25 06:21:08

play() 方法之外定义 bet

Define bet outside of the play() method.

晚风撩人 2024-09-25 06:21:08

如果将 bet 定义为类中的字段,例如 :

public class Lottery {
    private int bet;

    public void lottery() {
        ...
    }

    public void play() {
        ...
    }

    public static void main(String[] args) {
        Lottery lottery = new Lottery();
        lottery.play();
    }
}

,则 bet 在类中的所有方法中都可用,但在外部不可用。

没有必要将 play 公开,因此这可以很好地保留在 play 方法的范围内。给予变量不超过严格需要的可见性被认为是良好的做法。

在这种情况下,大多数人不会将赌注设置为字段,而是将其作为参数传递给彩票方法。在这种情况下,可见性甚至可以进一步仅限于游戏和彩票方法。

最后一点是您使用函数调用来循环。由于java不支持尾部调用优化(并且在任何情况下都不适用于此处),如果您玩得足够长,您将填满堆栈并最终死于堆栈溢出。

If you define bet as a field in the class like :

public class Lottery {
    private int bet;

    public void lottery() {
        ...
    }

    public void play() {
        ...
    }

    public static void main(String[] args) {
        Lottery lottery = new Lottery();
        lottery.play();
    }
}

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.

铁憨憨 2024-09-25 06:21:08

将其作为参数在方法之间传递。 (不建议将其定义为 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文