📌  相关文章
📜  如何在 Android Studio 中构建罗马数字转换器?

📅  最后修改于: 2022-05-13 01:54:25.938000             🧑  作者: Mango

如何在 Android Studio 中构建罗马数字转换器?

罗马数字转换器是一个应用程序,通过它我们可以将十进制数字转换为其相应的罗马数字或将罗马数字转换为其相应的十进制数字,范围为 1 到 3999。用户将输入一个十进制数字,然后单击转换为罗马数字数字按钮,输入的数字会转换成对应的罗马数字,如果用户输入罗马数字,点击转换成十进制,输入的数字会转换成对应的十进制数字。在本文中,我们将使用 Kotlin和 XML 在 Android Studio 中进行罗马数字转换器。

先决条件:

  • 将 1 到 3999 之间的十进制数转换为罗马数字
  • 将罗马数字转换为 1 到 3999 之间的十进制

分步实施

第 1 步:创建一个新项目

要在 Android Studio 中创建新项目,请参阅如何在 Android Studio 中创建/启动新项目。请注意,选择Kotlin作为编程语言。

第 2 步:使用 build.gradle(Module) 文件



您需要像这样在 app build.gradle 模块中应用插件 kotlin-android-extensions

plugins {

   id 'com.android.application'

   id 'kotlin-android'

   id 'kotlin-android-extensions'

}

步骤 3:使用 activity_main.xml 文件

导航到app > res > layout > activity_main.xml并将以下代码添加到该文件中。下面是activity_main.xml文件的代码。

XML


  
    
  
    
  
    
  
    


Kotlin
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // Decimal to Roman convertor
        convert_to_roman_numeral.setOnClickListener {
  
            // check if decimal_et.text is empty or not.
            if (decimal_et.text.isNotEmpty()) {
  
                val number = decimal_et.text.toString().toInt()
  
                if (number <= 3999) {
                    // int_to_Roman function convert a decimal
                    // number to its corresponding Roman number.
                    val roman = decimal_to_Roman(number)
  
                    roman_numeral_tv.text = roman.toString()
  
                } else {
                    Toast.makeText(this, "please enter a number in range between 1 to 3999",Toast.LENGTH_SHORT).show()
                }
            } else {
                Toast.makeText(this, "please enter a number ", Toast.LENGTH_SHORT).show()
            }
  
        }
  
  
        // Roman to Decimal convertor.
        convert_to_decimal.setOnClickListener {
            // check if roman_et.text is empty or not.
            if (roman_et.text.isNotEmpty()) {
                val roman = roman_et.text.toString()
                // RomanToDecimal function convert a Roman number
                // to its corresponding decimal number.
                decimal_tv.text = RomanToDecimal(roman).toString()
            } else {
                Toast.makeText(this, "please enter a Roman Numeral ", Toast.LENGTH_SHORT).show()
            }
        }
    }
  
    private fun decimal_to_Roman(num: Int): Any {
  
        val m = arrayOf("", "M", "MM", "MMM")
        val c = arrayOf(
            "", "C", "CC", "CCC", "CD", "D",
            "DC", "DCC", "DCCC", "CM"
        )
        val x = arrayOf(
            "", "X", "XX", "XXX", "XL", "L",
            "LX", "LXX", "LXXX", "XC"
        )
        val i = arrayOf(
            "", "I", "II", "III", "IV", "V",
            "VI", "VII", "VIII", "IX"
        )
  
        // Converting to roman
        val thousands = m[num / 1000]
        val hundereds = c[num % 1000 / 100]
        val tens = x[num % 100 / 10]
        val ones = i[num % 10]
  
        return thousands + hundereds + tens + ones
    }
  
    fun value(r: Char): Int {
        if (r == 'I') return 1
        if (r == 'V') return 5
        if (r == 'X') return 10
        if (r == 'L') return 50
        if (r == 'C') return 100
        if (r == 'D') return 500
        return if (r == 'M') 1000 else -1
    }
  
    // Finds decimal value of a
    // given romal numeral
    fun RomanToDecimal(str: String): Int {
        // Initialize result
        var res = 0
        var i = 0
        while (i < str.length) {
  
            // Getting value of symbol s[i]
            val s1 = value(str[i])
  
            // Getting value of symbol s[i+1]
            if (i + 1 < str.length) {
                val s2 = value(str[i + 1])
  
                // Comparing both values
                if (s1 >= s2) {
                    // Value of current symbol
                    // is greater or equalto
                    // the next symbol
                    res = res + s1
                } else {
                    // Value of current symbol is
                    // less than the next symbol
                    res = res + s2 - s1
                    i++
                }
            } else {
                res = res + s1
            }
            i++
        }
        return res
    }
}


