導航:首頁 > 編程語言 > java隨機數字字母

java隨機數字字母

發布時間:2022-04-26 17:45:15

java中怎樣產生隨機數和隨機字母啊U

random

❷ 如何用java隨機生成一個字母

char c=(char)(int)(Math.random()*26+97);

System.out.println(c);

通過隨機函數生成,字母對應的int整型數字,然後轉換才char類型的字母。

tring chars = "abcdefghijklmnopqrstuvwxyz";

System.out.println(chars.charAt((int)(Math.random() * 26)));

根據chars 隨機截取其中的一個字母。

(2)java隨機數字字母擴展閱讀:

定義隨機字母之前調用

import java.util.Arrays;

產生隨機數的代碼為:

char cha[]={'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'};

char ch[]=new char[5];

for(int i=0;i<ch.length;i++)

{

int index;

index=(int)(Math.random()*(cha.length));

ch[i]=cha[index];

}

System.out.println(Arrays.toString(ch));

這個是產生5個隨機字母在A-Z的范圍

❸ java實現輸出指定長度的隨機字元(大小寫字母和數字)遇到問題

程序輸出錯誤的原因(見圖)

❹ 請java中的解釋一段隨機生成字母+數字的代碼

就是把隨機產生的int轉成大小寫字母和數字對應的ascii碼,因為大,小寫字母和數字的ascii碼不是連續的,所以要分別考慮。 asc('0') = 48, asc('A') = 65, asc('a') = 97

❺ 用java實現生成一隨機字母(包括大小寫),並輸出

package www;

public class Test6_5_2 {

public static void main(String[] args) {
// TODO 自動生成的方法存根
int origin1=65;
int end1 =90;
int origin2=97;
int end2=122;
int irand1 = (int)(Math.random()*( end1 - origin1 ));
int irand2 = (int)(Math.random()*( end2 - origin2 ));
irand1 += origin1;
irand2 += origin2;
int suiji=(int)(Math.random()*3);
if(suiji<=1){
System.out.println("輸出隨機字母:"+irand1);
}else{
System.out.println("輸出隨機字母:"+irand2);
}
}

}

❻ java中的數字,字母,漢字如何隨機生成

數字,字母比較簡單。

//手寫的,省略了一部分String szzm = "0123...789abc...xyzABC...XYZ"; StringBuilder bu = new StringBuilder();for(int i = 0;i<6;i++){ bu.append(szzm.charAt(new Random().nextInt(szzm.length())));}System.out.println(bu.toString());

漢字復雜一些。我的方法是數字區間,19968~40869 之間 int 轉char 都會變成漢字。
不過這個范圍有點大,2w多漢字,有繁體的,還有生僻字。 如果不怕麻煩,漢字也可和數字一樣,弄個漢字表。
StringBuilder bu = new StringBuilder();int base = 19968;int qujian = 40869 - 19968;for(int i = 0;i<6;i++){ int rand = base + new Random().nextInt(qujian); bu.append((char)rand);}System.out.println(bu.toString());

❼ Java中怎樣產生隨機數和隨機字母

java產生隨機數和隨機字母,使用madom類,示例如下:

packagecom.qiu.lin.he;

publicclassCeShi{

publicstaticvoidmain(String[]args){

intstr1=0;
for(inti=0;i<25;i++){//你想生成幾個字元的,就把4改成幾,如果改成1,那就生成一個隨機字母.
str1=(char)(Math.random()*26);
}
System.out.println("輸出隨機生成的字元串"+str1);

Stringstr="";
for(inti=0;i<25;i++){//你想生成幾個字元的,就把4改成幾,如果改成1,那就生成一個隨機字母.
str=str+(char)(Math.random()*26+'A');
}
System.out.println("輸出隨機生成的字元串"+str);
}
}

運行結果如下:

❽ 怎麼樣用java代碼自動生成不重復的含有數字和字母的隨機碼

package tests;
/**
* 隨機生成32位字元串
*/

public class String32 {
public static String random(int length){//傳入的字元串的長度
StringBuilder builder = new StringBuilder(length);
for(int i = 0; i < length; i++){

int r = (int) (Math.random()*3);
int rn1=(int)(48+Math.random()*10);
int rn2=(int)(65+Math.random()*26);
int rn3=(int)(97+Math.random()*26);

switch(r){
case 0:
builder.append((char)rn1);
break;
case 1:
builder.append((char)rn2);
break;
case 2:
builder.append((char)rn3);
break;
}
}
return builder.toString();
}
public static void main(String[] args){
String s = String32.random(32);
System.out.println(s);
}
}

❾ java中如何產生一個16位數字組成的隨機字元串

方法如下:

Random ran=new random();

boolean flag=true;

while(flag){

int a=ran.nextInt(99999999);

int b=ran.nextInt(99999999);

long c=a*100000000L+b;

if(c>1000000000000000L&&c<9999999999999999L){

flag=false;
c1=c;
String num=String.valueOf(c1);

}

❿ 使用java程序實現:隨機輸入一串數字與字母,對這串字元進行分組排序

String str="9f3k2j5z031";
char[] ch=str.toCharArray();
char[] temp=new char[ch.length];
int count=0;
for(char c:ch){
if(c>57){
count++;
}
}
System.array(ch, 0, temp, 0, ch.length);
Arrays.sort(temp);
System.out.println(count);
System.out.println(new String(temp));
int size=temp.length;
for(int i=0;i<size;i++){
if((i/2)<count&&i%2==0){
System.out.print(temp[size-1-(i/2)]);
}
else if((i/2)<count&&i%2==1){
System.out.print(temp[(i/2)]);
}
else{
System.out.print(temp[i-count]); }
}

閱讀全文

與java隨機數字字母相關的資料

熱點內容
銀河麒麟字體庫存在哪個文件夾 瀏覽:956
魔獸加丁伺服器的航空叫什麼 瀏覽:152
花冠改裝案例哪個app多 瀏覽:515
成績單app哪個好用 瀏覽:140
北美程序員vs國內程序員 瀏覽:181
php解析xml文檔 瀏覽:121
石墨文檔APP怎麼橫屏 瀏覽:185
牆主鋼筋加密和非加密怎麼看 瀏覽:144
金山區文件夾封套定製 瀏覽:708
soho程序員 瀏覽:672
java位元組截取 瀏覽:525
php提交作業 瀏覽:815
房產還沒解壓可以辦理贈予嗎 瀏覽:224
java毫秒轉分鍾 瀏覽:753
模式識別中文pdf 瀏覽:774
c語言平均數字編譯錯誤 瀏覽:171
單片機算交流 瀏覽:45
php自適應網站 瀏覽:467
2b2t伺服器怎麼獲得許可權 瀏覽:816
c語言javaphp 瀏覽:804