欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

隨機(jī)四位數(shù)java代碼 隨機(jī)生成四位數(shù)的代碼

java里面怎么產(chǎn)生5位隨機(jī)的四位數(shù)?

Math.random()產(chǎn)生一個(gè)0到1的浮點(diǎn)數(shù)(小數(shù))\x0d\x0a四位數(shù),1000到9999,所以\x0d\x0a(int)(Math.random()*9000+1000)\x0d\x0a這樣,最小是0*9000+1000是1000\x0d\x0a因?yàn)镸ath.random()不可能為1,所以最大數(shù)小于1*9000+1000=10000

創(chuàng)新互聯(lián)建站是網(wǎng)站建設(shè)專(zhuān)家,致力于互聯(lián)網(wǎng)品牌建設(shè)與網(wǎng)絡(luò)營(yíng)銷(xiāo),專(zhuān)業(yè)領(lǐng)域包括網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè)、電商網(wǎng)站制作開(kāi)發(fā)、成都小程序開(kāi)發(fā)、微信營(yíng)銷(xiāo)、系統(tǒng)平臺(tái)開(kāi)發(fā),與其他網(wǎng)站設(shè)計(jì)及系統(tǒng)開(kāi)發(fā)公司不同,我們的整合解決方案結(jié)合了恒基網(wǎng)絡(luò)品牌建設(shè)經(jīng)驗(yàn)和互聯(lián)網(wǎng)整合營(yíng)銷(xiāo)的理念,并將策略和執(zhí)行緊密結(jié)合,且不斷評(píng)估并優(yōu)化我們的方案,為客戶(hù)提供全方位的互聯(lián)網(wǎng)品牌整合方案!

JAVA中怎么隨機(jī)產(chǎn)生一個(gè)不重復(fù)的四位數(shù)

//題主估計(jì)想問(wèn)產(chǎn)生一個(gè)四個(gè)不同數(shù)字的四位數(shù)//

//首先千位不為0

String number=String.valueOf((int)(Math.random()*9+1));

//再產(chǎn)生其它三位數(shù)

for(int i=0;i=2;){

String newnumber=String.valueOf((int)(Math.random()*9));

if(!number.contains(newnumber)){

number=number+newnumber;

i++;

}

}

System.out.println(number);

用java實(shí)現(xiàn):隨機(jī)獲取4位的驗(yàn)證碼

驗(yàn)證碼是指網(wǎng)頁(yè)的驗(yàn)證碼還是手機(jī)的驗(yàn)證碼

下面是隨機(jī)生成四位數(shù)的相關(guān)代碼

import?java.util.Random;

public?class?RandomTest?{

public?static?void?main(String[]?args)?{

System.out.println("Math.random得到小數(shù)");

System.out.println(Math.round(Math.random()?*?10000));

System.out.println("Random");

System.out.println(new?Random().nextInt(9999));

System.out.println("字符串前面補(bǔ)0的話(huà)就這樣String.format");

System.out.println(String.format("%04d",new?Random().nextInt(9999)));

}

}

如何用Java代碼段生成四位數(shù)字加字母的驗(yàn)證碼?

不知道你問(wèn)的是不是生成這種圖片驗(yàn)證碼?如果只要一個(gè)隨機(jī)四位數(shù) 那這行代碼就夠了(new Random().nextInt(9000) + 1000;),如果是生成頁(yè)面圖片驗(yàn)證碼就是下面的了: //設(shè)定 響應(yīng)模式 resp.setContentType("image/jpeg"); // 生成令牌環(huán)數(shù)據(jù); Integer token = new Random().nextInt(9000) + 1000; // 保存令牌環(huán)數(shù)據(jù)到session中 req.getSession().setAttribute(IMAGE_TOKEN_NAME, token); // 生成令牌環(huán)圖片 ServletOutputStream out = resp.getOutputStream(); BufferedImage img = new BufferedImage(60, 20, BufferedImage.TYPE_INT_RGB); Graphics g = img.getGraphics(); g.setColor(Color.YELLOW); g.fillRect(0, 0, img.getWidth(), img.getHeight()); g.setColor(Color.BLUE); g.setFont(new Font("", Font.BOLD, 18)); g.drawString(String.valueOf(token), 10, 16); ImageIO.write(img, "jpg", out); out.close();

