Reputation: 11
I am a highschool student in an independent studies class and i have absolutly no clue what i am doing with eclipse, or java for that matter. I am trying to make an app that gives a math problem, allows you to answer, and moves on. It will show a toast notification if you got it right our wrong but move on no matter what. I cant figure out how to get it to refresh all the values once you submit your answer and it tells you if its right or wrong. i assume i need some type of loop to continuessly go through the code until you close the app, but i dont know what that should look like. Can someone help me?
package com.example.droidmaze;
import java.util.Random;
import android.os.Bundle;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class App extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app);
final Button sub=(Button) findViewById(R.id.button1);
final EditText answer=(EditText) findViewById(R.id.editText1);
final TextView messages=(TextView) findViewById(R.id.textView1);
int number1=0;
int number2=0;
Random randomGenerator = new Random();
for (int idx = 1; idx <= 10; ++idx){
int randomInt = randomGenerator.nextInt(10);
number1 += randomInt;
}
Random randomGenerator2 = new Random();
for (int idx = 1; idx <= 10; ++idx){
int randomInt = randomGenerator2.nextInt(10);
number2 += randomInt;
}
String message = number1 + "\n + \n" + number2;
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
final String sum = ""+ (number1 + number2);
messages.setText(message);
sub.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String user_answer=answer.getText().toString();
if (sum.equals(user_answer)){
Toast.makeText(getApplicationContext(), "Congrats!",
Toast.LENGTH_LONG).show();
}
if (sum.equals(user_answer)){
Toast.makeText(getApplicationContext(), "Thats not quite right!",
Toast.LENGTH_LONG).show();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.app, menu);
return true;
}
}
Upvotes: 1
Views: 88
Reputation: 11254
It can be done at the following way:
public class MyActivity extends Activity {
private Button sub;
private EditText answer;
private TextView messages;
private String sum;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app);
sub = (Button) findViewById(R.id.button1);
answer = (EditText) findViewById(R.id.editText1);
messages = (TextView) findViewById(R.id.textView1);
generateNumbers();
sub.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String user_answer = answer.getText().toString();
String toastMessage = "";
if (sum.equals(user_answer)) {
toastMessage = "Congrats!";
} else {
toastMessage = "Thats not quite right!";
}
Toast.makeText(getApplicationContext(), toastMessage ,
Toast.LENGTH_LONG).show();
generateNumbers();
}
});
}
private void generateNumbers() {
int number1 = 0;
int number2 = 0;
Random randomGenerator = new Random();
for (int idx = 1; idx <= 10; ++idx) {
int randomInt = randomGenerator.nextInt(10);
number1 += randomInt;
}
Random randomGenerator2 = new Random();
for (int idx = 1; idx <= 10; ++idx) {
int randomInt = randomGenerator2.nextInt(10);
number2 += randomInt;
}
String message = number1 + "\n + \n" + number2;
messages.setText(message);
sum = String.valueOf(number1 + number2);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.app, menu);
return true;
}
}
What's the logic behind it? Well, all the code for numbers generation has moved to another method - Don't Repeat Yourself
rule. We start this Activity
, find the Views
, generate numbers. At the onClickListener
we are checking entered value, if it's right, than we are displaying one message, if it's not - another message, and in any case generate new numbers. Also you can use variables at the inner classes if they are the fields of an outer class, final
modifier is not needed in such a case.
Upvotes: 1