Defective Android arithmetic

Source: Internet
Author: User

Java files:

Package com.example.dell_pc.homework;

Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.TextView;

Import Java.util.Random;

public class Mainactivity extends Appcompatactivity {
private int A;
private int B;
private int C;
private int D;
private int sy;
Private TextView TV;
Private Button btn_1;
Private Button btn_2;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Get Textview,button Object
TV = (TextView) Super.findviewbyid (R.ID.TEXTVIEW5);
btn_1 = (Button) Super.findviewbyid (r.id.btn_1);
Btn_2 = (Button) Super.findviewbyid (r.id.btn_2);
Random ();
View ();

Btn_2.setonclicklistener (New Button.onclicklistener () {
@Override

Algorithm
public void OnClick (View v) {

Switch (SY) {
Case 1:
Tv.settext (A + "+" + B + "-" + C + "*" +d+ "=?");
Break
Case 2:
Tv.settext (A + "-" + B + "*" + C + "/" + d+ "=?");
Break
Case 3:
Tv.settext (A + "*" + B + "+" + C + "-" + d+ "=?");
Break
Case 4:
Tv.settext (A + "/" + B + "-" + C + "+" + d+ "=?");
Break
Case 5:
Tv.settext (A + "+" + B + "/" + C + "+" + d+ "=?");
Break
Case 6:
Tv.settext (A + "+" + B + "/" + C + "+" + d+ "=?");
Break
Case 7:
Tv.settext (A + "-" + B + "/" + C + "*" + d+ "=?");
Break
}
}

});
Event Monitoring
Btn_1.setonclicklistener (New View.onclicklistener () {
public void OnClick (View v) {
Random ();
View ();
}
});
}

Generate a random number within 100
private void random () {
A = new Random (). Nextint (100);
b = new Random (). Nextint (100);
c = new Random (). Nextint (100);
sy = new Random (). Nextint (4);
}

private void View () {
Switch (SY) {
Case 1:
Tv.settext (A + "+" + B + "-" + C + "*" +d+ "=" + (a+b-c*d));
Break
Case 2:
Tv.settext (A + "-" + B + "*" + C + "/" + d+ "=" + (A-B*C/D));
Break
Case 3:
Tv.settext (A + "*" + B + "+" + C + "-" + d+ "=" + (a*b+c-d));
Break
Case 4:
Tv.settext (A + "/" + B + "-" + C + "+" + d+ "=" + (a/b-c+d));
Break
Case 5:
Tv.settext (A + "+" + B + "/" + C + "+" + d+ "=" + (a+b/c+d));
Break
Case 6:
Tv.settext (A + "+" + B + "/" + C + "+" + d+ "=" + (a+b/c+d));
Break
Case 7:
Tv.settext (A + "-" + B + "/" + C + "*" + d+ "=" + (a-b/c*d));
Break
}
}
}

Layout file:

<?xml version= "1.0" encoding= "Utf-8"?>
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:id= "@+id/activity_main"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
tools:context= "Com.example.dell_pc.homework.MainActivity" >


<linearlayout
android:orientation= "Horizontal"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:layout_below= "@+id/textview5"
Android:layout_alignparentleft= "true"
Android:layout_alignparentstart= "true"
android:layout_margintop= "108DP" >

<button
android:text= "Next Question"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:id= "@+id/btn_1"
android:layout_weight= "1"/>

<button
android:text= "Answer"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:id= "@+id/btn_2"
android:layout_weight= "1"/>
</LinearLayout>

<textview
android:text= "TextView"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:id= "@+id/textview5"
Android:layout_alignparenttop= "true"
Android:layout_centerhorizontal= "true"
android:layout_margintop= "38DP"/>
</RelativeLayout>

Defective 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.