我的任务是创建一个随机长度的A字母生成器。它必须创建50行字符串,其中包含随机数的A字母。老实说,我认为我真的很接近结论,但是,我仍然知道我缺少一些东西。目前,我的生成器正在创建随机数。
任务集中在使用for循环上。
此外-如果有比我编写的代码更容易编写我的代码的方法-我将不胜感激。
我尝试了另一种表达for循环的方式,但没有一个让我接近我的任务。
import java.util.*;
import java.lang.*;
import java.io.*;
class RandomGenerator{
private static final String alpha = "a";
private static final String data = alpha;
private static final String combinationOFA = alpha;
public static void main(String[] args) {
for (int n=0; n<50; n++){
Random theGeneratorForLength = new Random();
}
System.out.println("String : " + combinationOFA);
}
public static String generateRandomALetter(int length) {
Random theGeneratorAlpha = new Random();
StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; i++) {
int aLengthVariable =
theGeneratorAlpha.nextInt(combinationOFA.length());
char alphaTeta = combinationOFA.charAt(aLengthVariable);
sb.append(alphaTeta);
}
return sb.toString();
}
}
我希望这段代码创建50个由随机长度乘以一个字母组成的字符串
答案 0 :(得分:0)
也许这会有所帮助:
import java.util.*;
class RandomGenerator {
private static final char A = 'A';
public static void main(String[] args) {
List<String> lines = new ArrayList<>();
Random theGeneratorForLength = new Random();
for (int n = 0; n < 50; n++) {
int length = theGeneratorForLength.nextInt(50);
char[] chars = new char[length];
Arrays.fill(chars, A);
String line = new String(chars);
lines.add(line);
}
System.out.println("String : " + lines);
}
}
答案 1 :(得分:0)
像这样吗?
import java.util.*;
import java.lang.*;
class RandomGenerator {
private static final String alpha = "a";
private static final String data = alpha;
// I removed final from here. reusing your string variable for storing values.
private static String combinationOFA = alpha;
// Introduced two variable max and min to find out max and min
// characters you need to generate
private static final Integer max = 50;
private static final Integer min = 1;
public static void main(String[] args) {
for (int n = 0; n < 50; n++) {
Random theGeneratorForLength = new Random();
// you need to call method you created below with proper values.
// min and max are max and min random values for you
// and assigned value to combinationOFA
combinationOFA = generateRandomALetter(theGeneratorForLength.nextInt(max - min) + min);
// moved this line inside the loop to print
System.out.println("String " + (n + 1) + " :" + combinationOFA);
}
}
public static String generateRandomALetter(int length) {
Random theGeneratorAlpha = new Random();
StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; i++) {
int aLengthVariable =
theGeneratorAlpha.nextInt(combinationOFA.length());
char alphaTeta = combinationOFA.charAt(aLengthVariable);
sb.append(alphaTeta);
}
return sb.toString();
}
}
答案 2 :(得分:0)
使用来自Apache Commons Lang lib的RandomStringUtils
public class RandomStringGenerator {
public static void main(String [] args) {
char[] charSet= {'a','b','c','d'};
for(int i=0;i<50;i++){
int length = (int)(Math.random()*100);
System.out.println(RandomStringUtils.random(length,charSet));
}
}
}
答案 3 :(得分:0)
public static void main(String[] args) {
int maximumSymbolsInString = 50;
int countOfStrings = 10;
List<String> strings = new Random().ints(0, maximumSymbolsInString)
.limit(countOfStrings)
.mapToObj(el-> new String(new char[maximumSymbolsInString])
.replace('\0', 'A')
.substring(el))
.collect(Collectors.toList());
for (String string : strings) {
System.out.println(string);
}
}
在Java 11中,可以为每个字符串生成使用String类的新方法:repeate(int)。像"A".repeate(50)
答案 4 :(得分:0)
我已经编写了带有循环的基本程序。这是您想要的输出吗?
import java.util.Random;
class RandomString {
private static final String alpha = "a";
public static void main(String[] args) {
int length = 0;
for (int n = 0; n < 50; n++) {
Random l = new Random();
length = l.nextInt(50) + 1;
for (int i = 0; i < length; i++) {
StringBuilder sb = new StringBuilder();
sb = sb.append(alpha);
System.out.print("String: "+sb.toString());
}
System.out.println();
}
}
}