获取随机数的计算以与EditText进行比较

时间:2017-01-21 14:06:44

标签: android android-edittext

最近,我一直在尝试如何计算MainActivity中显示的两个随机数,以便稍后与文本字段进行比较,并指出插入的数字是正确还是否。

这里是代码:

MainActivity.java

package com.example.miguel.myfirstapp;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Random;

public class MainActivity extends AppCompatActivity {
    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
    Random random = new Random();
    int randomNum = random.nextInt(101);
    int randomNum2 = random.nextInt(101);
    String messageRandomNum = String.valueOf(randomNum);
    String messageRandomNum2 = String.valueOf(randomNum2);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView textViewRandomNum = (TextView) findViewById(R.id.random_num);
    textViewRandomNum.setText(messageRandomNum);
    messageRandomNum2 = String.valueOf(randomNum2+ "= ");
    TextView textViewRandomNum2 = (TextView) findViewById(R.id.random_num2);
    textViewRandomNum2.setText(messageRandomNum2);

}

public int getCalculation(){
    //Here is where I had tried to get the calculation of the same numbers that are shown in the screen
    int calculation = randomNum + randomNum2;
    return calculation;
}

public void sendMessage (View view){
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.edit_message);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}

}

DisplayMessageActivity.java

package com.example.miguel.myfirstapp;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

public class DisplayMessageActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_message);

        MainActivity mainActivity = new MainActivity();

        Intent intent = getIntent();

        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
        int messageInt = Integer.parseInt(message);

        TextView textView = new TextView(this);
        textView.setTextSize(40);

        //The comparison of editText and the calculation
        if(messageInt == mainActivity.getCalculation()){
            textView.setText("Correct!");
        }else{
            textView.setText("Incorrect!");
        }

        ViewGroup layout = (ViewGroup) findViewById(R.id.activity_display_message);
        layout.addView(textView);
    }
}

始终显示插入的数字(尽管很好)显示“不正确!”。我不知道为什么计算会得到随机数的总和,而不是随机数而不是屏幕上显示的数字。

如果你不明白,抱歉,我是西班牙语演讲者。

问候! ;)

2 个答案:

答案 0 :(得分:0)

代码中的错误是您在DisplayMessageActivity中从MainActivity创建对象。此对象与调用DisplayMessageActivity并且没有随机数的活动不同。您必须发送意向下一个活动的随机数,例如EXTRA_MESSAGE。

答案 1 :(得分:0)

在MainActivity中,您可以发送计算结果

public void sendMessage (View view){
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText editText = (EditText) findViewById(R.id.edit_message);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        int calcualtedResult=getCalculation();
         intent.putInt("calcualtedResult", calcualtedResult);
        startActivity(intent);
    }

在DisplayMessageActivity中

你可以获得意图

           int calculatedRes=intent.getInt("calculatedResult");


            if(messageInt == calculatedRes){
                textView.setText("Correct!");
            }else{
                textView.setText("Incorrect!");
            }