C#初学者问个24小时转12小时的问题

xjx2602 2012-05-07 06:15:10
这是我写的代码:
using System;
class Lian006
{
static void Main()
{
string time;//怎么把输入的转化成数值?
int x,y;
Console.WriteLine("请输入现在的时间:");
time=Console.ReadLine();
int x=(int)time;
y=x%12;
Console.WriteLine("你输入的时间是{0}点整。",y);
}
}
提示是无法把string转为int类型,这个怎么解决啊?请各位朋友指点下
...全文
163 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xjx2602 2012-05-21
  • 打赏
  • 举报
回复
感谢各位高手的帮忙,我试了一下:
用x=Conver.ToInt32(time)
和x=int.Parse(time)
两种方法都可以,多谢大家。
wwqcsc 2012-05-08
  • 打赏
  • 举报
回复
这样做就行了!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string time;//怎么把输入的转化成数值?
int x, y;
Console.WriteLine("请输入现在的时间:");
time = Console.ReadLine();
x =Convert.ToInt32(time);
y = x % 12;
Console.WriteLine("你输入的时间是{0}点整。", y);
Console.ReadKey();
}
}
}
wangsong145 2012-05-07
  • 打赏
  • 举报
回复
Convert.ToInt32
orochiheart 2012-05-07
  • 打赏
  • 举报
回复
[Quote=引用楼主 的回复:]
这是我写的代码:
using System;
class Lian006
{
static void Main()
{
string time;//怎么把输入的转化成数值?
int x,y;
Console.WriteLine("请输入现在的时间:");
time=Console.ReadLine();
int x=(int)time;
y=x%12;
Console.Wr……
[/Quote]
string类型不能隐式转换成int类型(也就是你转换的那种方法,能隐式转换的都是特点差不多的 比如int转long之类的),所以需要显式转换->
Convert.ToInt32(time);
dalmeeme 2012-05-07
  • 打赏
  • 举报
回复
int x=int.Parse(time);
EdsionWang 2012-05-07
  • 打赏
  • 举报
回复
int x=(int)time;改成:
x=Convert.ToInt32(time);

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