我怎么会在惯用的Kotlin中写这个? - 2

时间:2018-04-14 23:39:44

标签: kotlin idiomatic

我问了一个问题: How would I write this in idiomatic Kotlin?

现在我简短地想到了这个成语。如下所示

containsKey

getIntargumentswhen的方法。

当然这对public static int supprimerTest(Test[] tests, int nbrTests) { int longueurTests = tests.length; int indice = 0; int noTest = 1; int saisieNoTest = 0; String nomTest; System.out.println("***DELETE A TEST***\n"); if (nbrTests > 0) { boolean fin = true; do{ System.out.print("Please enter a number of the question to be deleted"); try { indice = Clavier.lireInt(); if (indice < 1 || indice > nbrTests){ throw new IndexOutOfBoundsException(); System.out.println("The test \"" + tests[indice - 1].getNomTest()); tests[indice-1] =null; nbrTests--; fin = false; } }catch (Exception e) { if (nbrTests < 1){ System.out.print("ERROR ! the number must be between 1 and " + nbrTests + "try again..."); }else { System.out.println("ERROR ! the number must 1. ... Try again..."); } } }while (fin); }else { System.out.println("Il n'existe aucun test."); System.out.print ("\nTPress <ENTRER> to continue ..."); Clavier.lireFinLigne(); } return nbrTests; } 来说不是正确的习语 有没有办法做到这一点?

参数是Android框架中的Bundle类。 你可以在下面看到 https://developer.android.com/reference/android/os/Bundle.html https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/os/Bundle.java https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/os/BaseBundle.java

2 个答案:

答案 0 :(得分:2)

根据您提供的信息,我只能给您这个答案:

private fun getTouchX(): Int = arguments.run {
    if (containsKey(KEY_DOWN_X)) getInt(KEY_DOWN_X)
    else centerX()
}

如果arguments可以为空,则可以是这样的:

private fun getTouchX(): Int = arguments?.run {
    if (containsKey(KEY_DOWN_X)) getInt(KEY_DOWN_X)
    else null
} ?: centerX()

答案 1 :(得分:0)

感谢@ ice1000的回答。 我也得到了下面的成语

private fun getTouchX(): Int = arguments?.run {
    when {
        containsKey(KEY_DOWN_X) -> getInt(KEY_DOWN_X)
        else -> null
    }
} ?: centerX()

当超过3个谓词条件(如果是x 3)

时,我可以使用它
相关问题