下面簡(jiǎn)單的介紹他們的功能和用途,執(zhí)行效率等。每個(gè)都有各自的優(yōu)缺點(diǎn)看你是做甚什么方面的研究開(kāi)發(fā)用。.net,是網(wǎng)站編程,現(xiàn)在很多都用這個(gè),但是這個(gè)語(yǔ)言編程都有統(tǒng)一思路,很好掌握。窒息那個(gè)效率不是很高;php 支持跨平臺(tái),很容易學(xué)會(huì),執(zhí)行的效率很高;asp是ASP.net的前身,它比較穩(wěn)定,比.net要弱一點(diǎn)。但是比.net好學(xué)。jsp 是網(wǎng)頁(yè)編程,這個(gè)學(xué)習(xí)大約一周就能搞定,不過(guò)這個(gè)得多實(shí)踐,不然的話(huà),時(shí)間長(zhǎng)了,就容易忘記。

我自己做的系統(tǒng)里面用作驗(yàn)證碼的JSP的%@page contentType="image/jpeg;charset=utf-8"%%@page import="java.util.*,java.awt.*,java.awt.image.*,javax.imageio.*" %%@ page import="java.io.OutputStream" %html body %! Color getRandColor(int fc,int bc) { Random rd=new Random(); if(fc255) fc=255; if(bc255) bc=255; int red=fc+rd.nextInt(bc-fc); int green=fc+rd.nextInt(bc-fc); int blue=fc+rd.nextInt(bc-fc); return new Color(red,green,blue); } % % Random r=new Random(); response.addHeader("Pragma","No-cache"); response.addHeader("Cache-Control","no-cache"); response.addDateHeader("expires",0); int width=90; int height=23; BufferedImage pic=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); Graphics gc=pic.getGraphics(); gc.setColor(getRandColor(200,250)); gc.fillRect(0,0,width,height); String[] rNum ={"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f", "g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w", "x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N", "O","P","Q","R","S","T","U","V","W","X","Y","Z"}; int[] style = {Font.PLAIN,Font.BOLD,Font.ITALIC,Font.PLAIN+Font.BOLD, Font.BOLD+Font.ITALIC,Font.PLAIN+Font.ITALIC,Font.PLAIN+Font.BOLD+Font.ITALIC}; gc.setColor(Color.WHITE); gc.drawLine(0,30,90,10); gc.setColor(getRandColor(160,200)); for (int i=0;i50;i++) { int x = r.nextInt(width); int y = r.nextInt(height); int xl = r.nextInt(10); int yl = r.nextInt(10); gc.drawLine(x,y,x+xl,y+yl); } gc.setColor(getRandColor(60,150)); String rt = ""; for(int i=0;i4;i++){ String temp = rNum[r.nextInt(62)]; rt = rt+temp; gc.setFont(new Font("Times New Roman",style[r.nextInt(7)],15)); gc.drawString(temp,5+i*15+r.nextInt(10),10+r.nextInt(10)); } gc.dispose(); session.setAttribute("randNum",rt); OutputStream os=response.getOutputStream(); ImageIO.write(pic,"JPEG",os); System.out.println("當(dāng)前驗(yàn)證碼為:"+session.getAttribute("randNum")); os.flush(); os.close(); os=null; response.flushBuffer(); out.clear(); out = pageContext.pushBody(); % /body/html

java編碼中怎樣產(chǎn)生四位隨機(jī)數(shù)

可以借助Math類(lèi)里的random方法或者借助Random類(lèi)來(lái)實(shí)現(xiàn)

1、使用Math類(lèi)的random方法實(shí)現(xiàn)產(chǎn)生1000-9999的隨機(jī)數(shù)代碼如下:

int a = (int)(Math.random()*(9999-1000+1))+1000;//產(chǎn)生1000-9999的隨機(jī)數(shù)

2、使用Random類(lèi)實(shí)現(xiàn)代碼:

import java.util.Random;//導(dǎo)入Random包

public class Ranadd {

public static void main(String[] args) {

int x;//定義兩變量

Random ne=new Random();//實(shí)例化一個(gè)random的對(duì)象ne

x=ne.nextInt(9999-1000+1)+1000;//為變量賦隨機(jī)值1000-9999

System.out.println("產(chǎn)生的隨機(jī)數(shù)是:"+x);//輸出

}

}

網(wǎng)頁(yè)名稱(chēng):隨機(jī)四位數(shù)java代碼 隨機(jī)生成四位數(shù)的代碼
文章分享:http://chinadenli.net/article6/dodssog.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、、品牌網(wǎng)站設(shè)計(jì)、定制開(kāi)發(fā)、服務(wù)器托管、網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)