字符串和整型转化问题

right89757 2012-10-29 07:48:14
写了一个类用于验证两个字符串是否一样。提示错误,不能从int转换成string

class Validation
{
public string Mr;

public static int Comp_result(string s1,string s2)
{
int result = string.Compare(s1, s2);
return result;
}
}


Mr = Convert.ToInt32(Console.ReadLine());
string Ord1 ="-1";
string Ord2 = "1";
int res1 = Validation.Comp_result(Mr, Ord1);
int res2 = Validation.Comp_result(Mr, Ord2);
...全文
212 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
xieniuniu219 2012-10-30
  • 打赏
  • 举报
回复
Console.ReadLine() 本来就是字符串了,你再用 Mr = Convert.ToInt32(Console.ReadLine());
把它转化成整型,而传进去的需要一个字条串,你还得再转换成字符串,这不是多余了吗?
直接string Mr = Console.ReadLine();
XBodhi. 2012-10-30
  • 打赏
  • 举报
回复


string i ="-1";

int a = Covnert.ToInt32(i);
int b = int.Parse(i);

string c = a.ToString();
string d = Convert.ToString(b) ;

装箱,拆箱,
显示转换,隐式转换。




多去看看 基础, 要不你就别写程序了 ,连这个都解决不聊, 那你其他的 根本就没有办法在写下去了
chlo080316 2012-10-29
  • 打赏
  • 举报
回复
“int result = string.Compare(s1, s2);”这句的结果怎么是int啊?不是应该是bool么
Mr = Convert.ToInt32(Console.ReadLine()); 前面是string型的Mr 怎么又去把后面的转换成int型的呢 ……
heiyedebing 2012-10-29
  • 打赏
  • 举报
回复
Convert.ToInt32(Console.ReadLine())应该是个int类型
Convert.ToInt32(Console.ReadLine()).ToString();才是string类型
SQL77 2012-10-29
  • 打赏
  • 举报
回复
int res1 = Validation.Comp_result(Mr.tostring(), Ord1);
gyk_net 2012-10-29
  • 打赏
  • 举报
回复
Mr=Console.Readline();
gyk_net 2012-10-29
  • 打赏
  • 举报
回复
Mr=Console.Readline();
wuyq11 2012-10-29
  • 打赏
  • 举报
回复
Comp_result(Mr.ToString(), Ord1);
devmiao 2012-10-29
  • 打赏
  • 举报
回复
事实上根本不用转int,你画蛇添足了。
devmiao 2012-10-29
  • 打赏
  • 举报
回复
int res1 = Validation.Comp_result(Mr.ToString(), Ord1);
sj490790083 2012-10-29
  • 打赏
  • 举报
回复
Mr = Convert.ToInt32(Console.ReadLine()).ToString();

111,093

社区成员

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

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

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