刚开始学习Scala,老师布置了一个选择题项目,顺便练练手,由于刚开始学,代码格式上还是摆脱不了Java的模式
题目需求
选择题共分为:单项选择、多项选择两种类型。
选择题包括:题目(title)、选项(options)和标准答案(answers)
要求:使用继承的方式实现单项选择和多项选择的实现类;
在测试类中,分别对单项选择题和多项选择题进行测试。
父类:
package scala.选择题
/**
* id
* Title
* OPtion
*
*/
class Qus {
private var id:String=_
private var Title:String=_
private var option:String=_
var Option:Array[String]=new Array[String](4)
def Qus(id:String,Title:String): Unit ={
this.id=id
this.Title=Title
}
def getId: String = id
def setId(newId:String): Unit ={id=newId}
def getOption: String = option
def setOption(newOption:String): Unit ={option=newOption}
def getTitle: String = Title
def setTitle(newTitle:String): Unit ={Title=newTitle}
def check(answer:String): Boolean ={
return false
}
}
单选题类,
package scala.选择题
/**
* id 题号
* Title 题目
* OPtion 选项
* Answers 标准答案
* score:得分
*/
class SingleQus extends Qus{
private var id:String=_
private var Title:String=_
private var option:String=_
private var score:Int=0
private var answers:String=_
override def getOption: String = super.getOption
override def setOption(newOption: String): Unit = super.setOption(newOption)
override def getId: String = super.getId
override def setId(newId: String): Unit = super.setId(newId)
override def getTitle: String = super.getTitle
override def setTitle(newTitle: String): Unit = super.setTitle(newTitle)
def getAnswers: String = answers
def setAnswers(newAnswers: String): Unit = {answers=newAnswers}
def getScore: Int = score
def setScore(newScore: Int): Unit = {score=newScore}
def SinleQus(id:String,Title:String,answers:String): Unit ={
this.id=id
this.Title=Title
this.answers=answers
}
/**
* check方法
* @param answers
* @return
*/
override def check(answers:String): Boolean ={
//if(输入答案==答案||大写过后的答案==答案)
if( this.answers==answers || answers.toUpperCase==this.answers){
println("回答正确")
score+=3
return true
} else {
println("回答错误")
return false
}
}
}
多选题类
package scala.选择题
/**
* multiQus继承父类的属性与check方法并重写
* id 题号
* Title 题目
* OPtion 选项
* Answers 标准答案
* score:得分
*/
class MultiQus extends Qus{
private var id:String=_
private var Title:String=_
private var Answers:String=_
private var option:String=_
private var score:Int=0
override def getOption: String = super.getOption
override def setOption(newOption: String): Unit = super.setOption(newOption)
override def getId: String = super.getId
override def setId(newId: String): Unit = super.setId(newId)
override def getTitle: String = super.getTitle
override def setTitle(newTitle: String): Unit = super.setTitle(newTitle)
def getAnswers: String = Answers
def setAnswers(newAnswers: String): Unit = {Answers=newAnswers}
def getScore: Int = score
def setScore(newScore: Int): Unit = {score=newScore}
def MultiQus(id:String,Title:String,Answers:String): Unit ={
this.id=id
this.Title=Title
this.Answers=Answers
}
/**
* 定义一个排序字符串的方法
* @param newString
* @return
*/
def reverseString(newString: String): String = {
var revString = ""
val n = newString.length()
for(i <- 0 to n-1){
revString = revString.concat(newString.charAt(n-i-1).toString)
}
return revString
}
override def check(Answers:String): Boolean ={
//if(输入答案==答案||排序过后的答案==答案||大写过后的答案==答案||排序过后的答案&&大写过后的答案==答案)
if( this.Answers==Answers || reverseString(Answers)==this.Answers || Answers.toUpperCase==this.Answers||Answers.toUpperCase==this.Answers&&reverseString(Answers)==this.Answers){
println("回答正确")
score+=5
return true
} else {
println("回答错误")
return false
}
}
}
主程序类(测试)
package scala.选择题
import java.sql.{Connection, DriverManager, PreparedStatement, ResultSet}
import scala.collection.mutable.{ArrayBuffer, ListBuffer}
import scala.io.StdIn
object Test {
private val driver = "com.mysql.cj.jdbc.Driver" //驱动
private val url = "jdbc:mysql://localhost:3306/exam" //自己的ip地址:端口号/库名
private val username = "root" //用户名
private val password = "123456" //密码
private var connection: Connection = null
private var rs: ResultSet = null
private var ps: PreparedStatement = null
//加载驱动
def getConnection(): Unit = {
Class.forName(driver)
connection = DriverManager.getConnection(url, username, password)
}
def main(args: Array[String]): Unit = {
var allScore:Int=0
//创建单选题对象
var singlequs = new SingleQus
//创建多选题对象
var multiQus = new MultiQus
getConnection()
println("第一届scala客观选题现在开始,请考生登录")
//调用登录方法
login()
val selectQus = "select qus_id,qus,ans,A,B,C,D from tbl_question "
ps = connection.prepareStatement(selectQus)
rs = ps.executeQuery()
//新建答案集合,存取所有题目的答案
var ansList:ArrayBuffer[String] = new ArrayBuffer[String](4)
var strAns =""
//遍历数据库查询到的题目信息
while (rs.next()) {
println(rs.getString("qus_id") + "." + rs.getString("qus") + "\n" + "A." + rs.getString("A") + " " + "B." + rs.getString("B") + " " + "C." + rs.getString("C") + " " + "D." + rs.getString("D") + " ")
//str为遍历出来的答案长度
val str:String = rs.getString("ans")
var strans=""
//判断遍历出来的是否大于2,如果大于,则为单选题
if(str.length>2){
//对数据库中的答案进行去,运算(数据库中存的答案都是X,X,X),并遍历该集合
for(element<-str.split(",")){
strans+=element
//答案集合添加元素
ansList.append(element)
}
multiQus.setAnswers(strans)
print("请输入你的答案:")
val qusAns = StdIn.readLine()
//调用重写的多选题的check方法,并对用户输入的答案作排序,例如,bca->abc
multiQus.check(multiQus.reverseString(qusAns))
}
//判断遍历出来的答案长度是否小于2,如果小于则为单选题
if(str.length<2){
singlequs.setAnswers(rs.getString("ans"))
print("请输入你的答案:")
val qusAns = StdIn.readLine()
//调用单选题类重写的check方法
singlequs.check(qusAns)
//答案集合添加元素
ansList.append(rs.getString("ans"))
}
//退出循环,输出题号和答案信息
strAns+=(rs.getString("qus_id")+"."+rs.getString("ans")+" ")
}
print("正确答案应该是:"+strAns)
// for(ele<-ansList)
// print(ele+" ")
//计算总得分并输出
allScore=singlequs.getScore+multiQus.getScore
println("你此次考试的总分为"+allScore)
}
/**
* 定义一个登录方法
*/
def login():Any = {
print("请输入你的用户名:")
val uname = StdIn.readLine()
print("请输入你的密码:")
val upwd = StdIn.readLine()
val selectSqlString = "select username,password from user where username = ? and password = ?"
ps = connection.prepareStatement(selectSqlString)
ps.setString(1,uname)
ps.setString(2,upwd)
rs = ps.executeQuery()
if(rs.next()){//与数据库数据进行比对
println("scala客观题考试正式开始")
println("本次试题非为单项选择和多项选择两个部分,1~3题为单项选择,4~5题为多选题。单选题一题3分,多选题一题5分,总分为19分"
)
}
//递归
else{
println("登录失败,请重新登录")
login()
}
}
}
注释写的很清楚了,这里就不作一些杂乱的解释了,写的不怎么好
以下是数据库表
测试结果: