Skip to content

Instantly share code, notes, and snippets.

@kanemu
Last active December 23, 2015 00:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kanemu/6557373 to your computer and use it in GitHub Desktop.
Save kanemu/6557373 to your computer and use it in GitHub Desktop.
groovyで足し算コンソールアプリ
groovy https://gist.github.com/kanemu/6557373/raw/tasu.groovy
#!/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