Android: Randomly generated arithmetic arithmetic simple demo (random generation of the number of the group of numbers, for subtraction operation)

Source: Internet
Author: User

First create a new Android project, the following is the page layout:

Java code:

Let's start by analyzing how to complete the steps:

1. First, the generated random number is completed first. (including randomly generating several sets of numbers, the number of ranges, arithmetic symbols, etc.);

2. To complete the specific logic, first specify a number of randomly generated numbers, and then arrange the numbers and arithmetic symbols

The following code generates a random number for each case that only writes one:

private void question () {
Java.util.Random random=new java.util.Random ();
The first is to randomly generate several sets of numbers (--);
int temp= Random.nextint (3) +2;

Random Generation (0~3) four numbers correspond: +,-,x,÷
If four numbers are randomly set, there will be three operators
int Signal=random.nextint (4);//First operator
int Signal1=random.nextint (4);//second operator
int Signal2=random.nextint (4);//Third operator

The value of each set of numbers randomly generated (1~11)
Final int Num1=random.nextint (11) +1;
Final int Num2=random.nextint (11) +1;
Final int Num3=random.nextint (11) +1;
Final int Num4=random.nextint (11) +1;

Switch (temp) {
Case 2://2 Group number
Switch (signal) {
Case 0://+
Tv_math.settext (num1+ "+" +num2+ "=?");
Btn_answers.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
Tv_math.settext (num1+ "+" +num2+ "=" + (num1+num2));
}
});
Break
Case 1://-
//......
Break
Case 2://x
//......
Break
Case 3://÷
//......
Break

}
Break
Case 3://3 Group number
Switch (signal) {//First operator
Case 0://+
Switch (SIGNAL1) {//second operator
Case 0://+
Tv_math.settext (num1+ "+" +num2+ "+" +num3+ "=?");
Btn_answers.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
Tv_math.settext (num1+ "+" +num2+ "+" +num3+ "=" + (NUM1+NUM2+NUM3) ");
}
});
Break
//......
}
Break
//......
}
Break
Case 4://4 Group number
Switch (signal) {//First operator
Case 0://+
Switch (SIGNAL1) {//second operator
Case 0://+
Switch (SIGNAL2) {//third operator
Case 0://+
Tv_math.settext (num1+ "+" +num2+ "+" +num3+ "+" +num4+ "=?");
Btn_answers.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
Tv_math.settext (num1+ "+" +num2+ "+" +num3+ "+" +num4+ "=" + (NUM1+NUM2+NUM3+NUM4) ");
}
});
Break
//......
}
//......
}
Break
//......
}
Break
}
}

The following results are attached:

Because of the shallow knowledge, the written code is also miscellaneous. Since the result of the last return is an integral type, there will be no decimals when there is no division, the integer is returned, and it has not been modified. If there is a mistake also hope understanding!

Android: Randomly generated arithmetic arithmetic simple demo (random generation of the number of the group of numbers, for subtraction operation)

Related Article

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.