3 ways to generate image verification codes in Java (letters, addition, subtraction, multiplication and division, Chinese)

The first method is composed entirely of letters and numbers package com.myFirstSpring.util; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.Random; import javax.imageio.ImageIO; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * * @Description Generate verification code * @author waa * @version V1.0, date 2011-7-4 */ public class ValidateCodeUtils extends HttpServlet { private static final long serialVersiOnUID= -1409007752285164213L; private static int width = 75; private static int height = 35; // private static char[] ch = // “AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789”.toCharArray(); private static char[] ch = “abcdefghijklmnopqrstuvwxyz0123456789”.toCharArray(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } // Generate a verification code of numbers and letters public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(“image/jpeg”); response.setHeader(“Pragma”, “No-cache”); response.setHeader(“Cache-Control”, “no-cache”); response.setDateHeader(“Expires”, 0); //Create image in memory BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // Get graphics context Graphics g = image.getGraphics(); // Generate random class Random random = new Random(); //Set background color g.setColor(getRandColor(200, 250)); g.fillRect(0, 0, width, height); //Set font g.setFont(new Font(“Times New Roman”, Font.PLAIN, 18)); // draw borders // g.setColor(new Color()); // g.drawRect(0,0,width-1,height-1); // Randomly generate 155 interference…

Basic usage of BigDecimal in java for addition, subtraction, multiplication and division

Foreword As we all know, the API class BigDecimal provided by Java in the java.math package is used to perform precise operations on numbers with more than 16 significant digits. Double precision floating point variable double can handle 16 significant numbers. In practical applications, larger or smaller numbers need to be operated and processed. Float and double can only be used for scientific calculations or engineering calculations. java.math.BigDecimal should be used in commercial calculations. What BigDecimal creates is an object. We cannot use traditional arithmetic operators such as +, -, *, / to directly perform mathematical operations on its objects, but must call its corresponding methods. The parameters in the method must also be BigDecimal objects. Constructors are special methods of a class that are used to create objects, especially objects with parameters. The sample code is as follows import java.math.BigDecimal; public class T { public static void main(String[] args) { String a = “9999.9999”; int b = 9999; double c = 9999.9999; char d = 99; System.out.println(“===================”); // Convert different types to BigDecimal BigDecimal ma = new BigDecimal(a); BigDecimal mb = new BigDecimal(b); BigDecimal mc = new BigDecimal(c); BigDecimal md = new BigDecimal(d); System.out.println(“ma:”+ma.toString()); System.out.println(“mb:”+mb.toString()); System.out.println(“mc:”+mc.toString()); System.out.println(“md:”+md.toString()); System.out.println(“===================”); //…

JavaScript solution to the problem of loss of precision in addition, subtraction, multiplication and division of decimals

Reason: js handles the addition, subtraction, multiplication and division of decimals according to the binary system. On the basis of arg1, the precision of arg2 is expanded or inversely expanded to match, so the following situation will occur. The problem of decimal point addition, subtraction, multiplication and division in Javascript (js) is a js bug, such as 0.3*1 = 0.2999999999, etc. Listed below are four js algorithms that can perfectly obtain the corresponding accuracy function accDiv(arg1,arg2){ var t1=0,t2=0,r1,r2; try{t1=arg1.toString().split(“.”)[1].length}catch(e){} try{t2=arg2.toString().split(“.”)[1].length}catch(e){} with(Math){ r1=Number(arg1.toString().replace(“.”,””)) r2=Number(arg2.toString().replace(“.”,””)) return accMul((r1/r2),pow(10,t2-t1)); } } //Multiplication function accMul(arg1,arg2) { var m=0,s1=arg1.toString(),s2=arg2.toString(); try{m+=s1.split(“.”)[1].length}catch(e){} try{m+=s2.split(“.”)[1].length}catch(e){} return Number(s1.replace(“.”,””))*Number(s2.replace(“.”,””))/Math.pow(10,m) } //addition function accAdd(arg1,arg2){ var r1,r2,m; try{r1=arg1.toString().split(“.”)[1].length}catch(e){r1=0} try{r2=arg2.toString().split(“.”)[1].length}catch(e){r2=0} m=Math.pow(10,Math.max(r1,r2)) return (arg1*m+arg2*m)/m } //Subtraction function Subtr(arg1,arg2){ var r1,r2,m,n; try{r1=arg1.toString().split(“.”)[1].length}catch(e){r1=0} try{r2=arg2.toString().split(“.”)[1].length}catch(e){r2=0} m=Math.pow(10,Math.max(r1,r2)); n=(r1>=r2)?r1:r2; return ((arg1*m-arg2*m)/m).toFixed(n); } Let’s analyze the problem of loss of numerical precision in Javascript 1. Some typical problems with JS digital precision loss 1. Add two simple floating point numbers 0.1 + 0.2 != 0.3 // true Firebug This is really not a Firebug problem, you can try using alert (haha, just kidding). Look at the results of Java operations Look at Python again 2. Large integer operations 9999999999999999 == 10000000000000001 // ? Firebug It makes no sense that the 16-digit and…

Examples of Java implementation of matrix addition, subtraction, multiplication, division and conversion functions

The examples in this article describe Java’s implementation of matrix addition, subtraction, multiplication, division and conversion functions. Share it with everyone for your reference, the details are as follows: As a beginner in Java, write a matrix budget program as a tool for future use when writing algorithms. public class MatrixOperation { public static int[][] add(int[][] matrix_a, int[][] matrix_b) { int row = matrix_a.length; int col = matrix_a[0].length; int[][] result = new int[row][col]; if (row != matrix_b.length || col != matrix_b[0].length) { System.out.println(“Fault”); } else { for (int i = 0; i <row; i++) { for (int j = 0; j <col; j++) { result[i][j] = matrix_a[i][j] + matrix_b[i][j]; } } } return result; } public static int[][] sub(int[][] matrix_a, int[][] matrix_b) { int row = matrix_a.length; int col = matrix_a[0].length; int[][] result = new int[row][col]; if (row != matrix_b.length || col != matrix_b[0].length) { System.out.println("Fault"); } else { for (int i = 0; i <row; i++) { for (int j = 0; j <col; j++) { result[i][j] = matrix_a[i][j] – matrix_b[i][j]; } } } return result; } public static int[][] dot(int[][] matrix_a, int[][] matrix_b) { /* * matrix_a's dimention m*p matrix_b's dimention p*n. return dimention *m*n */ int row…

JavaScript solution to the loss of precision in addition, subtraction, multiplication and division of decimals

Reason: js handles the addition, subtraction, multiplication and division of decimals according to the binary system. On the basis of arg1, the precision of arg2 is expanded or inversely expanded to match, so the following situation will occur. The problem of decimal point addition, subtraction, multiplication and division in Javascript (js) is a js bug, such as 0.3*1 = 0.2999999999, etc. Listed below are four js algorithms that can perfectly obtain the corresponding accuracy function accDiv(arg1,arg2){ var t1=0,t2=0,r1,r2; try{t1=arg1.toString().split(“.”)[1].length}catch(e){} try{t2=arg2.toString().split(“.”)[1].length}catch(e){} with(Math){ r1=Number(arg1.toString().replace(“.”,””)) r2=Number(arg2.toString().replace(“.”,””)) return accMul((r1/r2),pow(10,t2-t1)); } } //Multiplication function accMul(arg1,arg2) { var m=0,s1=arg1.toString(),s2=arg2.toString(); try{m+=s1.split(“.”)[1].length}catch(e){} try{m+=s2.split(“.”)[1].length}catch(e){} return Number(s1.replace(“.”,””))*Number(s2.replace(“.”,””))/Math.pow(10,m) } //addition function accAdd(arg1,arg2){ var r1,r2,m; try{r1=arg1.toString().split(“.”)[1].length}catch(e){r1=0} try{r2=arg2.toString().split(“.”)[1].length}catch(e){r2=0} m=Math.pow(10,Math.max(r1,r2)) return (arg1*m+arg2*m)/m } //Subtraction function Subtr(arg1,arg2){ var r1,r2,m,n; try{r1=arg1.toString().split(“.”)[1].length}catch(e){r1=0} try{r2=arg2.toString().split(“.”)[1].length}catch(e){r2=0} m=Math.pow(10,Math.max(r1,r2)); n=(r1>=r2)?r1:r2; return ((arg1*m-arg2*m)/m).toFixed(n); } Let’s analyze the problem of loss of numerical precision in Javascript 1. Some typical problems with JS digital precision loss 1. Add two simple floating point numbers 0.1 + 0.2 != 0.3 // true Firebug This is really not a Firebug problem, you can try using alert (haha, just kidding). Look at the results of Java operations Look at Python again 2. Large integer operations 9999999999999999 == 10000000000000001 // ? Firebug It makes no sense that the 16-digit and…

How to implement addition, subtraction, multiplication and division in javascript

How to implement addition, subtraction, multiplication and division in Javascript: first use “document.getElementById('id').value” to get the value of two numbers; 2. Use “+”, “-“, The “*” and “/” operation symbols perform arithmetic operations and obtain results. The operating environment of this tutorial: Windows 7 system, Javascript version 1.8.5, Dell G3 computer. Addition, subtraction, multiplication and division Value A: Value B: Result: Recommended learning: Javascript video tutorial The above is the details of how to implement addition, subtraction, multiplication and division in Javascript. Please pay attention to other related articles for more!

Common date operations in Java (value acquisition, conversion, addition, subtraction, comparison)

In the development process of Java, it is inevitable to get entangled with the Date type. I am going to summarize the date-related operations frequently used in the project. JDK version 1.7. If it can help everyone save a few minutes to get up and move around and make a cup of coffee, it will be great. Okay, hehe. Of course, I only provide feasible solutions and do not guarantee best practices. Discussions are welcome. 1. Date value In the era of the old version of JDK, many codes used the java.util.Date class to obtain date values. However, since the Date class is not convenient for internationalization, in fact, starting from JDK1.1, it is more recommended to use java The .util.Calendar class handles time and date aspects. We won’t introduce the operations of the Date class here. Let’s go straight to the topic, how to use the Calendar class to obtain the current date and time. Since Calendar’s constructor method is protected, we will create a Calendar object through the getInstance method provided in the API. //There are multiple overloaded methods to create Calendar objects Calendar now = Calendar.getInstance(); //Default //Specify the time zone and region, or you can enter…

Java Calculator Addition, Subtraction, Multiplication and Division_How to learn Java quickly, do you know?

Java Calculator Addition, Subtraction, Multiplication and Division_How to learn Java quickly, do you know?

It is very difficult to learn JAVA first,No matter Whether you are taught or self-study, it will be difficult. Just because it is difficult to learn, you must plan the path well. Otherwise, you don’t know when you will become very confused. Regarding route planning I hope you will check this out. Java detailed learning route and road map Before learning, we need to know what Java can do? Java is mainly divided into three parts javaSE& #xff0c;javaME,javaEE JAVA SE is the basis for learning JAVA EE and JAVA ME. It can also be used to make local software. In layman’s terms, it is a stand-alone version of software. xff1b; JAVA EE is mainly used to develop Web-based systems , Provide solutions for various Internet applications and large and complex projects JAVA ME is used to develop Software for games, electronic devices, mobile phones, etc. Click the link to joinfor everyone to learn, communicate and chat 1. Learn the basics of language Obviously ,Mastering the basics of language is the first step. If you don’t understand the basics then you won’t know if you’re doing something wrong or what to do next. Of course, this does not require you to…

Sword Pointer Offer Problem Solving Report (Java version) – Addition without addition, subtraction, multiplication and division 47

? ? Introduction ? ? Generally, questions that cannot use the four arithmetic operations can only be solved with bit operations. The purpose is to enhance everyone’s understanding of computer calculations. It is really a bit nonsense. ? ? Solve the problem ? ? First we have to think about how computers do addition, for example, 3 plus 4, if converted into binary it is 0011 and 0100 add up to 7, which is 0111, which is equivalent to the XOR operation of two binary numbers ? ? But let’s take another example: 4 plus 4, you will find that the two binary numbers are 0100 and 0100, the result of the XOR operation is 0000, but what we want is 1000. This is because the XOR operation does not take into account Regarding the carry issue, it seems that we need to use an operation to consider the carry issue. Do you remember how carry is calculated? If the same bits in the two trees are 1, then a carry is required. This is actually the same as the operation ? ? And in the end we need to add up the carry and sum without taking the carry…

Java basic knowledge enhancement 89: BigDecimal class introduction and overview of BigDecimal class and the use of BigDecimal (addition, subtraction, multiplication and division)

Java basic knowledge enhancement 89: BigDecimal class introduction and overview of BigDecimal class and the use of BigDecimal (addition, subtraction, multiplication and division)

1. Overview of BigDecimal class: Due to the fact that float types and doubles can easily lose precision during operations. Therefore, in order to accurately express and calculate floating point numbers, Java provides BigDecimal. BigDecimal: Immutable, arbitrary-precision signed decimal number. 2. BigDecimal construction method 1 public BigDecimal(String val) 3. Usage of BigDecimal (addition, subtraction, multiplication and division) 1 public BigDecimal add(BigDecimal augend):Add 2 public BigDecimal subtract(BigDecimal subtrahend): Subtract 3 public BigDecimal multiply(BigDecimal divisor): multiply 4 public BigDecimal divide(BigDecimal divisor): divide 5 public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode): Quotient, how many decimals (scale), how to round (roundingMode) Code demo: 1 package cn.itcast_02; 2 3 import java.math.BigDecimal; 4 5 /* 6 * Construction method: 7 * public BigDecimal(String val) 8 * 9 * public BigDecimal add(BigDecimal augend) 10 * public BigDecimal subtract(BigDecimal subtrahend) 11 * public BigDecimal multiply(BigDecimal multiplicand) 12 * public BigDecimal divide(BigDecimal divisor) 13 * public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode): quotient, how many decimals, how to round 14 */ 15 public class BigDecimalDemo { 16 public static void main(String[] args) { 17 // System.out.println(0.09 + 0.01); 18 // System.out.println(1.0 – 0.32); 19 // System.out.println(1.015 * 100); 20 // System.out.println(1.301 / 100); 21 22 BigDecimal bd1 = new…

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索