Randomly generate two-digit exercise questions

Source: Internet
Author: User

You can use the random () method in the math class to generate a value ranging from 0 ~ Random number between 1. This method is relatively simple. To become a requirement, the random class is added to the java. util package. It is a random number generator that can be initialized with 48 digits (seed, also called SEED.

For example:

Long seed = calendar. getinstance (). gettimeinmillis (); // when using the random class, you can use time-related values as random seeds.
Objrandom = new random (SEED );

Example:

Five double-digit exercise questions are randomly generated for primary school students.

Import java. util .*;
Public class genquestion
{

Random objrandom;
Public genquestion ()
{
Long seed = calendar. getinstance (). gettimeinmillis ();
Objrandom = new random (SEED );
}
Private void geneandprint ()
{
Stringbuffer buff = new stringbuffer ();
Buff. append ("the answer to the addition question is :");
For (INT I = 1; I <= 5; I ++)
{
Int A = objrandom. nextint (90) + 10;
Int B = objrandom. nextint (90) + 10;
System. Out. println (a + "+" + B + "=? ");
Buff. append (A + B );
Buff. append ("");
}
System. Out. println (buff. tostring ());
}
Public static void main (string ARGs [])
{
Genquestion OBJ = new genquestion ();
OBJ. geneandprint ();
}
}

NOTE: If two-digit addition is generated, the two numbers randomly generated must be between 10 and 10 ~ Within the range of 99. To obtain a random integer x, where a <= x <= B, apply "objrandom. Next (B-A + 1) + ".

The plus sign can be used for string connection. However, when a string is frequently connected, the use of the plus sign is less effective. We recommend that you use the stringbuffer class.

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.