我最近在 kotlin 中编写了下面的代码,使其成为是或否问题,但是当用户回答是或否时,它不会进入下一个

问题描述 投票:0回答:1
fun basicResponses(_message: String): String {

      
        val message =_message.toLowerCase()
        
        return when {

            message.contains("Yes")||message.contains("yes") || message.contains("YES") || message.contains("No")||message.contains("no")||message.contains("NO") -> {

                "Question 1:Do you Have ...?"

            }

            message.contains("Yes")||message.contains("yes") || message.contains("No")||message.contains("no")||message.contains("NO")-> {
                "Question 2:Do you Have ... ?"
            }

            message.contains("Yes")||message.contains("yes") || message.contains("No")||message.contains("no")||message.contains("NO")-> {
                "Question 3:Do you feel ... ?"
            }

            message.contains("Yes")||message.contains("yes") || message.contains("No")||message.contains("no")||message.contains("NO") -> {
                "Question 4:Do you Have ... ?"
            }

            message.contains("Yes")||message.contains("yes") || message.contains("No")||message.contains("no")||message.contains("NO") -> {
                "Question 5:Do you Have ... ?"
            }

            message.contains("Yes")||message.contains("yes") || message.contains("No")||message.contains("no")||message.contains("NO") -> {
                "Question 6:Do you Have ... ?"
            }

            message.contains("Yes")||message.contains("yes") || message.contains("No")||message.contains("no")||message.contains("NO") -> {
                "Question 7:Do you Have... ?"
            }

            message.contains("Yes")||message.contains("yes") || message.contains("No")||message.contains("no")||message.contains("NO") -> {
                "Question 8:Do you feel ... ?"
            }

            message.contains("Yes")||message.contains("yes") || message.contains("No")||message.contains("no")||message.contains("NO") -> {
                "Question 9:Do you Have Redness or flaky skin...?"
            }

            message.contains("Yes")||message.contains("yes") || message.contains("No")||message.contains("no")||message.contains("NO")-> {
                "Question 10:Do you feel Pain in any area of... ?"
            }

            //When the program doesn't understand...
            else -> {
                "sorry! your answer can be only yes or no"
                }
            }
        }

上面的代码使用“是”或“否”问题,用户回答“是”或“否”,唯一的问题是代码在仅询问第一个天气后不会转到下一个问题,答案是“是”或“否”,那么我该如何制作它转到第二个直到第十个并收集结果?

kotlin
1个回答
1
投票

以下可以是从给定列表中按顺序提出问题的方法,在获得有效答复后继续下一个问题,最后收集和共享结果:

val QUESTIONS = listOf(
    "Question 1:Do you have ... ?",
    "Question 2:Do you have ... ?",
    "Question 3:Do you feel ... ?",
    "Question 4:Do you have ... ?",
    "Question 5:Do you have ... ?",
    "Question 6:Do you have ... ?",
    "Question 7:Do you have ... ?",
    "Question 8:Do you feel ... ?",
    "Question 9:Do you have redness or flaky skin ... ?",
    "Question 10:Do you feel pain in any area of ... ?",
)

var yesCount = 0
var noCount = 0

fun main() {
    QUESTIONS.forEach {
        askQuestionAndParseResponse(it)
    }

    println("Result -> Yes - ${yesCount.toPercentage()}%, No - ${noCount.toPercentage()}%")
}

private fun askQuestionAndParseResponse(question: String) {
    println(question)
    val response = readln()
    when (response.trim().lowercase()) {
        "yes" -> yesCount++
        "no" -> noCount++
        else -> {
            println("Sorry! The answer can only be \"yes\" or \"no\"")
            askQuestionAndParseResponse(question)
        }
    }
}

private fun Int.toPercentage(): String =
    "%.2f".format((this.toDouble() / QUESTIONS.size) * 100)
© www.soinside.com 2019 - 2024. All rights reserved.