Simple Math Game?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I am new in Android and I would like to create simple Math quiz. I have a one textview that I display random question with random operator as below code.I would like, user will input their answer to EditText and submit their answer with ImageButton that I called submit answer. My question is, I could not handle to check user answer on Edittext via different method.How can I check user answer that evaluate the answer after submitbutton ?
public class MainActivity extends AppCompatActivity
int number1, number2, result;
public EditText answer;
char operator;
ImageButton submitAnswer;
Random rand = new Random();
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Random rnd = new Random();
number1 = rnd.nextInt(100) + 1;
number2 = rnd.nextInt(100) + 1;
generateOperator();
TextView question = findViewById(R.id.questionText);
question.setText(number1 + " " + operator + " " + number2 + " " + "=" + " " + "?");
public int generateOperator()
int op = rand.nextInt(3) + 1;
if (op == 1)
operator = '+';
result = number1+number2;
else if (op == 2)
operator = '-';
result= number1-number2;
else if (op == 3)
operator = '*';
result = number1+number2;
return operator;
public void submitAnswer(View view)
submitAnswer = findViewById(R.id.submitButton);
submitAnswer.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
if ( result == Integer.valueOf(answer.getText().toString()))
Toast.makeText(view.getContext(), "Correct",
Toast.LENGTH_SHORT).show();
else
Toast.makeText(view.getContext(), "Wrong",
Toast.LENGTH_SHORT).show();
);
android
add a comment |
I am new in Android and I would like to create simple Math quiz. I have a one textview that I display random question with random operator as below code.I would like, user will input their answer to EditText and submit their answer with ImageButton that I called submit answer. My question is, I could not handle to check user answer on Edittext via different method.How can I check user answer that evaluate the answer after submitbutton ?
public class MainActivity extends AppCompatActivity
int number1, number2, result;
public EditText answer;
char operator;
ImageButton submitAnswer;
Random rand = new Random();
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Random rnd = new Random();
number1 = rnd.nextInt(100) + 1;
number2 = rnd.nextInt(100) + 1;
generateOperator();
TextView question = findViewById(R.id.questionText);
question.setText(number1 + " " + operator + " " + number2 + " " + "=" + " " + "?");
public int generateOperator()
int op = rand.nextInt(3) + 1;
if (op == 1)
operator = '+';
result = number1+number2;
else if (op == 2)
operator = '-';
result= number1-number2;
else if (op == 3)
operator = '*';
result = number1+number2;
return operator;
public void submitAnswer(View view)
submitAnswer = findViewById(R.id.submitButton);
submitAnswer.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
if ( result == Integer.valueOf(answer.getText().toString()))
Toast.makeText(view.getContext(), "Correct",
Toast.LENGTH_SHORT).show();
else
Toast.makeText(view.getContext(), "Wrong",
Toast.LENGTH_SHORT).show();
);
android
Did you tryanswer.getText().toString()
inside your OnClickListener ?
– Whole Brain
Nov 15 '18 at 11:24
add a comment |
I am new in Android and I would like to create simple Math quiz. I have a one textview that I display random question with random operator as below code.I would like, user will input their answer to EditText and submit their answer with ImageButton that I called submit answer. My question is, I could not handle to check user answer on Edittext via different method.How can I check user answer that evaluate the answer after submitbutton ?
public class MainActivity extends AppCompatActivity
int number1, number2, result;
public EditText answer;
char operator;
ImageButton submitAnswer;
Random rand = new Random();
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Random rnd = new Random();
number1 = rnd.nextInt(100) + 1;
number2 = rnd.nextInt(100) + 1;
generateOperator();
TextView question = findViewById(R.id.questionText);
question.setText(number1 + " " + operator + " " + number2 + " " + "=" + " " + "?");
public int generateOperator()
int op = rand.nextInt(3) + 1;
if (op == 1)
operator = '+';
result = number1+number2;
else if (op == 2)
operator = '-';
result= number1-number2;
else if (op == 3)
operator = '*';
result = number1+number2;
return operator;
public void submitAnswer(View view)
submitAnswer = findViewById(R.id.submitButton);
submitAnswer.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
if ( result == Integer.valueOf(answer.getText().toString()))
Toast.makeText(view.getContext(), "Correct",
Toast.LENGTH_SHORT).show();
else
Toast.makeText(view.getContext(), "Wrong",
Toast.LENGTH_SHORT).show();
);
android
I am new in Android and I would like to create simple Math quiz. I have a one textview that I display random question with random operator as below code.I would like, user will input their answer to EditText and submit their answer with ImageButton that I called submit answer. My question is, I could not handle to check user answer on Edittext via different method.How can I check user answer that evaluate the answer after submitbutton ?
public class MainActivity extends AppCompatActivity
int number1, number2, result;
public EditText answer;
char operator;
ImageButton submitAnswer;
Random rand = new Random();
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Random rnd = new Random();
number1 = rnd.nextInt(100) + 1;
number2 = rnd.nextInt(100) + 1;
generateOperator();
TextView question = findViewById(R.id.questionText);
question.setText(number1 + " " + operator + " " + number2 + " " + "=" + " " + "?");
public int generateOperator()
int op = rand.nextInt(3) + 1;
if (op == 1)
operator = '+';
result = number1+number2;
else if (op == 2)
operator = '-';
result= number1-number2;
else if (op == 3)
operator = '*';
result = number1+number2;
return operator;
public void submitAnswer(View view)
submitAnswer = findViewById(R.id.submitButton);
submitAnswer.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
if ( result == Integer.valueOf(answer.getText().toString()))
Toast.makeText(view.getContext(), "Correct",
Toast.LENGTH_SHORT).show();
else
Toast.makeText(view.getContext(), "Wrong",
Toast.LENGTH_SHORT).show();
);
android
android
edited Nov 15 '18 at 13:41
Fantômas
32.9k156491
32.9k156491
asked Nov 15 '18 at 11:19
Semih UralSemih Ural
109
109
Did you tryanswer.getText().toString()
inside your OnClickListener ?
– Whole Brain
Nov 15 '18 at 11:24
add a comment |
Did you tryanswer.getText().toString()
inside your OnClickListener ?
– Whole Brain
Nov 15 '18 at 11:24
Did you try
answer.getText().toString()
inside your OnClickListener ?– Whole Brain
Nov 15 '18 at 11:24
Did you try
answer.getText().toString()
inside your OnClickListener ?– Whole Brain
Nov 15 '18 at 11:24
add a comment |
1 Answer
1
active
oldest
votes
First of all, edir your generateOperator()
method to keep answer.
public int generateOperator()
int op = rand.nextInt(3) + 1;
if (op == 1)
operator = '+';
result = number1 + number2;
else if (op == 2)
operator = '-';
result = number1 - number2;
else if (op == 3)
operator = '*';
result = number1 * number2;
return operator;
And then you can simply compare your result and the user's answer.
submitAnswer.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
if(result == Integer.valueOf(answer.getText().toString()))
//Answer is ok.
else
//Some code...
);
I did it.. But I got an crash when I use submitbutton.
– Semih Ural
Nov 15 '18 at 11:47
I update my latest code.
– Semih Ural
Nov 15 '18 at 11:48
Paste your error log pls.
– Cagri Yalcin
Nov 15 '18 at 11:49
AddEdittext
binding.answer = findViewById(R.id.myEditText);
– Cagri Yalcin
Nov 15 '18 at 11:49
1
It works appreciate.
– Semih Ural
Nov 15 '18 at 12:06
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318316%2fsimple-math-game%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
First of all, edir your generateOperator()
method to keep answer.
public int generateOperator()
int op = rand.nextInt(3) + 1;
if (op == 1)
operator = '+';
result = number1 + number2;
else if (op == 2)
operator = '-';
result = number1 - number2;
else if (op == 3)
operator = '*';
result = number1 * number2;
return operator;
And then you can simply compare your result and the user's answer.
submitAnswer.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
if(result == Integer.valueOf(answer.getText().toString()))
//Answer is ok.
else
//Some code...
);
I did it.. But I got an crash when I use submitbutton.
– Semih Ural
Nov 15 '18 at 11:47
I update my latest code.
– Semih Ural
Nov 15 '18 at 11:48
Paste your error log pls.
– Cagri Yalcin
Nov 15 '18 at 11:49
AddEdittext
binding.answer = findViewById(R.id.myEditText);
– Cagri Yalcin
Nov 15 '18 at 11:49
1
It works appreciate.
– Semih Ural
Nov 15 '18 at 12:06
add a comment |
First of all, edir your generateOperator()
method to keep answer.
public int generateOperator()
int op = rand.nextInt(3) + 1;
if (op == 1)
operator = '+';
result = number1 + number2;
else if (op == 2)
operator = '-';
result = number1 - number2;
else if (op == 3)
operator = '*';
result = number1 * number2;
return operator;
And then you can simply compare your result and the user's answer.
submitAnswer.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
if(result == Integer.valueOf(answer.getText().toString()))
//Answer is ok.
else
//Some code...
);
I did it.. But I got an crash when I use submitbutton.
– Semih Ural
Nov 15 '18 at 11:47
I update my latest code.
– Semih Ural
Nov 15 '18 at 11:48
Paste your error log pls.
– Cagri Yalcin
Nov 15 '18 at 11:49
AddEdittext
binding.answer = findViewById(R.id.myEditText);
– Cagri Yalcin
Nov 15 '18 at 11:49
1
It works appreciate.
– Semih Ural
Nov 15 '18 at 12:06
add a comment |
First of all, edir your generateOperator()
method to keep answer.
public int generateOperator()
int op = rand.nextInt(3) + 1;
if (op == 1)
operator = '+';
result = number1 + number2;
else if (op == 2)
operator = '-';
result = number1 - number2;
else if (op == 3)
operator = '*';
result = number1 * number2;
return operator;
And then you can simply compare your result and the user's answer.
submitAnswer.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
if(result == Integer.valueOf(answer.getText().toString()))
//Answer is ok.
else
//Some code...
);
First of all, edir your generateOperator()
method to keep answer.
public int generateOperator()
int op = rand.nextInt(3) + 1;
if (op == 1)
operator = '+';
result = number1 + number2;
else if (op == 2)
operator = '-';
result = number1 - number2;
else if (op == 3)
operator = '*';
result = number1 * number2;
return operator;
And then you can simply compare your result and the user's answer.
submitAnswer.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
if(result == Integer.valueOf(answer.getText().toString()))
//Answer is ok.
else
//Some code...
);
answered Nov 15 '18 at 11:27
Cagri YalcinCagri Yalcin
325413
325413
I did it.. But I got an crash when I use submitbutton.
– Semih Ural
Nov 15 '18 at 11:47
I update my latest code.
– Semih Ural
Nov 15 '18 at 11:48
Paste your error log pls.
– Cagri Yalcin
Nov 15 '18 at 11:49
AddEdittext
binding.answer = findViewById(R.id.myEditText);
– Cagri Yalcin
Nov 15 '18 at 11:49
1
It works appreciate.
– Semih Ural
Nov 15 '18 at 12:06
add a comment |
I did it.. But I got an crash when I use submitbutton.
– Semih Ural
Nov 15 '18 at 11:47
I update my latest code.
– Semih Ural
Nov 15 '18 at 11:48
Paste your error log pls.
– Cagri Yalcin
Nov 15 '18 at 11:49
AddEdittext
binding.answer = findViewById(R.id.myEditText);
– Cagri Yalcin
Nov 15 '18 at 11:49
1
It works appreciate.
– Semih Ural
Nov 15 '18 at 12:06
I did it.. But I got an crash when I use submitbutton.
– Semih Ural
Nov 15 '18 at 11:47
I did it.. But I got an crash when I use submitbutton.
– Semih Ural
Nov 15 '18 at 11:47
I update my latest code.
– Semih Ural
Nov 15 '18 at 11:48
I update my latest code.
– Semih Ural
Nov 15 '18 at 11:48
Paste your error log pls.
– Cagri Yalcin
Nov 15 '18 at 11:49
Paste your error log pls.
– Cagri Yalcin
Nov 15 '18 at 11:49
Add
Edittext
binding. answer = findViewById(R.id.myEditText);
– Cagri Yalcin
Nov 15 '18 at 11:49
Add
Edittext
binding. answer = findViewById(R.id.myEditText);
– Cagri Yalcin
Nov 15 '18 at 11:49
1
1
It works appreciate.
– Semih Ural
Nov 15 '18 at 12:06
It works appreciate.
– Semih Ural
Nov 15 '18 at 12:06
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318316%2fsimple-math-game%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Did you try
answer.getText().toString()
inside your OnClickListener ?– Whole Brain
Nov 15 '18 at 11:24