第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定

課后習(xí)題

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class SortTest {

private List<String> stringList = new ArrayList<String>();

/**
* 對隨機(jī)生成的十條字符串集合進(jìn)行排序 該字符串有如下要求:
* 1、字符串的長度為10以為的隨機(jī)整數(shù)
* ?2、字符串的每個字符都是隨機(jī)生成的的,可以重復(fù)
* 3、每條字符串不可重復(fù)
*
*/
public void testSort() {
// 定義一個random類來生成隨機(jī)數(shù)
Random random = new Random();
for (int i = 0; i < 10; i++) {

int stringLength = 0;// 獲取隨機(jī)長度
int temp = 0;// 接受隨機(jī)字符并轉(zhuǎn)換成字母
// 創(chuàng)建一個緩沖器,用于得到一個隨機(jī)字符串
StringBuffer buffer = new StringBuffer();

// 獲得10以內(nèi)的字符串長度
do {
stringLength = random.nextInt(10);
} while (stringLength == 0); // 長度不能為0

// 開始為buffer添加字符
do{
for (int j = 0; j < stringLength; j++) {
/*
* 用ascII碼進(jìn)行轉(zhuǎn)換成A~z的字符(65~90,97~112),獲取到一個字母后,添加給buffer
*/
do {
temp = random.nextInt(113);
} while (!((temp >= 65 && temp <= 90) || (temp >= 97 && temp <= 112)));
buffer.append((char) temp);

}
}while(stringList.contains(buffer.toString())); // do-while防止重復(fù)添加
// 若不重復(fù),添加到集合中
stringList.add(buffer.toString());
}
System.out.println("---------排序前----------");
print();
Collections.sort(stringList);
System.out.println("---------排序后----------");
print();
}

// 遍歷打印字符串集合
public void print() {
for (String str : stringList) {
System.out.println(str);
}

}

public static void main(String[] args) {
SortTest st = new SortTest();
st.testSort();
}
}


正在回答

1 回答

good

0 回復(fù) 有任何疑惑可以回復(fù)我~

舉報

0/150
提交
取消
Java入門第三季
  • 參與學(xué)習(xí)       409775    人
  • 解答問題       4546    個

Java中你必須懂得常用技能,不容錯過的精彩,快來加入吧

進(jìn)入課程

課后習(xí)題

我要回答 關(guān)注問題
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號