Last active
December 23, 2015 00:59
-
-
Save kanemu/6557373 to your computer and use it in GitHub Desktop.
groovyで足し算コンソールアプリ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
groovy https://gist.github.com/kanemu/6557373/raw/tasu.groovy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env groovy | |
def rnd = new Random() | |
int num1 | |
int num2 | |
String question | |
int ans | |
BufferedReader br | |
Map incorrectQ = [:] | |
label:for(;;){ | |
num1 = rnd.nextInt(10) | |
num2 = rnd.nextInt(10) | |
ans = num1+num2 | |
question = "$num1 + $num2 = $ans" | |
println "> $num1 + $num2 = ?" | |
for(;;){ | |
br = new BufferedReader(new InputStreamReader(System.in)) | |
def rtn = br.readLine() | |
if(rtn=="$ans"){ | |
println 'That is right!\n' | |
break | |
}else{ | |
if( rtn =~ /^[qQ]/ ){ | |
println '> 間違えた回答' | |
incorrectQ.each{k,v-> | |
println "$k ... $v" | |
} | |
println 'bye.' | |
break label | |
}else{ | |
if(!incorrectQ[question]){ | |
incorrectQ[question]=0 | |
} | |
incorrectQ[question]+=1 | |
println '> incorrect.' | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment