简体   繁体   English

是否可以在不同的对象中使用单个输入?

[英]Is it possible to use a single input in different objects?

The both codes below are from different class files.下面的两个代码来自不同的 class 文件。

I'm trying to use the input from spinWheels for pull.我正在尝试使用 spinWheels 的输入进行拉动。

Is that possible?那可能吗?

public int spinWheels(int betAmt)
{
    String[] aWheel = new String[5];
    Scanner kb = new Scanner(System.in);
    betAmt = kb.nextInt();
}


public void pull(int bet)
{
    betAmt = bet;
    totalcoins = totalcoins - bet;
}

You mean like this?你的意思是这样吗?

int x = 0;
spinWheels(x);
pull(x);

It is possible but there are several things that you must do first.这是可能的,但您必须首先做几件事。 Say you have a class A:假设您有一个 class A:

class A{
    public int spinWheels(int betAmt)
    {
        String[] aWheel = new String[5];
        Scanner kb = new Scanner(System.in);
        return kb.nextInt(); //I assume you need this value later
        //you must return an int here
     }
}

and you have a second class B:你有第二个 class B:

 class B{
     public void pull()
     {
       //you need a reference to an Object of type A
       A a = new A();       
       bet = a.spinWheels();
       totalcoins = totalcoins - bet;
     }
 }

Of course the above code is very simplistic, without knowing what you want to achieve it is not possible to say more.当然上面的代码很简单,不知道要实现什么就不能多说了。

You could just use你可以使用

public int spinWheels()
{
    String[] aWheel = new String[5];
    Scanner kb = new Scanner(kb.nextInt());
    ...
}

I don't understand why you'd want to pass betAmt as an argument if you're going to run it over anyway...我不明白你为什么要通过 betAmt 作为参数,如果你无论如何都要运行它......

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

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