Experimental verification Code generation in arithmetic experiment

Source: Internet
Author: User

Program Design Requirements:

Write a program to write a "software" that automatically generates 30 primary arithmetic topics.

Experimental requirements:

1) According to the content of the topic to write programs to achieve the function.

2) The requirements of the experiment report include the design idea, program flowchart, source program, realization result, experiment summary (including errors during debugging).

Programming Ideas:

1. First we call the Java.util packet to get the random number of the function import Java.util.Radom, and then define the subtraction, get the range of random numbers, I set the value range to 1-999. Next we want to set the number of questions generated, according to test instructions requirements, I set it to 30.

Source:

Package Sizemath;

Import Java.util.Random;

public class Sizematht {

public static void Main (string[] args) {

String[] Operate=new string[] {"+", "-", "*", "/"};

Int[] Numbers=new int[1000];

for (int i = 1;i<1000;i++)

{

Numbers[i-1]=i;

}

Random r=new random ();

for (int i=0;i<30;i++)

{

System. out. println (Numbers[r.nextint (1000)]+

Operate[r.nextint (4)]+

Numbers[r.nextint (1000)]+ "=");

}

}

}

Implementation results:

Experiment Summary:

This experiment is a practical thing to do experiment, because it comes from life, to give children this homework we certainly know, but it is very interesting to use a program to complete. This experiment gives me access to some of the features in Java that can invoke existing classes. Like this time I called the util. Random package. The experiment also made me experience.

Programming Ideas:

The program we first set the verification code of the length of the height, at the beginning of the generation of random numbers and the shape of the number, next to draw interference lines. We apply the random function to the shape of a single character and to the shape of the interfering line.

SOURCE program:

Package yanzhengma;

Import Java.awt.Color;

Import Java.awt.Font;

Import Java.awt.Graphics;

Import Java.awt.Graphics2D;

Import Java.awt.image.BufferedImage;

Import Java.util.Random;

Import Javax.swing.ImageIcon;

Import Javax.swing.JFrame;

Import Javax.swing.JLabel;

/**

* Draw the captcha image

*

* @author

*

*/

public class Yanzhengma {

public static class Captcha {

Image Width

private int width = 100;

Image Height

private int height = 30;

Verification Code character length

private int length = 6;

randomly generated verification code base string

Private final String basecharacters = "abcdefghijkmnpqrstuvwxyABCDEFGHIJKLMNPQRSTUVWXYZ123456789";

/**

* Get Verification Code image

*

* @return Verification Code Image

*/

Public BufferedImage Getcaptchaimage () {

Creating an image buffer

BufferedImage img = new BufferedImage (width, height,

BUFFEREDIMAGE.TYPE_INT_RGB);

Get Image context (brush)

Graphics g = img.getgraphics ();

set the image background color, fill the background rectangle

G.setcolor (Getrandomcolor (200, 255));

G.fillrect (0, 0, width, height);

Draw Border

G.setcolor (Color.Black);

G.drawrect (0, 0, width-1, height-1);

/* generate random Verification code * /

int len = Basecharacters.length (); Base String Length

G.setfont (New Font (" italics ", Font.hanging_baseline, 24)); set Captcha font

loop generate verification Code characters

Random rand = new Random ();

for (int i = 0; i < length; i++) {

randomly generate a single character in a verification code

String randstr = string.valueof (Basecharacters.charat (Rand

. Nextint (len));

single character draw width

int width = this.width/this.length;

current character draw Origin

int x = width * i;

int y = this.height/2 + rand.nextint (THIS.HEIGHT/3);

/* draw the character into the image * /

DrawString (g, X, Y, randstr);

}

Draw Interference Lines

DrawLine (g, 10);

releasing an image context (brush)

G.dispose ();

return img;

}

/**

* draw a single character in the CAPTCHA string

*

* @param g

* Image Context

* @param width

* the width of the characters

* @param height

* the height of the characters

* @param str

* string to be drawn

*/

private void DrawString (Graphics g, int width, int height, String str) {

Random rand = new Random ();

randomly generated character rotation angle ( -30~30 degrees )

int degree = Rand.nextint (60);

if (Degree > 30)

degree = 30-degree;

Set Font Color

G.setcolor (Getrandomcolor (0, 80));

convert graphics2d

graphics2d g2 = (graphics2d) g.create ();

translating the origin to the center of the graphical environment , This method actually moves the string to a certain location

G2.translate (Width + rand.nextint (5), Height + rand.nextint (5));

Rotate Text

G2.rotate (degree * math.pi/180);

to draw the text, it is important to note that the brush here already has the last specified position , so the specified here is actually a relative position

G2.drawstring (str, 0, 0);

}

/**

* draw random interference lines

*

* @param g

* Brushes (Image context)

* @param count

* number of interfering lines

*/

private void DrawLine (Graphics g, int count) {

Random rand = new Random ();

cycle through each line of interference

for (int i = 0; i < count; i++) {

set line random color

G.setcolor (Getrandomcolor (180, 200));

generate a random line start point end point coordinate point

int x1 = Rand.nextint (this.width);

int y1 = Rand.nextint (this.height);

int x2 = Rand.nextint (this.width);

int y2 = rand.nextint (this.height);

Draw Lines

G.drawline (x1, y1, x2, y2);

}

}

/**

* get random colors

*

* @param minimum

* Color Lower value

* @param maximum

* Color Upper value

* @return Random Color objects

*/

Private Color getrandomcolor (int minimum, int maximum) {

if (Minimum > maximum) {

int tmp = minimum;

minimum = maximum;

Maximum = tmp;

}

if (Maximum > 255)

Maximum = 255;

if (Minimum < 0)

Minimum = 0;

int r = minimum + (int) (Math.random () * (Maximum-minimum));

int g = minimum + (int) (Math.random () * (Maximum-minimum));

int b = minimum + (int) (Math.random () * (Maximum-minimum));

return new Color (R, G, b);

}

public static void Main (string[] args) {

Captcha Captcha = new Captcha ();

JFrame frame = new JFrame (" Verification Code ");

Frame.setsize (300, 200);

Frame.setlocationrelativeto (NULL);

Frame.setdefaultcloseoperation (Jframe.exit_on_close);

JLabel lbl = new JLabel (New ImageIcon (Captcha.getcaptchaimage ()));

Frame.add (LBL);

Frame.setvisible (TRUE);

}

Experiment:

}

}

Experiment Summary:

This experiment is very difficult, after all, I thought we now do the output of the program is only in the black box to show a bunch of the results we want, but I did not think we now this level can also do verification code. It makes me feel very fresh. Through practice I run out of what I want.

Experimental verification Code generation in arithmetic experiment

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.