Kotlin中的小游戏——计分问题
Small game in Kotlin - counting score problem
我有一个创造一只狗和一只猫的练习class。这些 classes 中的每一个都应该包含相同的字段(重量、速度、声音、强度)。然后写一个函数,让 return 成为决斗的赢家——它给在特定领域具有更大价值的动物一分(例如 cat.weigh > dog.weigh -> cat has 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 的回答是正确的,但我也会对您的代码进行一些改进以避免代码复制:
- 定义具有不可变 val 属性的 class
Animal
,添加 AnimalType
。无需定义两个具有相同属性的相同 class。
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
}
- 将您的“whoWins”函数重构为 return 获胜者及其分数:
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)
}
}
- 通过在 returned 获胜者上使用 运行 函数来简化您的主要任务:
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}!")
}
}
我有一个创造一只狗和一只猫的练习class。这些 classes 中的每一个都应该包含相同的字段(重量、速度、声音、强度)。然后写一个函数,让 return 成为决斗的赢家——它给在特定领域具有更大价值的动物一分(例如 cat.weigh > dog.weigh -> cat has 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 的回答是正确的,但我也会对您的代码进行一些改进以避免代码复制:
- 定义具有不可变 val 属性的 class
Animal
,添加AnimalType
。无需定义两个具有相同属性的相同 class。
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
}
- 将您的“whoWins”函数重构为 return 获胜者及其分数:
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)
}
}
- 通过在 returned 获胜者上使用 运行 函数来简化您的主要任务:
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}!")
}
}