Another graphics verification code (type of formula)

Source: Internet
Author: User
Tags arithmetic
Inadvertently on the Internet to see this formula type of graphics verification code, feel good. I've written another practice.
The code is as follows:
/** */ /**
* Random generation of an addition or multiplication of arithmetic expressions,
* Draw a graphical verification code for it and return the result of the expression.
*/
Package Greenis

Import Java.awt.Color;
Import Java.awt.Font;
Import Java.awt.Graphics;
Import Java.awt.image.BufferedImage;
Import java.io.IOException;
Import Java.io.OutputStream;
Import Java.util.Random;

Import Javax.imageio.ImageIO;

/** */ /**
* @author Greenis
* @date 2007-10-8
* @version 1.0
*/
public class Arithmeticvalidatecode
... {
A collection of numbers.
Private int[] Number = ... {0,1,2,3,4,5,6,7,8,9};
The collection of operators.
Private char[] operator = ... {' + ', ' x '};
Picture width.
private int width = 50;
Picture height.
private int height = 20;
An arithmetic expression.
private String expression;
Calculated results.
private int result;

/** *//**
* Generate an arithmetic expression and its corresponding result.
*/
Public Arithmeticvalidatecode ()
... {
int NUM1 = this.number[(int) (Number.length * Math.random ())];
int num2 = this.number[(int) (Number.length * Math.random ())];
Char oper = this.operator[(int) (Operator.length * Math.random ())];
This.expression = integer.tostring (NUM1) + character.tostring (oper) + integer.tostring (num2) + "=";

Switch (OPER)
... {
Case ' + ': This.result = num1 + num2; Break
Case ' x ': This.result = num1 * NUM2; Break
Default:this.result =-1; Break
}
}

/** *//**
* Generates a graphical validation code that contains an arithmetic expression and returns the result of an expression.
*
* @param os
* @return Result
*/
Public String Getvalidatecode (outputstream OS)
... {
//
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.