我是编码新手,我正在学习Scala。我的练习如下,但我觉得我的代码太冗长了。我该如何改进呢?
练习:
*网络电话服务提供商在计算机和未连接到互联网的普通电话之间提供呼叫服务。编写一个脚本,帮助人们确定他们的通话费用将根据他们希望在一个月内通话的时间,他们是使用按月订阅还是按使用付费服务,他们将在哪个国家/地区进行这些电话,以及是否会拨打移动电话和固定电话,或只拨打固定电话。使用以下信息来帮助您完成任务:
Lewis,Mark C。; Lacher,Lisa。使用Scala编程和解决问题的简介,第二版(Chapman& Hall / CRC教科书计算)(页74)。 CRC出版社。 Kindle版。*
$drivers = DB::table('drivers')
->join('bank','bank.driver_id','=','drivers.driver_id')
->where('city', 'like', "$keywords[0]%")
->Where('first_name', 'like', "$keywords[1]%")
->orWhere('bank.bank_name', 'like', "$keywords[3]%")
->get();
答案 0 :(得分:0)
只需简单的模式匹配即可完成清理工作:
object callingCost {
def main(args: Array[String]): Unit = {
//user input
print("Will you be using (1)Monthly or (2)Pay-As-You-Go ")
val planType = readInt()
print("What country will you be calling? ")
val country = readLine()
print("How many minutes would you talk per month? ")
val minsMonth = readInt()
print("Will you be calling both Mobile and Landlines as opposed to just landlines? (Y/N) ")
val lineType = readLine()
//declare monthly subscription cost per country
val cost = (planType, country, minsMonth, lineType) match {
case (1, "China", m, _) if m <= 120 => 1.19
case (1, "China", m, _) if m <= 400 => 3.89
case (1, "China", m, _) if m <= 800 => 7.79
case (1, "China", _, _) => 13.99
case (1, "India", m, _) if m <= 120 => 1.42
case (1, "India", m, _) if m <= 800 => 8.99
case (1, "India", _, _) => 19.99
case (1, "UK", _, "Y") => 2.09
case (1, "UK", _, _) => 1.19
case (1, "Mexico", _, _) => 1.79
case (1, "USA", _, _) => 2.99
case (2, "China", m, _) => m * 0.02
case (2, "India", m, _) => m * 0.015
case (2, "USA", m, _) => m * 0.023
case (2, "Mexico", m, "Y") => m * 0.035
case (2, "Mexico", m, "N") => m * 0.01
case (2, "UK", m, "Y") => m * 0.10
case (2, "UK", m, _) => m * 0.023
}
println(s"Your projected cost per month is $cost")
}
}