VB.net 控制台应用程序打印数字的反转

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32158560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:24:55  来源:igfitidea点击:

VB.net console application to print Reverse of number

vb.net

提问by Priyanka Rajendra Sonawane

I am trying to print reverse of number using VB.NET console application and I have variable of type Integer. when I give number 651 as input it prints 1561. I have write code as

我正在尝试使用 VB.NET 控制台应用程序打印数字的反转,并且我有整数类型的变量。当我将数字 651 作为输入时,它会打印 1561。我已将代码编写为

Sub Main()
    Dim no, rev, temp As Integer
    Console.WriteLine("enter the no")
    no = CInt(Console.ReadLine())
    rev = 0
    temp = no
    While temp > 0
        Dim t As Integer
        t = temp Mod 10
        rev = rev * 10 + t
        temp = temp / 10
    End While
    Console.WriteLine("Reverse number=>"+rev.ToString())
    Console.ReadKey()
End Sub

when I enter number 123 then it gives proper output as 321, but when i give 678,it give output 8761 or other garbage value, please suggest me suggetion

当我输入数字 123 时,它给出正确的输出为 321,但是当我输入 678 时,它给出输出 8761 或其他垃圾值,请给我建议

回答by sujith karivelil

You are getting such result because,

你得到这样的结果是因为,

when you assign a division result to an integer value it will automatically get rounded to the next higher integer.

当您将除法结果分配给整数值时,它将自动四舍五入到下一个更高的整数。

For example Dim no As Integer = 68 / 10will give you result as 7, if you are coding with Option Strict Onthen this casting is not allowed. The best way is suggested by Bj?rn-Roger Kringsj?you can simply reverse a number and print it.

例如Dim no As Integer = 68 / 10会给你结果为7,如果你正在编码,Option Strict On那么这个转换是不允许的。Bj?rn-Roger Kringsj建议的最佳方法是什么?你可以简单地反转一个数字并打印出来。

Console.Write("Reverse of {0} is : ", no.ToString().Reverse())

or else you can follow the following steps:

或者您可以按照以下步骤操作:

    Console.WriteLine("enter the no")
    Dim no As Integer = CInt(Console.ReadLine())
    Console.Write("Reverse of {0} is : ", no)
    While no > 0
        Console.Write(no Mod 10)
        no = Math.Floor(no / 10)
    End While
    Console.ReadKey()

回答by MANAV BANSAL

Just use \instead of /and your work is done.

只需使用\而不是,/您的工作就完成了。