考試要考這題,我完全不會呀求助大神

uj5u.com熱心網友回復:
import org.springframework.util.StringUtils;
import java.util.Arrays;
import java.util.Random;
public class MainTest {
public static void main(String[] args) {
String[] str = new String[100];
StringBuffer buffer = new StringBuffer(100);
for (int i = 0; i < str.length; i++) {
str[i] = randomString(1);
buffer.append(str[i]);
}
System.out.println(buffer);
int count = method1(buffer.toString(), "123");
System.out.println("123子字串的個數是:" + count);
}
/**
* 統計重復個數
* @param buffer
* @param a
* @return
*/
public static int method1(String buffer,String a){
int len = buffer.length() - buffer.replace(a,"").length();
return len / a.length();
}
/**
* 生成亂數
* @param a
* @return
*/
public static String randomString(int a) {
char[] cs = new char[a];
String pool = "";
for (short i = '0'; i <= '9'; i++) {
pool = pool + (char) i;
}
for (int h = 0; h < cs.length; h++) {
int index = (int) (Math.random() * pool.length());//產生一個pool范圍內的亂數
cs[h] = pool.charAt(index);//回傳指定索引處的字符
}
String str1 = new String(cs);
return str1;
}
}
uj5u.com熱心網友回復:
謝謝你勒大佬
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/241061.html
標籤:Eclipse
上一篇:mvcfram
