我试过下面 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