2017-07-28 45 views
0

我在Java:的Java爲Python的例子循環

public static void main(String[] args) { 

    int x = 10; 

    for(int i = 0; i <= x; i++){ 
     System.out.println("i = " + i + "******"); 
     for(int j = 0; j <= i; j++){ 
      System.out.print("j = " + j + " "); 
     } 

    } 

與輸出:

run: 
i = 0****** 
j = 0 i = 1****** 
j = 0 j = 1 i = 2****** 
j = 0 j = 1 j = 2 i = 3****** 
j = 0 j = 1 j = 2 j = 3 i = 4****** 
j = 0 j = 1 j = 2 j = 3 j = 4 i = 5****** 
j = 0 j = 1 j = 2 j = 3 j = 4 j = 5 i = 6****** 
j = 0 j = 1 j = 2 j = 3 j = 4 j = 5 j = 6 i = 7****** 
j = 0 j = 1 j = 2 j = 3 j = 4 j = 5 j = 6 j = 7 i = 8****** 
j = 0 j = 1 j = 2 j = 3 j = 4 j = 5 j = 6 j = 7 j = 8 i = 9****** 
j = 0 j = 1 j = 2 j = 3 j = 4 j = 5 j = 6 j = 7 j = 8 j = 9 i = 10****** 
j = 0 j = 1 j = 2 j = 3 j = 4 j = 5 j = 6 j = 7 j = 8 j = 9 j = 10 

我想實現的Python for循環究竟效果:

x = 10 

for i in range(0, x, 1): 
    print("i = ", i, "*******") 
    for j in range(0, i, 1): 
     print("j = ", j, end="") 

輸出:

i = 0 ******* 
i = 1 ******* 
j = 0i = 2 ******* 
j = 0j = 1i = 3 ******* 
j = 0j = 1j = 2i = 4 ******* 
j = 0j = 1j = 2j = 3i = 5 ******* 
j = 0j = 1j = 2j = 3j = 4i = 6 ******* 
j = 0j = 1j = 2j = 3j = 4j = 5i = 7 ******* 
j = 0j = 1j = 2j = 3j = 4j = 5j = 6i = 8 ******* 
j = 0j = 1j = 2j = 3j = 4j = 5j = 6j = 7i = 9 ******* 
j = 0j = 1j = 2j = 3j = 4j = 5j = 6j = 7j = 8 

我開始學習python,我知道java在中級水平,不能開始思考那些pyhon循環。 Python的3.6

+0

'九月= 「」,結束=」「'應該在你的情況下工作。參數'sep'之間的默認分隔被設置爲'「」'。 Python範圍不包含最終值,因此循環將不得不轉到'x + 1'和'i + 1' –

回答

6

上限的range(..)是獨家,所以你只需要添加一個到上限

x = 10 

for i in range(0, x+1): 
    print("i = ",i,"*******",sep='') 
    for j in range(0, i+1): 
     print("j = ",j, sep='', end=' ')

如果步1,你沒有提這。默認情況下,Python使用1作爲步驟。

此外,默認Python將分開兩個參數與一個空格,您可以使用sep參數拆分它沒有空間(或另一個字符序列)。

此打印:

i = 0******* 
j = 0 i = 1******* 
j = 0 j = 1 i = 2******* 
j = 0 j = 1 j = 2 i = 3******* 
j = 0 j = 1 j = 2 j = 3 i = 4******* 
j = 0 j = 1 j = 2 j = 3 j = 4 i = 5******* 
j = 0 j = 1 j = 2 j = 3 j = 4 j = 5 i = 6******* 
j = 0 j = 1 j = 2 j = 3 j = 4 j = 5 j = 6 i = 7******* 
j = 0 j = 1 j = 2 j = 3 j = 4 j = 5 j = 6 j = 7 i = 8******* 
j = 0 j = 1 j = 2 j = 3 j = 4 j = 5 j = 6 j = 7 j = 8 i = 9******* 
j = 0 j = 1 j = 2 j = 3 j = 4 j = 5 j = 6 j = 7 j = 8 j = 9 i = 10******* 
j = 0 j = 1 j = 2 j = 3 j = 4 j = 5 j = 6 j = 7 j = 8 j = 9 j = 10 
+0

謝謝,我現在明白了。 – Buckethead

3

你想用end=" ",而不是end=""擁有的j值,並且從下一次迭代字符串j =之間的空間。 此外,range的結果是不是包含上限,您必須加1來補償它。 最後,而不是:

print("i = ", i, "*******") 

使用

print("i = {}*******".format(i)) 

準確地再現您的Java輸出。

+0

Dziękiwielkie;) – Buckethead

0

簡單的解決你的問題。

x=10 
for i in range(0,x+1): 
print "i = "+str(i)+"******" 
for j in range(0,i+1): 
    print "j = "+str(j)+" ", 

輸出: -

i = 0****** 
j = 0 i = 1****** 
j = 0 j = 1 i = 2****** 
j = 0 j = 1 j = 2 i = 3****** 
j = 0 j = 1 j = 2 j = 3 i = 4****** 
j = 0 j = 1 j = 2 j = 3 j = 4 i = 5****** 
j = 0 j = 1 j = 2 j = 3 j = 4 j = 5 i = 6****** 
j = 0 j = 1 j = 2 j = 3 j = 4 j = 5 j = 6 i = 7****** 
j = 0 j = 1 j = 2 j = 3 j = 4 j = 5 j = 6 j = 7 i = 8****** 
j = 0 j = 1 j = 2 j = 3 j = 4 j = 5 j = 6 j = 7 j = 8 i = 9****** 
j = 0 j = 1 j = 2 j = 3 j = 4 j = 5 j = 6 j = 7 j = 8 j = 9 i = 10****** 
j = 0 j = 1 j = 2 j = 3 j = 4 j = 5 j = 6 j = 7 j = 8 j = 9 j = 10