包含交替单词/数字的字符串 - 根据计数打印单词[重复]

时间:2022-09-13 11:33:16

This question already has an answer here:

这个问题在这里已有答案:

I have a Java string that contains numbers and words interchanged throughout as follows:

我有一个Java字符串,其中包含数字和单词互换,如下所示:

String str = "7 first 3 second 2 third 4 fourth 2 fifth"

I need to find a concise way (if possible) to print the words (first, second, third, fourth, fifth, etc.) the number of times shown.

我需要找到一种简洁的方法(如果可能的话)来打印所显示的单词(第一,第二,第三,第四,第五等)。

The expected output is:

预期的输出是:

firstfirstfirstfirstfirstfirstfirst
secondsecondsecond
thirdthird
fourthfourthfourthfourth
fifthfifth

I tried to split the the string into an array and then use a for loop to iterate over every other number (in my outer loop) and every other word in my inner loop, but I was not successful.

我试图将字符串分割成一个数组,然后使用for循环遍历所有其他号码(在我外环),并在我的内循环每隔词,但我没有成功。

Here is what I tried but I suspect this is not the correct approach or at the very least not the most concise one:

这是我尝试的但我怀疑这不是正确的方法,或者至少不是最简洁的方法:

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]);

}

I'll be the first to admit I am very much a notice at Java, so if this approach is completely asinine please feel free to laugh, but thank you in advance for your assistance.

我将是第一个承认我非常注意Java的人,所以如果这种方法完全是asinine请随意笑,但提前感谢您的帮助。

2 个解决方案

#1


0  

Parse the number as an int, and then use this value to print the word as many times as you need:

将数字解析为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 will get the values 7, 3, 2, etc.

count将获得值7,3,2等。

#2


1  

Considering your solution, the main problem consists in that you don't need to iterate considering the initial string array inside the inner loop. Rather, you should read the numeric digit and iterate considering it as a limit. As follows, for example:

考虑到您的解决方案,主要问题在于您不需要考虑内部循环内的初始字符串数组进行迭代。相反,您应该读取数字并迭代将其视为限制。如下,例如:

    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();
    }

Hope it helps!

希望能帮助到你!

#1


0  

Parse the number as an int, and then use this value to print the word as many times as you need:

将数字解析为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 will get the values 7, 3, 2, etc.

count将获得值7,3,2等。

#2


1  

Considering your solution, the main problem consists in that you don't need to iterate considering the initial string array inside the inner loop. Rather, you should read the numeric digit and iterate considering it as a limit. As follows, for example:

考虑到您的解决方案,主要问题在于您不需要考虑内部循环内的初始字符串数组进行迭代。相反,您应该读取数字并迭代将其视为限制。如下,例如:

    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();
    }

Hope it helps!

希望能帮助到你!