Home > Enterprise >  java - how do I print out the words in increasing magnitudes, e.g 1st = Our, 2nd = Our greatest, 3rd
java - how do I print out the words in increasing magnitudes, e.g 1st = Our, 2nd = Our greatest, 3rd

Time:03-02

String s2 = "Our greatest glory is not in never falling, but in rising every time we fall.";

String[] split1 = s2.split(" ");

for (String s3 : split1) { System.out.println(s3 );

   }
  

CodePudding user response:

This is the implementatio:

String s2 = "Our greatest glory is not in never falling, but in rising every time we fall.";
String[] split1 = s2.split(" ");

for (int i = 0; i < split1.length; i  ) {
    for (int j = 0; j < i   1; j  ) {
        System.out.print(split1[j]   " ");
    }
    System.out.println();
}

Output:

Our 
Our greatest 
Our greatest glory 
Our greatest glory is 
Our greatest glory is not 
Our greatest glory is not in 
Our greatest glory is not in never 
Our greatest glory is not in never falling, 
Our greatest glory is not in never falling, but 
Our greatest glory is not in never falling, but in 
Our greatest glory is not in never falling, but in rising 
Our greatest glory is not in never falling, but in rising every 
Our greatest glory is not in never falling, but in rising every time 
Our greatest glory is not in never falling, but in rising every time we 
Our greatest glory is not in never falling, but in rising every time we fall. 
  •  Tags:  
  • java
  • Related