# 如何实现以下逻辑的模式？

go评论54阅读模式

How to implements pattern for below logic?

# 问题

``````我尝试了以下代码

``````

public static void rightTriangle(int n) {
int rows, i, j=0;
String s = "从纽约到英国的廉价航班";
String[] arr = s.split("\s");
for (i = 0; i <= arr.length - 1; i++) {
for (j = 0; j <= i; j++)
System.out.print(arr[j] + " ");
System.out.println("");
}
}

``````

&lt;pre&gt;

&lt;/pre&gt;
&lt;pre&gt;

&lt;/pre&gt;
``````

I tried with

``````public static void rightTriangle(int n) {
int rows, i, j=0;
String s = &quot;Cheap flights from New York to United Kingdom&quot;;
String[] arr = s.split(&quot;\\s&quot;);
for (i = 0; i &lt;= arr.length - 1; i++) {
for (j = 0; j &lt;= i; j++)
System.out.print(arr[j] + &quot; &quot;);
System.out.println(&quot;&quot;);
}
}
``````

And I got
<pre>
Cheap
Cheap flights
Cheap flights from
Cheap flights from New
Cheap flights from New York
Cheap flights from New York to
Cheap flights from New York to United
Cheap flights from New York to United Kingdom

</pre>
<pre>
I
I am
I am a
I am a girl

am
am a
am a girl

a
a girl

girl
</pre>

# 答案1

``````String s = "纽约到英国的廉价航班";
String[] arr = s.split("\\s");
for (int i = 0; i < arr.length; i++) {
for (int j = i; j < arr.length; j++) {
for (int k = i; k <= j; k++) {
System.out.print(arr[k] + " ");
}
System.out.println("");
}
System.out.println("");
}
``````

Here you need to introduce one more for loop to get your desired output.

``````String s = &quot;Cheap flights from New York to United Kingdom&quot;;
String[] arr = s.split(&quot;\\s&quot;);
for (int i = 0; i &lt; arr.Length; i++)
{
for (int j = i; j &lt; arr.Length; j++)
{
for (int k = i; k &lt;= j; k++)
{
System.out.print(arr[k] + &quot; &quot;);
}
System.out.println(&quot;&quot;);
}
System.out.println(&quot;&quot;);
}
``````

# 答案2

``````public class Main {
public static void main(String[] args) {
rightTriangle(3);
}

public static void rightTriangle(int n) {
String s = "纽约飞往英国的廉价航班";
String[] arr = s.split("\\s");
for (int k = 0; k < arr.length - n; k++) {
for (int i = k; i < arr.length; i++) {
for (int j = k; j <= i; j++)
System.out.print(arr[j] + " ");
System.out.println();
}
System.out.println();
}
}
}
``````

``````public class Main {
public static void main(String[] args) {
rightTriangle(3);
}
public static void rightTriangle(int n) {
String s = &quot;Cheap flights from New York to United Kingdom&quot;;
String[] arr = s.split(&quot;\\s&quot;);
for(int k = 0;k&lt;arr.length-n;k++) {
for (int i = k; i&lt; arr.length; i++) {
for (int j = k; j &lt;= i; j++)
System.out.print(arr[j] + &quot; &quot;);
System.out.println();
}
System.out.println();
}
}

}
``````

this should produce the correct output, you could tweak it to put it in a method yourself if needed however this should work fine. You can dictate how many triangles are displayed by changing the `rightTriangle(int n)` parameter, so if you put 0 in the method call it will draw all the triangles and if you put 8 it wont draw any because there are 8 words.

• 本文由 发表于 2020年10月6日 13:08:42
• 转载请务必保留本文链接：https://go.coder-hub.com/64219694.html
• java
• string

go 57

go 68

go 62

go 60