Simple Android Arithmetic

Source: Internet
Author: User

Layout interface:

Code for the layout interface:

Java code:

The answer is displayed when you click the button:
public class Mainactivity extends Appcompatactivity {
private int a,b,c,ty;
Private TextView TV;
Private Button Show,next;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
tv= (TextView) Findviewbyid (R.ID.TEXTVIEW2);
Show = (Button) Findviewbyid (R.id.button1);
Next = (Button) Findviewbyid (R.id.button2);
SZ ();
SW ();
Show answers when you click the Answer button
Show.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
Switch (ty) {
Case 0:
Tv.settext (A + "+" + B + "=" + (A + b));
Break
Case 1:
Tv.settext (A + "-" + B + "=" + (a));
Break
Case 2:
Tv.settext (A + "+" + B + "*" + c + "=" + (a+b*c));
Break
Case 3:
Tv.settext (A + "+" + B + "/" + c + "=" + (a+b/c));
Break
Case 4:
Tv.settext (A + "-" + B + "*" + c + "=" + (a-b*c));
Break
Case 5:
Tv.settext (A + "-" + B + "/" + c + "=" + (a-b/c));
Break
Case 6:
Tv.settext (A + "*" + B + "=" + (a*b));
Break
Case 7:
Tv.settext (A + "/" + B + "=" + (A/b));
Break
}
}
});

Click to display the next question:

Click on the question
Next.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
SZ ();
SW ();
}
});
}

Generate random Number:

Generate random numbers
private void sz () {
Random random = new random ();
Random.nextint ();
A = Random.nextint (100);
b = random.nextint (100);
c = random.nextint (100);
ty = Random.nextint (8);
}

Show Title:

//Display title 
private void SW () {
switch (ty) {
Case 0:
Tv.settext (A + ") + "+ B +" = "+"? ");
break;
Case 1:
Tv.settext (A + "-" + B + "=" + "?");
break;
Case 2:
Tv.settext (A + "+" + B + "*" + c + "=" + "?");
break;
Case 3:
Tv.settext (A + "+" + B + "/" + c + "=" + "?");
break;
Case 4:
Tv.settext (A + "-" + B + "*" + c + "=" + "?");
break;
Case 5:
Tv.settext (A + "-" + B + "/" + c + "=" + "?");
break;
Case 6:
Tv.settext (A + "*" + B + "=" + "?");
break;
Case 7:
Tv.settext (A + "/" + B + "=" + "?");
break;
}
}
}

Simple Android Arithmetic

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.