java - 我在 for 循环中大写了太多元素,但仅在特定索引上
问题描述
我不知道为什么,但在这种方法中,第 5 和第 10 个元素每次都被大写。我找不到原因。例如:传递“ZpglnRxqenU”作为参数应该返回:“Z-Pp-Ggg-Lllll-Nnnnn-Rrrrrr-Xxxxxxx-Qqqqqqqq-Eeeeeeeee-Nnnnnnnnnnn-Uuuuuuuuuuu”,而是返回:“Z-Pp-Ggg-Llll-Nnnnn -RRRRRR-Xxxxxxx-Qqqqqqqq-Eeeeeeeee-Nnnnnnnnnn-UUUUUUUUUUU"
请注意,与其他字符不同,R 和 U 都是大写的。
/**
* The purpose of this method is to receive a string, deconstruct it by characters
* and save it in array, then iterate through this array creating a new string
* where each character will be represented the number of times equivalent to its
* position in the array + 1 while upperCasing every 1st occurrence of the character
* and separating every set of character repetitions with "-".
* @param string
* @return String
*/
public String builder (String string){
/*
Here we split the string we receive by characters and save these characters in
a new array called strArr.
*/
String[] strArr = string.split("");
String finalString = "";
/*
We iterate through the array of characters adding "-" each time we start to
represent a new character.
We add the characters N times where N is the order of appearance in the string
we receive as param.
*/
for (int i = 0; i < strArr.length; i++) {
if(i > 0){
finalString += "-";
}
for (int j = 0; j < i + 1; j++) {
if(j == 0){
/*
this is upperCasing every 5th and 10th character for some reason
*/
// TODO: 26/12/2019 stop upperCasing 5th and 10th element.
finalString += strArr[i].toUpperCase();
continue;
}
finalString += strArr[i];
}
}
return finalString;
解决方案
你必须首先添加这一行: string = string.toLowerCase();
公共字符串生成器(字符串字符串){
string = string.toLowerCase();
/*
Here we split the string we receive by characters and save these characters in
a new array called strArr.
*/
String[] strArr = string.split("");
String finalString = "";
/*
We iterate through the array of characters adding "-" each time we start to
represent a new character.
We add the characters N times where N is the order of appearance in the string
we receive as param.
*/
for (int i = 0; i < strArr.length; i++) {
if(i > 0){
finalString += "-";
}
for (int j = 0; j < i + 1; j++) {
if(j == 0){
/*
this is upperCasing every 5th and 10th character for some reason
*/
// TODO: 26/12/2019 stop upperCasing 5th and 10th element.
finalString += strArr[i].toUpperCase();
continue;
}
finalString += strArr[i];
}
}
return finalString;
}
推荐阅读
- python-3.x - 如何在 Python 中使用 Google API v3 使驱动器文件夹“对网络公开”?
- python - 使用 Python 和 Selenium 时在 for 循环中按顺序执行步骤
- swift - FirebaseFirestoreSwift 不会安装(CocoaPods)
- c# - RestClient 正在返回 StatusCode OK 但长度为 -1
- opengl - 如何反转 3D look_at 函数?
- r - 如何找到未存储在 .GlobalEnv 中的重物?
- android - 对象不是抽象的,没有实现抽象成员 public abstract fun onClick(p0: View!): Unit
- java - SQL 错误:java.io.IOException:java.lang.IllegalArgumentException:bucketId 超出范围:-1
- elasticsearch - 如何在 Kibana 中的两个过滤器之间聚合数据?
- reactjs - 无法将时间和数据作为输入并写入使用 Hasura GraphQL 实现的数据库