0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

rustとscalaとkotilnの勉強 数当てゲーム(kotlin)

Posted at

TRPLの数当てゲームをkotlinで

import java.util.Random
import java.util.Scanner

fun main(args:Array<String>){
	val secret_number = Random().nextInt(99) + 0
	println("秘密の数字は次の通り:${secret_number}")
	println("数をあててごらん")
	println("ほら予想を入力してね")

	loop@while(true){
		println("in")
		val s = Scanner(System.`in`)
		val guess = s.nextLine()
		if (guess != null){
			println("次のように予想しました${guess}")
			try{
				val n_guess = Integer.parseInt(guess)
				when {
					n_guess == secret_number -> {
						println("正解")
						break@loop
					}
					n_guess > secret_number -> println("おおきすぎる")
					n_guess < secret_number -> println("ちいさすぎる")
				}
			}catch(e:Exception){
				println(e.toString())
			}
		}
	}
}

実はgradlew runで実行すると動かない。
ScannerのnextLineあたりでおかしくなる。もともとはreadLineだったんだけどそっちもだめ.
gradleとおさずに直接実行するとうまくうごく。
なんなんだろ。

kotlinはふつうにtry catch
maybeとかoptionとかeitherとかはなし
正直この程度しか書いてないとちょっと文法が変わったJavaという感想。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?