在c#中将十进制数字循环1

Mat*_*hew 0 c# math

我想使用一次可能的操作将十进制数字循环1.

if x is 0 - 8 then x = x + 1
if x is 9 then x = 0
Run Code Online (Sandbox Code Playgroud)

或相反的

if x is 1 - 9 then x = x - 1
if x is 0 then x = 9
Run Code Online (Sandbox Code Playgroud)

有没有办法在一行代码中用C#做到这一点?如果不是C#还有其他任何语言吗?

如果我想通过多个(2或3或其他)循环它会怎么样?

dav*_*v_i 7

我想你要找的是

var y = (x + 1) % 10;
Run Code Online (Sandbox Code Playgroud)

这给出了:

x    y
------
0    1
1    2
...
8    9
9    0
Run Code Online (Sandbox Code Playgroud)

递减

var y = (i + 9) % 10;
Run Code Online (Sandbox Code Playgroud)

显然要改变金额,只需改变19分别改变

  • 使用(x + 9)%10作为-1方向. (2认同)