| Date | 2011/4/10 |
|---|---|
| Name | BullsAndCows |
| Description | A number guessing game written in Scala |
| Filename | BullsAndCowsScala.scala |
| Content |
|
| Sample Input: | |
| Sample Output: | |
| Expected Test Command: | scalac BullsAndCowsScala.scala scala BullsAndCowsScala |
object BullsAndCowsScala{
import java.util.Random
import java.util.Scanner
import java.util.regex.Pattern
import scala.util.control.Breaks._
def main(args:Array[String]){
var scan=new Scanner(System.in)
var msg=""
var ans=""
var playNextGame=""
var bull=0
var cow=0
var secretDigit=new Array[Int](4)
var arr=new Array[Int](4)
println("Bulls and Cows")
println("==============")
println("This is an ancient game with guessing numbers.")
var rand=new Random()
var regex=Pattern.compile("^\\d{4}$|^quit$")
do{
var secret=""
playNextGame=""
for(i<-0 until 4){
secretDigit(i)=rand.nextInt(10)
secret+=secretDigit(i)
}
println("A secret number of 4-digit has been draw!")
do{
do{
print("Please enter a 4-digit number (or type 'quit' to exit):")
ans=scan.nextLine().trim()
}while(!regex.matcher(ans).find())
if(ans.equals("quit")){
println("The secret number is: " + secret)
}else{
cow=0
bull=0
for(i<-0 until 4)
try{
arr(i)=Integer.parseInt(ans.substring(i,i+1))
if(arr(i)==secretDigit(i)) bull+=1
}catch{
case nfe: NumberFormatException =>
}
for(i<-0 until 4)
breakable{
for(j<-0 until 4)
if(arr(j)==secretDigit(i)){
cow+=1
arr(j)= -1
break
}
}
cow-=bull
msg=""
if(cow==0 && bull==0){
msg="Neither a bull nor a cow!"
}else{
if(bull==4)
msg="4 bulls! You are a genius!"
else{
if(cow==0){
if(bull==1)
msg="1 bull only."
else
msg=bull + " bulls only."
}else if(bull==0){
if(cow==1)
msg="1 cow only."
else
msg=cow + " cows only."
}else{
if(bull==1)
msg="1 bull and "
else
msg=bull + " bulls and "
if(cow==1)
msg+="1 cow."
else
msg+=cow + " cows."
}
}
}
println(msg)
}
}while(bull!=4 && !ans.equals("quit"))
if(!ans.equals("quit")){
do{
print("Do you want to play a new game (Y/N)?")
playNextGame=scan.nextLine().trim()
}while(!playNextGame.toUpperCase().equals("Y") && !playNextGame.toUpperCase().equals("N"))
}
} while (playNextGame.toUpperCase().equals("Y") && !ans.equals("quit"))
println("Thank you for playing Bulls and Cows!")
}
}| Date | 2011/4/9 |
|---|---|
| Name | Add Two Numbers |
| Description | Add Two Numbers using Scala |
| Filename | AddTwoNumScala.scala |
| Content |
|
| Sample Input: | 123 3.5 dsfasd 4 fdsafds 6 |
| Sample Output: | 126 0 |
| Expected Test Command: | scalac AddTwoNumScala.scala scala AddTwoNumScala < testdata.txt |
object AddTwoNumScala{
import java.util.Scanner
def main(args:Array[String]){
var nCount=0
var total=0
var in=new Scanner(System.in)
while(in.hasNext()){
var temp=in.next()
try{
var num=java.lang.Integer.parseInt(temp)
if(nCount % 2==0) total=num else {
total+=num
print(total+" ")
}
nCount+=1
}catch{
case nfe: NumberFormatException =>
}
}
}
}
| Date | 2011/4/8 |
|---|---|
| Name | Hello Friend |
| Description | Ask user's name and echo greeting message to user using Scala |
| Filename | HelloFriendScala.scala |
| Content |
|
| Sample Input: | Peter Pan |
| Sample Output: | Hello, Peter Pan! |
| Expected Test Command: | scalac HelloFriendScala.scala scala HelloFriendScala < testdata.txt |
object HelloFriendScala{
def main(args:Array[String]){
var ok=true
while(ok){
print("Your name?")
var name=readLine()
ok=name!=null
if(ok) println("Hello, "+name+"!")
}
}
}
| Date | 2011/4/7 |
|---|---|
| Name | Hello World |
| Description | Display greeting message to user using Scala |
| Filename | HelloWorldScala.scala |
| Content |
|
| Sample Input: | |
| Sample Output: | Hello, World! |
| Expected Test Command: | scalac HelloWorldScala.scala scala HelloWorldScala |
object HelloWorldScala{
def main(args:Array[String]){
println("Hello, World!");
}
}