【发布时间】:2023-03-10 20:47:02
【问题描述】:
我有一个字符串作为输入,我想将整个字符串转换为小写,除了一个需要大写的随机字母。
我尝试了以下方法: splited 是输入字符串数组
word1 = splited[0].length();
word2 = splited[1].length();
word3 = splited[2].length();
int first = (int) Math.random() * word1;
String firstLetter = splited[0].substring((int) first, (int) first + 1);
String lowcase1 = splited[0].toLowerCase();
char[] c1 = lowcase1.toCharArray();
c1[first] = Character.toUpperCase(c1[first]);
String value = String.valueOf(c1);
System.out.println(value);
当我尝试打印字符串时,它总是将第一个字母返回为大写,而字符串的其余部分为小写。为什么返回的不是随机字母而是第一个字母。
干杯
【问题讨论】:
-
显然,
first始终是0。那么,word1是什么? -
first是使用Math.random() * word1随机生成的。word1是length of the string -
是的,我可以看到。
word1的值是多少? -
为什么first总是0?我以为是
Math.random() * range -
word1的值是6因为字符串“author”