【问题标题】:Basic Kotlin , Basic Kotlin Android Development基础 Kotlin , 基础 Kotlin Android 开发
【发布时间】:2022-09-29 16:27:19
【问题描述】:
>     1   package com.linecorp.exam
>     2  
>     3   import android.os.Bundle
>     4   import android.widget.TextView
>     5   import android.app.Activity 
>     6   import android.graphics.Color
>     7   import android.view.Gravity
>     8   import android.view.View
>     9   import android.view.ViewGroup
>     10  import android.widget.BaseAdapter
>     11  import android.widget.ListView
>     12   
>     13  class MainActivity : Activity() {
>     14   
>     15      enum class taskstate {
>     16          todo,
>     17          done
>     18      }
>     19   
>     20      var tasklist = mutableListOf<Pair<String, taskstate>>()
>     21   
>     22      private lateinit var myadapter: Myadapter
>     23   
>     24      override fun onCreate(savedInstanceState: Bundle?) {
>     25          super.onCreate(savedInstanceState)
>     26          setContentView(R.layout.activity_main)
>     27   
>     28          myadapter = Myadapter()
>     29          val listView = findViewById<ListView>(R.id.list_view)
>     30          listView.adapter = myadapter
>     31   
>     32          tasklist.clear()
>     33          var i = 0
>     34          todoRepository.instance.fetch_all().forEach { t ->
>     35              tasklist.add(i++, t)
>     36              myadapter.notifyDataSetChanged()
>     37          }
>     38      }
>     39   
>     40      override fun onDestroy() {
>     41          tasklist.clear()
>     42      }
>     43   
>     44      inner class Myadapter : BaseAdapter() {
>     45   
>     46          private lateinit var convertView: View
>     47   
>     48          override fun getCount(): Int {
>     49              return tasklist.size
>     50          }
>     51   
>     52          override fun getItem(position: Int): Any {
>     53              val li = tasklist.filter { it.second == taskstate.todo } +
>     54                          tasklist.filter { it.second == taskstate.done }
>     55              return li[position]
>     56          }
>     57   
>     58          override fun getItemId(position: Int): Long {
>     59              return 0
>     60          }
>     61   
>     62          override fun getView(position: Int, convertView: View?, container: ViewGroup?): View {
>     63              this.convertView = if (convertView == null) {
>     64                  layoutInflater.inflate(R.layout.list_item, container, false)
>     65              } else {
>     66                  convertView
>     67              }
>     68   
>     69              val i = getItem(position) as Pair<String, taskstate>
>     70              this.convertView.findViewById<TextView>(R.id.item_label)
>     71                  .apply {
>     72                      when (i.second) {
>     73                          taskstate.todo -> {
>     74                              setText(\"TODO\")
>     75                              setBackgroundColor(Color.YELLOW)
>     76                          }
>     77                          else -> {
>     78                              setText(\"DONE\")
>     79                          }
>     80                      }
>     81                  }
>     82              this.convertView.findViewById<TextView>(R.id.item_text)
>     83                  .setText(i.first)
>     84   
>     85              return convertView!!
>     86          }
>     87      }
>     88  }`enter code here`

这是我参加考试时遇到的一个问题。不幸的是我失败了 :) 问题是如何改进这个 Kotlin Android 代码并在需要时添加必要的 cmets。 (请不要考虑这条线 ipsum dfgsdndd gfnjfn vjfnvkf fjnvkfv vnkdkvd dndk.sds dshdsd shdahd sdiauhd basudsua saudhaus sahdsuahd ashdoahsd shdoahd ashdosahd asdhoaishd )

    标签: kotlin


    【解决方案1】:

    这里的问题是如果s1&lt;s2 则返回total,而事实并非如此。即使你发现了一个这样的情况,那不是字符串的结尾,你应该继续处理。您应该将i 增加到i+2 而不是返回,因为您已经处理了i+1。由于您不能在 for 循环之间进行这种增量,因此您将不得不使用 while 循环。

    fun romanToInt(s: String): Int {
        val map = mapOf('I' to 1, 'V' to 5, 'X' to 10, 'L' to 50, 'C' to 100, 'D' to 500, 'M' to 1000)
        var total = 0
        var i = 0
    
        while (i < s.length) {
            val current = map[s[i]]!!
            val next = if (i != s.lastIndex) map[s[i + 1]]!! else 0
            if (current >= next) {
                total += current
                i++
            } else {
                total += next - current
                i += 2
            }
        }
        return total
    }
    

    【讨论】: