Help us understand the problem. What is going on with this article?

kotlinでrandとプロパティ

More than 1 year has passed since last update.

randの使い方が分かったので使ってみた

chant.kt
import java.util.*

fun main(args: Array<String>) {
    val words = arrayOf("Lets", "go","Cena")
    val rand = Random()
    val chant = Regex(".*LetsgoCena$")
    var fans = ""
    var num = 0
    loop@while (true) {
    println(fans)
    num ++
        when (chant.matches(fans)) {
            true -> {
                println("Cena Sucks ")
                println("$num chants")
                break@loop
            }
            false ->  fans += words[rand.nextInt(words.size)]
        }
    }
}

"Lets"と"go","Cena"の三つをランダムに出力してLetsgoCenaの順になったときに"Cena Sucks"と出力して終わるもの。
RegexやmatchesはjavaのAPIから

おまけ

propa.kt
import java.util.Scanner

public class   Human(){
    public var name : String = ""
    public var age : Int = 0
    public var live : String = ""
}
fun main(arg : Array<String>) {
    val human1 = scan()
    println("name = "+human1.name)
    println("age = " +human1.age)
    println("liveing = " +human1.live)
}
fun scan(): Human {
    val input = Scanner(System.`in`)
    val human1 = Human()
    println("name")
    human1.name = input.next()
    println("age")
    human1.age = input.nextInt()
    println("livehu")
    human1.live = input.next()
    return human1
}

あんまり変わってないですね。
publicは今回はいらないけど一応。

nimani76
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away