字符串转换为int [英] Convert String to int
问题描述
我试图写一个简单的程序,要求用户输入一个数字,然后我会用这个数字来决定门票的成本会是怎样为他们特定的年龄。我试图为int的字符串转换时遇到了麻烦。否则,程序布局是罚款。有什么建议么?
感谢
使用系统;
类ticketPrice
{
公共静态无效的主要(字串[] args)
{
Console.WriteLine(请输入您的年龄);
INT输入=到Console.ReadLine();
如果(输入小于5)
{
Console.WriteLine(你是+输入+和admisson是免费的!);
}
,否则如果(输入> 4&安培;输入< 18)
{
Console.WriteLine(你是+输入+和录取为5 $) ;
}
,否则如果(输入> 17安培;输入< 56)
{
Console.WriteLine(你是+输入+和录取为$ 10) ;
}
,否则如果(输入> 55)
{
Console.WriteLine(你是+输入+和录取为$ 8);
}
}
}
尝试 int.TryParse(...)
方法。它不会抛出异常。
的 http://msdn.microsoft.com/en-us/library/f02979c7.aspx
此外,你应该使用&放大器;&安培;
不是&安培;
在你的条件。 &放大器;&安培;
是逻辑与和&安培;
是位与。
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. I am having trouble when trying to convert the string to int. Otherwise the program layout is fine. Any suggestions? thanks
using System;
class ticketPrice
{
public static void Main(String[] args)
{
Console.WriteLine("Please Enter Your Age");
int input = Console.ReadLine();
if (input < 5)
{
Console.WriteLine("You are "+input+" and the admisson is FREE!");
}
else if (input > 4 & input < 18)
{
Console.WriteLine("You are "+input+" and the admission is $5");
}
else if (input > 17 & input < 56)
{
Console.WriteLine("You are "+input+" and the admission is $10");
}
else if (input > 55)
{
Console.WriteLine("You are "+input+" and the admission is $8");
}
}
}
Try the int.TryParse(...)
method. It doesn't throw an exception.
http://msdn.microsoft.com/en-us/library/f02979c7.aspx
Also, you should use &&
not &
in your conditions. &&
is logical AND and &
is bitwise AND.
这篇关于字符串转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!