/**

10多年的欽北網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開(kāi)發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。成都全網(wǎng)營(yíng)銷(xiāo)的優(yōu)勢(shì)是能夠根據(jù)用戶(hù)設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整欽北建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)公司從事“欽北網(wǎng)站設(shè)計(jì)”,“欽北網(wǎng)站推廣”以來(lái),每個(gè)客戶(hù)項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
1.對(duì),沒(méi)有跳出循環(huán),循環(huán)次數(shù)應(yīng)該為26次 0-25
2.另外你的代碼對(duì)第一次做了判斷,執(zhí)行了第一次,接著繼續(xù)執(zhí)行后面的代碼,這就導(dǎo)致第一次for循環(huán)的時(shí)候,list加入了2次
所以,你代碼的for循環(huán)條件應(yīng)該為 times25.也可以用if(times==0){}else{}
3.另外r.nextInt(26)+97 才為 a-z的字符
4.直接使用 ArrayListCharacter list; Character為char型對(duì)應(yīng)的對(duì)象。
5.第一次不用單獨(dú)判斷,否則每次的循環(huán)的時(shí)候還得判斷times==0,影響效率
6.26個(gè)字符,這個(gè)數(shù)量最好定義為一個(gè)常量 ,如public static int charCount=26;
**/
//下面的代碼是我修改后的。
import java.util.ArrayList;
import java.util.Random;
public class Test {
public static void main(String[] args) {
Random r = new Random();
char zi;
ArrayListCharacter list =new ArrayListCharacter();//直接定義為Character對(duì)象
for(int times=0,count=1;times26;times++){
while(true){
zi=(char)(r.nextInt(26)+97);//第一次不用單獨(dú)判斷,否則每次的循環(huán)的時(shí)候還得判斷times==0,影響效率
System.out.println("count:"+(count++)+"--"+zi);
if(!list.contains(zi)){
list.add(zi);
break;
}
}
}
System.out.println("list size:"+list.size());
for(char c:list){
System.out.print(c+" ");
}
}
}
public?class?My?{
public?static?void?main(String[]?args)?{
makeCount();
}
public?static?void?makeCount()?{
Random?random?=?new?Random();
int?i?=?random.nextInt(10);
int?j?=?random.nextInt(10);
int?k?=?random.nextInt(10);
while?(i?==?j)?{
j?=?random.nextInt(10);
}
while?(k?==?j?||?k?==?i)?{
k?=?random.nextInt(10);
}
System.out.println(i?+?","?+?j?+?","?+?k);
}
}
生成隨機(jī)數(shù)可以java.util.Random類(lèi)的nextInt(int)方法來(lái)生成,如果要不重復(fù),可把這些數(shù)放入Set集合中,會(huì)自動(dòng)去重。下面是代碼:
import java.util.*;
public class Test
{
public static void main(String[] args)
{
SetInteger set=new HashSetInteger();
Random r=new Random();
do
{
int i=r.nextInt(80);
set.add(i);
}while(set.size()20);
Integer[] arr=new Integer[20];
set.toArray(arr);
for(int a=0;aarr.length;a++)
System.out.print(arr[a]+" ");
}
}
當(dāng)前文章:java不重復(fù)名代碼 java產(chǎn)生不重復(fù)隨機(jī)數(shù)的代碼
文章起源:http://chinadenli.net/article24/dohgece.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站策劃、App開(kāi)發(fā)、、網(wǎng)站設(shè)計(jì)公司、手機(jī)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(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)