我試過下面 http://mynotes.wordpress.com/2009/07/23/java-generating-random-string/隨機文本[已解決]
我努力去適應了代碼爲我使用本網站的代碼。生成以下結果
但get
方法不起作用。這裏是我的代碼:
/*
* Retorna nomes e sobrenomes
*/
package gamification;
import java.util.Random;
public class Textos {
/*
* http://mynotes.wordpress.com/2009/07/23/java-generating-random-string/
*/
public static char[] alphanumeric = alphanumeric();
public static Random rnd = new Random();
// public CRUD() {this(null);}
// public CRUD(Random rand) {this.rand = (rand != null) ? rand : new Random();}
public static String getTexto(int len) {
StringBuffer out = new StringBuffer();
while (out.length() < len) {
int idx = Math.abs((rnd.nextInt() % alphanumeric.length));
out.append(alphanumeric[idx]);
}
return out.toString();
}
// create alphanumeric char array
private static char[] alphanumeric() {
StringBuffer buf = new StringBuffer(128);
for (int i = 48; i <= 57; i++) {
buf.append((char) i); // 0-9
}
for (int i = 65; i <= 90; i++) {
buf.append((char) i); // A-Z
}
for (int i = 97; i <= 122; i++) {
buf.append((char) i); // a-z
}
return buf.toString().toCharArray();
}
// Metodo que gera n registros aleatorios
public static void gerarRegistrosAleatorios(ArrayList lista, int n)
throws ParseException {
final long intervalo = 1000000000;
for (int i = 0; i < n; i++) {
Usuario u = new Usuario(proximoID(lista));
Random rnd = new Random();
String nomeComposto = Textos.getNomes()[rnd.nextInt(Textos.getNomes().length)]
+ " " + Textos.getSobrenomes()[rnd.nextInt(Textos.getSobrenomes().length)];
u.nome = nomeComposto;
u.pontos = rnd.nextInt(3000);
u.perguntas = new ArrayList<Pergunta>();
for (int j = 0; j < rnd.nextInt(10); j++) {
Pergunta p = new Pergunta();
p.Pergunta = Textos.getTexto(15);
p.resposta = new ArrayList<Resposta>();
for (int k = 0; k < rnd.nextInt(5); k++) {
Resposta r = new Resposta();
r.Resposta = Textos.getTexto(20);
p.resposta.add(r);
}
u.perguntas.add(p);
}
calcularEmblemas(u);
// para dataP usar exemplo de prest\exercicios\random04DiferencaDataVetor
// Vetores usados para receber os valores de getNewData
int vE[];
int vS[];
do {
vE = getNewData();
vS = getNewData();
} while (vS[0] < vE[0]);
/*
* Transforma os dados numa String para atribuir aos campos
* horaEntrada e horaSaida. No caso, foi considerado uma diferenca
* apenas nas horas e minutos, dia, mes e ano sao os mesmos. hh:mm
* dd/MM/yyyy
*/
String dateStringE = vE[0] + ":" + vE[1] + " " + vE[2] + "/" + vE[3] + "/" + vE[4];
String dateStringS = vS[0] + ":" + vS[1] + " " + vE[2] + "/" + vE[3] + "/" + vE[4];
u.horaEntrada = dateStringE;
u.horaSaida = dateStringS;
lista.add(u);
}
}
我的代碼更完整。
最新錯誤? – PermGenError
'get(int len)'方法無法工作?我們甚至無法考慮如何解決它,而不知道它是如何工作的,以及它是如何工作的。 – Vulcan
我在代碼中看不到明顯的錯誤。也許如果你告訴我們出了什麼問題,我們會知道該找什麼。 – Jay