写了这么多代码后,我们的 UI 看起来是这样的:

步骤 4:使用 MainActivity.kt 文件

转到MainActivity.kt文件并参考以下代码。下面是MainActivity.kt文件的代码。代码中添加了注释以更详细地理解代码。

科特林

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // Decimal to Roman convertor
        convert_to_roman_numeral.setOnClickListener {
  
            // check if decimal_et.text is empty or not.
            if (decimal_et.text.isNotEmpty()) {
  
                val number = decimal_et.text.toString().toInt()
  
                if (number <= 3999) {
                    // int_to_Roman function convert a decimal
                    // number to its corresponding Roman number.
                    val roman = decimal_to_Roman(number)
  
                    roman_numeral_tv.text = roman.toString()
  
                } else {
                    Toast.makeText(this, "please enter a number in range between 1 to 3999",Toast.LENGTH_SHORT).show()
                }
            } else {
                Toast.makeText(this, "please enter a number ", Toast.LENGTH_SHORT).show()
            }
  
        }
  
  
        // Roman to Decimal convertor.
        convert_to_decimal.setOnClickListener {
            // check if roman_et.text is empty or not.
            if (roman_et.text.isNotEmpty()) {
                val roman = roman_et.text.toString()
                // RomanToDecimal function convert a Roman number
                // to its corresponding decimal number.
                decimal_tv.text = RomanToDecimal(roman).toString()
            } else {
                Toast.makeText(this, "please enter a Roman Numeral ", Toast.LENGTH_SHORT).show()
            }
        }
    }
  
    private fun decimal_to_Roman(num: Int): Any {
  
        val m = arrayOf("", "M", "MM", "MMM")
        val c = arrayOf(
            "", "C", "CC", "CCC", "CD", "D",
            "DC", "DCC", "DCCC", "CM"
        )
        val x = arrayOf(
            "", "X", "XX", "XXX", "XL", "L",
            "LX", "LXX", "LXXX", "XC"
        )
        val i = arrayOf(
            "", "I", "II", "III", "IV", "V",
            "VI", "VII", "VIII", "IX"
        )
  
        // Converting to roman
        val thousands = m[num / 1000]
        val hundereds = c[num % 1000 / 100]
        val tens = x[num % 100 / 10]
        val ones = i[num % 10]
  
        return thousands + hundereds + tens + ones
    }
  
    fun value(r: Char): Int {
        if (r == 'I') return 1
        if (r == 'V') return 5
        if (r == 'X') return 10
        if (r == 'L') return 50
        if (r == 'C') return 100
        if (r == 'D') return 500
        return if (r == 'M') 1000 else -1
    }
  
    // Finds decimal value of a
    // given romal numeral
    fun RomanToDecimal(str: String): Int {
        // Initialize result
        var res = 0
        var i = 0
        while (i < str.length) {
  
            // Getting value of symbol s[i]
            val s1 = value(str[i])
  
            // Getting value of symbol s[i+1]
            if (i + 1 < str.length) {
                val s2 = value(str[i + 1])
  
                // Comparing both values
                if (s1 >= s2) {
                    // Value of current symbol
                    // is greater or equalto
                    // the next symbol
                    res = res + s1
                } else {
                    // Value of current symbol is
                    // less than the next symbol
                    res = res + s2 - s1
                    i++
                }
            } else {
                res = res + s1
            }
            i++
        }
        return res
    }
}

输出:

想要一个更快节奏和更具竞争力的环境来学习 Android 的基础知识吗?
单击此处前往由我们的专家精心策划的指南,旨在让您立即做好行业准备!