简体   繁体   English

反转数字

[英]Reversing Digits

I'm trying to make a function that takes a three digit number and reverses it (543 into 345)我正在尝试制作一个 function ,它需要一个三位数字并将其反转(543 到 345)
I can't take that value from a TextBox because I need it to use the three numbers trick to find a value.我无法从 TextBox 中获取该值,因为我需要它使用三个数字技巧来查找值。

RVal = ReverseDigits(Val)
Diff = Val - RVal
RDiff = ReverseDigits(Diff)
OVal = Diff + RDiff

543-345=198 543-345=198
198+891=1089 198+891=1089

Then it puts 1089 in a TextBox然后将 1089 放入 TextBox

Function ReverseDigits(ByVal Value As Integer) As Integer
    ' Take input as abc
    ' Output is (c * 100 + b * 10 + a) = cba
    Dim ReturnValue As Boolean = True
    Dim Val As String = CStr(InputTextBox.Text)
    Dim a As Char = Val(0)
    Dim b As Char = Val(1)
    Dim c As Char = Val(2)

    Value = (c * 100) + (b * 10) + (a)

    Return ReturnValue
End Function

I've tried this but can't figure out why it won't work.我已经尝试过了,但无法弄清楚为什么它不起作用。

You can convert the integer to a string, reverse the string, then convert back to an integer.您可以将 integer 转换为字符串,反转字符串,然后转换回 integer。 You may want to enforce the three digit requirement.您可能希望强制执行三位数要求。 You can validate the argument before attempting conversion您可以在尝试转换之前验证参数

Public Function ReverseDigits(value As Integer) As Integer
    If Not (value > 99 AndAlso value < 1000) Then Throw New ArgumentException("value")
    Return Integer.Parse(New String(value.ToString().Reverse().ToArray()))
End Function

My code is pretty simple and will also work for numbers that don't have three digits assuming you remove that validation.我的代码非常简单,假设您删除了该验证,也适用于没有三位数的数字。 To see what's wrong with your code, there are a couple of things.要查看您的代码有什么问题,有几件事。 See the commented lines which I changed.请参阅我更改的注释行。 The main issue is using Val as a variable name, then trying to index the string like Val(0) .主要问题是使用Val作为变量名,然后尝试像Val(0)那样索引字符串。 Val is a built in function to vb.net and the compiler may interpret Val(0) as a function instead of indexing a string. Val是内置的 function 到 vb.net 并且编译器可以将Val(0)解释为 function 而不是索引字符串。

Function ReverseDigits(ByVal Value As Integer) As Integer
    ' Dim ReturnValue As Boolean = True
    ' Dim Val As String = CStr(InputTextBox.Text)
    Dim s As String = CStr(Value)
    Dim a As Char = s(0)
    Dim b As Char = s(1)
    Dim c As Char = s(2)

    Value = Val(c) * 100 + Val(b) * 10 + Val(a)

    'Return ReturnValue
    Return Value
End Function

(Or the reduced version of your function, but I would still not hard-code the indices because it's limiting your function from expanding to more or less than 3 digits) (或者您的 function 的简化版本,但我仍然不会对索引进行硬编码,因为它限制了您的 function 扩展至多于或少于 3 位数字)

Public Function ReverseDigits(Value As Integer) As Integer
    Dim s = CStr(Value)
    Return 100 * Val(s(2)) + 10 * Val(s(1)) + Val(s(0))
End Function

And you could call the function like this你可以像这样调用 function

Dim inputString = InputTextBox.Text
Dim inputNumber = Integer.Parse(inputString)
Dim reversedNumber = ReverseDigits(inputNumber)

Bonus: If you really want to use use math to find the reversed number, here is a version which works for any number of digits奖励:如果您真的想使用数学来找到反转的数字,这里有一个适用于任意位数的版本

Public Function ReverseDigits(value As Integer) As Integer
    Dim s = CStr(value)
    Dim result As Integer
    For i = 0 To Len(s) - 1
        result += CInt(Val(s(i)) * (10 ^ i))
    Next
    Return result
End Function

Or in VBA或在 VBA

  Turn into text, use strreverse and then return it to number


  Public Function ReverseDigits(l As Long) As Long
     ReverseDigits = CLng(StrReverse(CStr(l)))
  End Function

Here's a method I wrote recently when someone else posted basically the same question elsewhere, probably doing the same homework:这是我最近在其他人在其他地方发布基本相同的问题时写的一种方法,可能在做同样的功课:

Private Function ReverseNumber(input As Integer) As Integer
    Dim output = 0

    Do Until input = 0
        output = output * 10 + input Mod 10
        input = input \ 10
    Loop

    Return output
End Function

That will work on a number of any length.这将适用于任何长度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM