Kotlin的小游戏-计算分数的问题



我有一个excersise来创建一个dog和一个cat类。每个类都应该包含相同的字段(重量、速度、声音、强度(。然后编写一个函数,返回决斗的获胜者-它为在特定领域具有更大价值的动物提供一分(例如,猫的体重>狗的体重->猫的体重为1分(。得分最高的动物获胜。此外,我希望用户可以在控制台中输入特定的值。

该功能正确地识别谁应该获胜——例如;20〃;对于所有狗的值;10〃;对于cat,因此函数打印";Dog Doge得了1分并获胜&";,但正如您所看到的,函数计算的点数是错误的(dog应该有4点,而不是1点(。

整个代码:

open class Cat(name: String)
{
var name = name
var weight = 1
var speed = 1
var voice = 1
var strength = 1
}
class Dog(name: String) : Cat(name)
{}
fun whoWins(dog1: Dog, cat1: Cat)
{
var scoreCat = 0
var scoreDog = 0
if (dog1.strength == cat1.strength)
{ scoreDog =+ 0; scoreCat =+ 0}
else if (dog1.strength > cat1.strength)
{ scoreDog =+ 1; scoreCat=+ 0}
else { scoreDog=+ 0; scoreCat =+ 1}
if (dog1.speed == cat1.speed)
{ scoreDog =+ 0; scoreCat =+ 0}
else if (dog1.speed > cat1.speed)
{ scoreDog =+ 1; scoreCat=+ 0}
else { scoreDog=+ 0; scoreCat =+ 1}
if (dog1.weight == cat1.weight)
{ scoreDog =+ 0; scoreCat =+ 0}
else if (dog1.weight > cat1.weight)
{ scoreDog =+ 1; scoreCat=+ 0}
else { scoreDog=+ 0; scoreCat =+ 1}
if (dog1.voice == cat1.voice)
{ scoreDog =+ 0; scoreCat =+ 0}
else if (dog1.voice > cat1.voice)
{ scoreDog =+ 1; scoreCat=+ 0}
else { scoreDog=+ 0; scoreCat =+ 1}
if (scoreCat == scoreDog)
{ println("Draw! Cat ${cat1.name} and dog ${dog1.name} has the same score!")}
else if (scoreCat > scoreDog)
{ println("Cat ${cat1.name} has $scoreCat points and wins!")}
else
{ println("Dog ${dog1.name} has $scoreDog points and wins!")}
}
fun main()
{
var Doge= Dog("Doge")
var Felix = Cat("Felix")
println("Enter ${Doge.name}’s weight:")
Doge.weight = readLine()!!.toInt()
println("Enter ${Doge.name}’s speed:")
Doge.speed = readLine()!!.toInt()
println("Enter ${Doge.name}’s voice strength:")
Doge.voice= readLine()!!.toInt()
println("Enter ${Doge.name}’s strength:")
Doge.strength = readLine()!!.toInt()
println("Enter ${Felix.name}’s weight:")
Felix.weight = readLine()!!.toInt()
println("Enter ${Felix.name}’s speed:")
Felix.speed = readLine()!!.toInt()
println("Enter ${Felix.name}’s voice strength:")
Felix.voice = readLine()!!.toInt()
println("Enter ${Felix.name}’s strength:")
Felix.strength = readLine()!!.toInt()
whoWins(Doge, Felix)
}

添加和更新变量值的正确方法是使用plusAssign运算符+=。您使用的是反向=+,这实际上意味着您每次都分配正值1。

您可以在此处阅读有关语法的更多信息。https://kotlinlang.org/docs/reference/operator-overloading.html#assignments

您可以通过使用++来增加值来简化代码。另外,你不需要指定一个值增加0,只是不要修改它

fun whoWins(dog1: Dog, cat1: Cat)
{
var scoreCat = 0
var scoreDog = 0
if (dog1.strength > cat1.strength)
{ scoreDog++ }
else {scoreCat++}
if (dog1.speed > cat1.speed)
{ scoreDog++}
else {scoreCat++}

if (dog1.weight > cat1.weight)
{ scoreDog++}
else { scoreCat++}

if (dog1.voice > cat1.voice)
{ scoreDog++}
else { scoreCat++}

if (scoreCat == scoreDog)
{ println("Draw! Cat ${cat1.name} and dog ${dog1.name} has the same score!")}
else if (scoreCat > scoreDog)
{ println("Cat ${cat1.name} has $scoreCat points and wins!")}
else
{ println("Dog ${dog1.name} has $scoreDog points and wins!")}
}

@Rishabh Kohli的答案是正确的,但我也会对您的代码进行一些改进,以避免代码复制:

  1. 定义一个具有不可变val属性的类Animal,并添加一个AnimalType。无需定义具有相同属性的两个相同类
class Animal(
val name: String,
val weight: Int,
val speed: Int,
val voice: Int,
val strength: Int,
val type: AnimalType
)
enum class AnimalType {
DOG, CAT
}
  1. 重构您的"whoWins";函数返回获胜者及其分数:
fun whoWins(dog: Animal, cat: Animal): Pair<Animal?, Int?> {
var catScore = 0
var dogScore = 0
when {
dog.strength > cat.strength -> dogScore++
dog.strength < cat.strength -> catScore++
}
when {
dog.speed > cat.speed -> dogScore++
dog.speed < cat.speed -> catScore++
}
when {
dog.weight > cat.weight -> dogScore++
dog.weight < cat.weight -> catScore++
}
when {
dog.voice > cat.voice -> dogScore++
dog.voice < cat.voice -> catScore++
}
return when {
catScore < dogScore -> Pair(dog, dogScore)
catScore > dogScore -> Pair(cat, catScore)
else -> Pair(null, catScore)
}
}
  1. 通过对返回的获胜者使用run函数来简化您的main:
fun main() {
val doge = Animal("Doge", 20, 10, 25, 50, AnimalType.DOG)
val felix = Animal("Felix", 10, 50, 15, 10, AnimalType.CAT)
val fight = whoWins(doge, felix)
fight.first?.run {
println("${this.type} ${this.name} has ${fight.second} points and wins!")
} ?: kotlin.run {
println("Animals have the same score: ${fight.second}!")
}
}

最新更新