包含交替 words/numbers 的字符串 - 根据计数打印单词
String containing alternating words/numbers - printing words according to count
我有一个 Java 字符串,其中包含以下互换的数字和单词:
String str = "7 first 3 second 2 third 4 fourth 2 fifth"
我需要找到一种简洁的方式(如果可能的话)来打印单词(第一、第二、第三、第四、第五等)显示的次数。
预期输出为:
firstfirstfirstfirstfirstfirstfirst
secondsecondsecond
thirdthird
fourthfourthfourthfourth
fifthfifth
我尝试将字符串拆分为一个数组,然后使用 for 循环遍历所有其他数字(在我的外循环中)和我的内循环中的所有其他单词,但我没有成功。
这是我尝试过的方法,但我怀疑这不是正确的方法,或者至少不是最简洁的方法:
String[] array = {"7", "first", "3", "second", "2", "third", "4", "fourth", "2", "fifth"};
for (int i=0; i < array.length; i+=2)
{
// i contains the number of times (1st, 3rd, 5th, etc. element)
for (int j=1; j < array.length; j+=2)
// j contains the words (first, second, third, fourth, etc. element)
System.out.print(array[j]);
}
我会第一个承认我在 Java 非常引人注目,所以如果这种方法完全愚蠢,请随意笑,但在此先感谢您的帮助。
将数字解析为 int
,然后使用此值根据需要多次打印单词:
String[] array = {"7", "first", "3", "second", "2", "third", "4", "fourth", "2", "fifth"};
for (int i=0; i < array.length; i+=2)
{
int count = Integer.parseInt(array[i]);
for (int j=0; j < count; j++) {
System.out.print(array[i+1]);
}
System.out.println();
}
count
将得到值 7、3、2 等
考虑到您的解决方案,主要问题在于您不需要迭代考虑内部循环内的初始字符串数组。相反,您应该阅读数字并将其视为限制进行迭代。如下,例如:
String initialString = "7 first 3 second 2 third 4 fourth 2 fifth";
String splittedStrings[] = initialString.split(" ");
for(int i = 0; i < splittedStrings.length; i = i + 2){
int times = Integer.parseInt(splittedStrings[i]);
String number = splittedStrings[i+1];
for(int j = 0; j < times; j++){
System.out.print(number);
}
System.out.println();
}
希望对您有所帮助!
我有一个 Java 字符串,其中包含以下互换的数字和单词:
String str = "7 first 3 second 2 third 4 fourth 2 fifth"
我需要找到一种简洁的方式(如果可能的话)来打印单词(第一、第二、第三、第四、第五等)显示的次数。
预期输出为:
firstfirstfirstfirstfirstfirstfirst
secondsecondsecond
thirdthird
fourthfourthfourthfourth
fifthfifth
我尝试将字符串拆分为一个数组,然后使用 for 循环遍历所有其他数字(在我的外循环中)和我的内循环中的所有其他单词,但我没有成功。
这是我尝试过的方法,但我怀疑这不是正确的方法,或者至少不是最简洁的方法:
String[] array = {"7", "first", "3", "second", "2", "third", "4", "fourth", "2", "fifth"};
for (int i=0; i < array.length; i+=2)
{
// i contains the number of times (1st, 3rd, 5th, etc. element)
for (int j=1; j < array.length; j+=2)
// j contains the words (first, second, third, fourth, etc. element)
System.out.print(array[j]);
}
我会第一个承认我在 Java 非常引人注目,所以如果这种方法完全愚蠢,请随意笑,但在此先感谢您的帮助。
将数字解析为 int
,然后使用此值根据需要多次打印单词:
String[] array = {"7", "first", "3", "second", "2", "third", "4", "fourth", "2", "fifth"};
for (int i=0; i < array.length; i+=2)
{
int count = Integer.parseInt(array[i]);
for (int j=0; j < count; j++) {
System.out.print(array[i+1]);
}
System.out.println();
}
count
将得到值 7、3、2 等
考虑到您的解决方案,主要问题在于您不需要迭代考虑内部循环内的初始字符串数组。相反,您应该阅读数字并将其视为限制进行迭代。如下,例如:
String initialString = "7 first 3 second 2 third 4 fourth 2 fifth";
String splittedStrings[] = initialString.split(" ");
for(int i = 0; i < splittedStrings.length; i = i + 2){
int times = Integer.parseInt(splittedStrings[i]);
String number = splittedStrings[i+1];
for(int j = 0; j < times; j++){
System.out.print(number);
}
System.out.println();
}
希望对您有所帮助!