如何实现以下逻辑的模式?

huangapple go评论67阅读模式
英文:

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

得分: 2

这里您需要再引入一个 for 循环以获得您所需的输出。

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

得分: 1

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.

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

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定