อะไรคือรูปแบบการสั่งงานเมื่อใช้จำนวน comparations ในตอนแถลงการณ์

0

คำถาม

ฉันมีรหัส:

      statisticsSettings = when (ScreenHandler.convertPixelsToDp(width, context).toInt()){
            320 -> StatisticsSettings.SMALL_PHONE
            480 -> StatisticsSettings.LARGE_PHONE
            600 -> StatisticsSettings.SMALL_TABLET
            720 -> StatisticsSettings.LARGE_TABLET
            else -> throw IllegalArgumentException("Cannot compute dp")
        }

และฉันสงสัยว่าถ้าฉันสามารถทำให้คดีของ when แถลงการณ์กับ comparator แทนที่จะเป็นเป็นเลขจำนวนเต็ม บางอย่างเหมือนนี้:

      statisticsSettings = when (ScreenHandler.convertPixelsToDp(width, context).toInt()){
            ScreenHandler.convertPixelsToDp(width, context).toInt()) < 320 -> StatisticsSettings.SMALL_PHONE
            ScreenHandler.convertPixelsToDp(width, context).toInt()) < 480 -> StatisticsSettings.LARGE_PHONE
            ScreenHandler.convertPixelsToDp(width, context).toInt()) < 600 -> StatisticsSettings.SMALL_TABLET
            ScreenHandler.convertPixelsToDp(width, context).toInt()) < 720 -> StatisticsSettings.LARGE_TABLET
            else -> throw IllegalArgumentException("Cannot compute dp")
        }
kotlin
2021-11-23 23:05:51
1

คำตอบที่ดีที่สุด

4

ใช้ช่วง:

val statisticsSettings = when (ScreenHandler.convertPixelsToDp(width, context).toInt()){
  in 0..320 -> StatisticsSettings.SMALL_PHONE
  in 321..480 -> StatisticsSettings.LARGE_PHONE
  in 481..600 -> StatisticsSettings.SMALL_TABLET
  in 601..720 -> StatisticsSettings.LARGE_TABLET
  else -> throw IllegalArgumentException("Cannot compute dp")
}

หรือคุณสามารถใช้ enum ค่าคงที่:

enum class StatisticsSettings(val intRange: IntRange) {
  SMALL_PHONE(0..320),
  LARGE_PHONE(321..480),
  SMALL_TABLET(481..600),
  LARGE_TABLET(601..720)
}

val intRange = ScreenHandler.convertPixelsToDp(width, context).toInt()

val statisticsSettings = StatisticsSettings.values().find { intRange in it.intRange }

นี่มันได้เปรียบว่าช่วงคือ"มุ่ง"เพื่อนที่ enum ตัวมันเอง ถ้าคุณเคยเปลี่ยนค่าของพวกนี้,เธอไม่ต้องเปลี่ยนพวกเขาอาจจะหลายตำแหน่งของในรหัสของคุณ.

แก้ไข:เปลี่ยนจาก ตัวกรอง เพื่อ ค้นหา (ต้องขอบคุณ@ArpitShukla เห็นความคิดเห็นด้านล่างนี้)

2021-11-24 08:43:33

คุณสามารถแทนที่ filter กับ find. นั่นฟังดูมีเหตุผลกว่าเหรอยู่ที่นี่
Arpit Shukla

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่