Elementary school arithmetic mental arithmetic practice app---No.3

Source: Internet
Author: User

Today, the main issue is to achieve the number of questions according to the assigned topic. On the basis of setting up the page yesterday, the first thing to learn today is to receive the parameter problems between different pages. Details are as follows:

Then start my argument and receive parameter questions!

To jump on the current activity,

The code is as follows:

Package com.example.mmmjh.calculator;

Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.app.AlertDialog;
Import android.content.Intent;
Import Android.widget.TextView;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.EditText;
Import Android.content.DialogInterface;
Import Android.widget.Toast;

public class Activity_calculators extends Appcompatactivity {
Private Button begin;
EditText b,min,sec;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_calculators);
Begin= (Button) Findviewbyid (R.id.button1);

b= (EditText) Findviewbyid (R.ID.EDITTEXT2);
Min= (EditText) Findviewbyid (R.ID.EDITTEXT3);
Sec= (EditText) Findviewbyid (R.ID.EDITTEXT4);
Begin.setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View arg0) {
TODO auto-generated Method Stub
Intent intent=new Intent ();
String Tishu=b.gettext (). toString ();//Gets the value entered

Bundle bundle = new bundle ();
Bundle.putstring ("Tishu", Tishu);
Intent.putextras (bundle);//Pass the number of topics to the next page this is a method
          
Intent.setclass (Activity_calculators.this, calculatoractivity.class);//Jump
StartActivity (Intent);
Activity_calculators.this.finish ();
}
}


The Java code for the target page of the jump:
Calculatoractivity.class:

Package com.example.mmmjh.calculator;

Import Java.util.Random;

Import Android.os.Bundle;
Import android.app.Activity;
Import Android.view.Menu;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.EditText;
Import Android.widget.TextView;
Import Android.widget.Toast;
Import android.content.Intent;
public class Calculatoractivity extends Activity {
Private random num1=new random ();//Stochastic number generation
Private random num2=new random ();//Stochastic number generation
Private random R = new Random ();
Private char[] ch = {' + ', '-', ' x ', ' ÷ '}; Character array
private int index = R.nextint (ch.length); Random number, less than the length of the array, 0~3
Private char a=ch[index];//get operation symbol

Private TextView text1,text2,text3;
Private EditText answer;
Private button surebutton;//OK button
private int i1,i2,i3;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_calculator);

Intent Intent = Getintent ();
Bundle bundle = Intent.getextras ();
String Tishu = bundle.getstring ("Tishu");//receive the number of topics passed over
int i=0;
try {
i = Integer.parseint (Tishu);//i here is the number of topics in the Shaping value
} catch (NumberFormatException e) {
E.printstacktrace ();
}

text1= (TextView) Findviewbyid (R.ID.TEXTVIEW1);//random numbers
Text2= (TextView) Findviewbyid (R.ID.TEXTVIEW2);//Operation symbol
text3= (TextView) Findviewbyid (R.ID.TEXTVIEW3);//random numbers
Answer= (EditText) Findviewbyid (R.ID.EDITTEXT1);//Operation result
String c=string.valueof (Num1.nextint (100));
I2=integer.valueof (c);//convert the resulting values into plastic
String d=string.valueof (a);//Operator
String e=string.valueof (Num2.nextint (100));
I3=integer.valueof (e);//convert the resulting values into plastic

while ((D.equals ("÷") &&i2%i3!=0) | | (D.equals ("-") &&i2<i3) | | (D.equals ("x") && (I2*I3) >100) | | (D.equals ("+") && (I2+I3) >100))
{
text1= (TextView) Findviewbyid (R.ID.TEXTVIEW1);//random numbers
text3= (TextView) Findviewbyid (R.ID.TEXTVIEW3);//random numbers
C=string.valueof (Num1.nextint (100));
I2=integer.valueof (c);//convert the resulting values into plastic
E=string.valueof (Num2.nextint (100));
I3=integer.valueof (e);//convert the resulting values into plastic
}


Text1.settext (c);//random number 1-100 assign them a value
Text2.settext (d);//random operator +,-, *,/
Text3.settext (e);//random number 1-100 assignment is displayed on the phone
surebutton= (Button) Findviewbyid (R.id.surebutton);//OK button

Surebutton.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View arg0) {
TODO auto-generated Method Stub
Final String B=answer.gettext (). toString ();//Gets the value entered
I1=integer.valueof (b);//convert the acquired value into an orthopedic

Switch (index) {//index is the one that determines which operator in the array
Case 0:
{
if (i1== (I2+I3))
Toast.maketext (Calculatoractivity.this, "correct, the answer is" +b, Toast.length_short). Show ();
Else
Toast.maketext (Calculatoractivity.this, "error, correct answer for" + (I3+I2), Toast.length_short). Show ();
Break
}
Case 1:
{
if (i1== (I2-I3))
Toast.maketext (Calculatoractivity.this, "correct, the answer is" +b, Toast.length_short). Show ();
Else
Toast.maketext (Calculatoractivity.this, "error, correct answer for" + (I2-I3), Toast.length_short). Show ();
Break
}
Case 2:{
if (i1== (I2*I3))
Toast.maketext (Calculatoractivity.this, "correct, the answer is" +b, Toast.length_short). Show ();
Else
Toast.maketext (Calculatoractivity.this, "error, correct answer for" + (I2*I3), Toast.length_short). Show ();
Break
}
Case 3:
{
if (i3!=0) {
if (i1== (double) i2/(double) i3))
Toast.maketext (Calculatoractivity.this, "correct, the answer is" +b, Toast.length_short). Show ();
Else
Toast.maketext (Calculatoractivity.this, "error, correct answer for" + (double) i2/(double) i3), Toast.length_short). Show ();
}
Break
}
}
}
});

}

@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.calculator, menu);
return true;
}

}

At present can only do this effect, but also to change the position of the horizontal line!

Elementary school arithmetic mental arithmetic practice app---No.3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.