0

所以在我的应用程序中,我试图在我的一种方法中做一个简单的数学运算,而不使用大量的 if/else 语句。

所以我有一个名为“StartInt”的整数,最大值为 13。现在我需要得到的是 FinishInt 一个整数,它将成为这种模式的结果:

StartInt: 13 FinishInt: 1  
StartInt: 12 FinishInt: 2 
StartInt: 11 FinishInt: 3

等等......一直到 StartInt 为 1 并且 FinishInt 为 13。无论如何我将如何完成这个?我知道这一定很简单,但我的数学不是那么好!:)

4

1 回答 1

6

All the way down until StartInt is 0 and FinishInt is 13. Anyway how would I accomplish this?

That won't quite work if startInt = 13 gives finishInt = 1 and you want a finishInt to increment 1 for each decrement of startInt. Check out the following table:

13   1
12   2
11   3
10   4
 9   5
 8   6
 7   7
 6   8
 5   9
 4  10
 3  11
 2  12
 1  13

So you're off by 1 at either the beginning or end of your sequence. Nevertheless, it looks like you want something like this:

(int) calculateFinish(int startInt)
{
    int finishInt = -1;
    if (startInt >= 0 && startInt <= 13) {
        finishInt = 14 - startInt;
    }
    return finishInt;
}

That'd give a value of 14 for finishInt when startInt = 0.

于 2012-09-24T02:13:08.553 回答