Method of construction 1: Write a program that generates arithmetic topics

Source: Internet
Author: User

ImportJava.util.*; Public classArithmetictopic {Private StaticRandom random =NewRandom ();  PublicArithmetictopic () {//TODO auto-generated Constructor stub    }    PrivateString Createnumber () {intNumber1 =1+random.nextint (100); intPoint1 = 0; String numberstring=""; if(Random.nextboolean ()) {point1=Number1; Number1= 1+Random.nextint (POINT1); if(Point1==number1) point1=0; }        if(point1>0) {numberstring= "" +number1+ "/" +point1; }Else{numberstring= "" +Number1; }        returnnumberstring; }    PrivateString topicstring () {Charoperator = ' + '; Switch(Random.nextint (3))  {             Case0:operator = ' + '; Break;  Case1:operator = '-'; Break;  Case2:operator = ' * '; Break;  Case3:operator = '/'; Break; default: Break;        }; returnCreatenumber () +operator+createnumber () + "="; }     Public Static voidMain (string[] args) {arithmetictopic topic=NewArithmetictopic ();  for(inti=0;i<10;i++) System.out.println (topic.topicstring ()); }}

Program Ideas:

Generate a true score: first produces a 1-100 random integer y, according to the random Boolean value to determine whether to produce a 1-y random integer x as a molecule, to form x/y, if the x/y=1 is directly output x.

Generates arithmetic expression: number x1,x2, inserts a random operator in the middle of the string, and ends with an equal sign output to the command line.

Method of construction 1: Write a program that generates arithmetic topics

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.