微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Kotlin 中的小游戏——计分问题

如何解决Kotlin 中的小游戏——计分问题

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

功能正确识别谁应该获胜 - 例如。用户为所有狗的值输入“20”,为猫输入“10”,因此该函数会打印“Dog Doge 有 1 分并获胜!”,但正如您所看到的,该函数计算的点数错误(狗应该有 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. 通过对返回的获胜者使用运行函数来简化您的主程序:
fun main() {
        val doge = Animal("Doge",20,10,25,50,AnimalType.DOG)
        val felix = Animal("Felix",15,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}!")
        }
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。