如何条件检查像我< J in for loop kotlin

时间:2017-05-25 09:27:09

标签: for-loop kotlin

需要知道如何在for循环中编写条件检查

例如Kotlin

class StarTriangle { 

public static void main(String[] args)  { 
   int i,j,k; for(i=1; i<=5; i++) { 
   for(j=4; j>=i; j--) { 
      System.out.print(" "); 
   } 
   for(k=1; k<=(2*i-1); k++) {
      System.out.print("*"); 
   }
   System.out.println(""); 
 } 
}

3 个答案:

答案 0 :(得分:1)

尝试使用while而不是for。

#first circle {
  stroke-width: 4px;
  fill: none;
}

#first circle:nth-child(1) {
  fill: #fff;
}

输出:

<div id="first">
  <svg width="114" height="114">
    <circle stroke="#6d86c7" stroke-dashoffset="62.02111948377269" stroke-dasharray="62.02111948377269,258.42133118 23862" r="51" cx="50%" cy="50%"></circle>
    <circle stroke="#e05c5c" stroke-dashoffset="165.38965195672716" stroke-dasharray="103.36853247295448,217.07391819320443" r="51" cx="50%" cy="50%"></circle>
    <circle stroke="#f6a623" stroke-dashoffset="320.4424506661589" stroke-dasharray="155.05279870943173,165.38965195672712" r="51" cx="50%" cy="50%"></circle>
  </svg>
</div>

答案 1 :(得分:1)

你需要像这样使用for循环的iterator:

for(i=1; i<=5; i++)更改为for(i in 1..5)

var i=0
var j=0
var k=0
for(i in 1..5) {
    for(j in 4 downTo i) { 
        print(" ")
    }
    for(k in 1..(2*i-1)) { 
        print("*")
    } 
    println("")
} 

输出,

    *
   ***
  *****
 *******
*********

答案 2 :(得分:0)

我将添加其他答案,您还可以使用repeat功能:

for (i in 1..5) {
    repeat(5 - i) { print(" ") }
    repeat(2 * i - 1) { print("*") }
    println()
}