【问题标题】:Cannot resolve methods in Android app for game of craps无法解析 Android 应用程序中的掷骰子游戏方法
【发布时间】:2016-01-14 18:30:03
【问题描述】:

我正在创建一个简单的掷骰子应用程序。
我收到此错误:

无法解析我的 nextInt() 方法和 valueOf() 方法的方法。

我已将myResults 变量声明为Integer,所以我不确定为什么会收到此错误。目前,应用程序应该掷骰子并返回值。在解决此错误之前,我无法处理其余的逻辑。我想确保在继续之前我至少可以运行这部分。最终目标是掷两个骰子并返回两者的总和,但我目前只编写了一个掷骰子的代码。有人可以指出我正确的方向吗?

public class MainActivity extends Activity {

        // my variables
        TextView mText;
        ImageButton mButton;
        int myResults;
        String output;

        // function that runs on Start Up
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            mText = (TextView)findViewById(R.id.results);
            mButton = (ImageButton)findViewById(R.id.roll);

            mButton.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View v)
                {
                    myResults = new Random(System.currentTimeMillis().nextInt(6)+1);
                    rollDice();
                    mText.setText(String.valueOF(myResults));
                }
            });
        }

        public void rollDice()
        {
            switch (myResults)
            {
                case 1:
                    mButton.setImageResource(R.drawable.die1);
                    break;
                case 2:
                    mButton.setImageResource(R.drawable.die2);
                    break;
                case 3:
                    mButton.setImageResource(R.drawable.die3);
                    break;
                case 4:
                    mButton.setImageResource(R.drawable.die4);
                    break;
                case 5:
                    mButton.setImageResource(R.drawable.die5);
                    break;
                case 6:
                    mButton.setImageResource(R.drawable.die6);
                    break;
            }
        }

一旦这个问题得到解决,我想要一些关于将第二个骰子计入 myResults 的指导。但是先解决方法会很棒。

错误:

myResults = new Random(System.currentTimeMillis().nextInt(6)+1);
rollDice();
mText.setText(String.valueOF(myResults));

【问题讨论】:

    标签: java android methods


    【解决方案1】:

    你把)放错了地方。

    myResults = new Random(System.currentTimeMillis().nextInt(6)+1);
    

    应该是

    myResults = new Random(System.currentTimeMillis()).nextInt(6)+1;
    

    【讨论】:

      【解决方案2】:

      System.currentTimeMillis() 返回一个 long,它是一个原语,因此没有任何成员 nextInt(...)

      valueOF()

      在一些相关的注释中,我不会在活动代码中包含骰子逻辑。遵循 OOP 和 MVC 架构原则的更好方法是将骰子逻辑提取到单独的类中并从活动中调用。

      【讨论】:

        【解决方案3】:

        String.valueOf() 在您的代码中大写错误 (String.valueOF())。

        您还错误地使用了Random nextInt() 方法。 System.currentTimeInMillis() 返回一个 long 值,而 nextInt() 需要一个 Random 对象。使用nextInt()的正确方法已在此question的答案中演示。

        Random r = new Random();
        int i1 = r.nextInt(80 - 65) + 65;
        

        【讨论】:

          【解决方案4】:

          问题是,

          第一:

          myResults = new Random(System.currentTimeMillis()).nextInt(6)+1;
          

          第二:

          mText.setText(String.valueOf(myResults));
          

          试试这个方法。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-12-24
            • 2012-02-29
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-08-02
            相关资源
            最近更新 更多