Convert String to int in C#

前端 未结 5 1891
星月不相逢
星月不相逢 2020-12-11 08:10

I am trying to write a simple program that asks the user to enter a number and then I will use that number to decide what the cost of the ticket will be for their given age.

5条回答
  •  没有蜡笔的小新
    2020-12-11 08:22

    • For easy parsing of strings to intgers (and other number types) use that number type's .TryParse(inputstring, yourintegervariable) method. This method will output a boolean (True/False) letting you know whether the operation passed or failed. If the result is false, you can give an error message before going any further (don't have to worry about crashing your program).

    • Previous text concerning switch statements has been removed

    • In C#, you need to use the && operator for logical AND. & is not the same and may not work the way you believe it will.

提交回复
热议问题