【发布时间】:2021-11-28 15:23:56
【问题描述】:
我有一个包含 50 个字符的 String[50],这些字符需要使用 (int)(Math.random() * 3) 放置在 String[10][10] 中,这样循环才会通过整个矩阵将字符放在最后一个位置+无论 math.random 得到什么。 老师提出的这些问题限制了您回答这些问题的方式,这让我发疯了。
public static void main(String[] args) {
Scanner entrada = new Scanner(System.in);
System.out.println("Type a phrase.");
String phrase = entrada.nextLine();
while (frase.length() > 50) {
System.out.println("The phrase has to be less than 50 characters. Try again.");
phrase = entrada.nextLine();
}
int carac = phrase.length();
String[] vector = new String[carac];
for (int i = 0; i < carac; i++) {
vector[i] = String.valueOf(phrase.charAt(i));
System.out.print(vet[i]);
}
int position = 0;
String[][] matrix = new String[10][10];
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
position = (int) (Math.random() * 3);
matrix[i][j] = vet[i] //this part doesn't make any sense, I'm stuck here.
}
}
}
所需输出示例:
【问题讨论】:
-
我是这样理解的。您需要保持 lastPos + 当前随机数的总和并将字符放在 lastPos 中。但这意味着您需要计算 lastPos 的行和列。如果lastPos = 48,那么row = 48/10 = 4 and c = 48%10 = 8。另外,你描述的方式意味着你可能会覆盖一些刚刚生成的随机数为0的字符。
-
您可能想与您的老师讨论这个问题,以便根据本文中的 cmets 对预期内容进行一些澄清。