C# 有error 帮帮忙
要弄1个console application 只接受名字然后用户输入id 号码 xxxxxxxxxxxx 或 xxxxxx-xx-xxxx (不接受 a-z ,A-Z array 6和 9 可以放 - )
之后计算里面的号码
可是还是有error ,有人可以帮帮忙吗?
ERROR : Operator '>=' cannot be applied to operands of type 'string' and 'int'
我是用 Microsoft Visual Studio .NET 2003 的,有些coding 2005才能用
例如
using System.Collections.Generic;
Console.ReadKey();
Console.Clear();
using System;
namespace IC_Systems_V3
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
string name = "";
while (name.Length == 0)
{
Console.Write("Please enter your full name: ");
name = Console.ReadLine();
}
string idnum = "";
while (idnum.Length == 0)
{
Console.Write("Please enter your ID#: ");
idnum = Console.ReadLine();
}
string[] input = new string[14];
int index=0, maxLength = 12;
do
{
if (idnum >= 0 && idnum <= 9) //it is a digit
{
input[index] = idnum ;
index++;
Console.Write(idnum );
}
else //not a digit
{
if (index == 6 || index == 9)
{
if (idnum == "-")
{
maxLength = 14;
input[index] = idnum ;
index++;
Console.Write(idnum);
}
else
{
Console.Write("user may insert the wrong format,please retry");
}
}
else //invalid input
{
Console.Write("user may insert the wrong format,please retry");
}
}
}while(index < maxLength);
try
{
string b= idnum.Replace("-",string.Empty);
string year = b.Substring(0, 2);
string month = b.Substring(2, 2);
string day = b.Substring(4, 2);
string sum = b.Substring(6, 2);
bool gender = (b[11] % 2 == 1);
string c;
if ("01, 21, 22, 23, 24".IndexOf(sum) > 0)
{
c = "apple";
}
else if ("02, 25, 26, 27 ,33 ".IndexOf(sum) > 0)
{
c = "orange";
}
else
{
c = "unknown";
}
Console.WriteLine("\nName of resident : {0}", name);
Console.WriteLine("Date of birth: {0}/{1}/19{2}", day, month, year);
Console.WriteLine("City of Birth: {0}", c);
Console.WriteLine("Gender: {0}", gender ? "Male" : "Female");
}
catch
{
Console.WriteLine("\nuser may input the incorrect format, please try again.");
}
}
}
}
--------------------编程问答--------------------
你看看红颜色的两行代码。
上面的string类型 怎么能像 整型数据一样用呢?
除非你 Convert.ToInt32一下,
C# code
using System;
namespace IC_Systems_V3
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
string name = "";
while (name.Length == 0)
{
Console.Write("Please enter your full name: ");
name = Console.ReadLine();
}
string idnum = ""; while (idnum.Length == 0)
{
Console.Write("Please enter your ID#: ");
idnum = Console.ReadLine();
}
string[] input = new string[14];
int index=0, maxLength = 12;
do
{
if (idnum >= 0 && idnum <= 9) //it is a digit {
input[index] = idnum ;
index++;
Console.Write(idnum );
}
else //not a digit
{
if (index == 6 || index == 9)
{
if (idnum == "-")
{
maxLength = 14;
input[index] = idnum ;
index++;
Console.Write(idnum);
}
else
{
Console.Write("user may insert the wrong format,please retry");
}
}
else //invalid input
{
Console.Write("user may insert the wrong format,please retry");
}
}
}while(index < maxLength);
try
{
string b= idnum.Replace("-",string.Empty);
string year = b.Substring(0, 2);
string month = b.Substring(2, 2);
string day = b.Substring(4, 2);
string sum = b.Substring(6, 2);
bool gender = (b[11] % 2 == 1);
string c;
if ("01, 21, 22, 23, 24".IndexOf(sum) > 0)
{
c = "apple";
}
else if ("02, 25, 26, 27 ,33 ".IndexOf(sum) > 0)
{
c = "orange";
}
else
{
c = "unknown";
}
Console.WriteLine("\nName of resident : {0}", name);
Console.WriteLine("Date of birth: {0}/{1}/19{2}", day, month, year);
Console.WriteLine("City of Birth: {0}", c);
Console.WriteLine("Gender: {0}", gender ? "Male" : "Female");
}
catch
{
Console.WriteLine("\nuser may input the incorrect format, please try again.");
}
}
}
}
--------------------编程问答-------------------- Operator '>=' cannot be applied to operands of type 'string' and 'int'
将STRING 转化成INT吧,字符和数值型不能比
if (convert.toint32(idnum) >= 0 && convert.toint32(idnum)<= 9) //it is a digit
{
input[index] = idnum ;
index++;
Console.Write(idnum );
}
--------------------编程问答-------------------- --------------------编程问答-------------------- use this:
public static bool TryParse(string s, System.Globalization.NumberStyles style, System.IFormatProvider provider, out int result)--------------------编程问答-------------------- 数据类型不对
Member of System.Int32
if (idnum >= 0 && idnum <= 9) //it is a digit
idnum 是字符串 你怎么和0和9比较起来了?如果是字符比较
可以转换为int比较 或则直接比较char的值
--------------------编程问答-------------------- 之前是用有用 TryParse
可是 2003 版本好像不支持呢 --------------------编程问答--------------------
using System;
namespace IC_Systems_V3
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
string name = "";
while (name.Length == 0)
{
Console.Write("Please enter your full name: ");
name = Console.ReadLine();
}
string icnum = "";
while (icnum.Length == 0)
{
Console.Write("Please enter your IC#: ");
icnum = Console.ReadLine();
}
string[] input = new string[14];
int index=0, maxLength = 12;
do
{
if (Convert.ToInt32(icnum) >= 0 && Convert.ToInt32(icnum) <= 9) //it is a digit
{
input[index] = icnum ;
index++;
Console.Write(icnum );
}
else //not a digit
{
if (index == 6 || index == 9)
{
if (icnum == "-")
{
maxLength = 14;
input[index] = icnum ;
index++;
Console.Write(icnum);
}
else
{
Console.Write("user may insert the wrong format,please retry");
}
}
else //invalid input
{
Console.Write("user may insert the wrong format,please retry");
}
}
}while(index < maxLength);
try
{
string id= icnum.Replace("-",string.Empty);
string year = id.Substring(0, 2);
string month = id.Substring(2, 2);
string day = id.Substring(4, 2);
string states = id.Substring(6, 2);
bool gender = (id[11] % 2 == 1);
string city;
if ("01, 21, 22, 23, 24".IndexOf(states) >=0 )
{
city = "Johor";
}
else if ("02, 25, 26, 27 ,33 ".IndexOf(states) >= 0 )
{
city = "Kedah";
}
else if ("03,28,29 ".IndexOf(states) >= 0 )
{
city = "Kelantan";
}
else if ("04,30 ".IndexOf(states) >= 0 )
{
city = "Malacca";
}
else if ("05,31,59 ".IndexOf(states) >= 0 )
{
city = "Negeri Sembilan";
}
else if ("06,32 ".IndexOf(states) >= 0 )
{
city = "Pahang";
}
else if ("07,34,35 ".IndexOf(states) >= 0 )
{
city = "Penang";
}
else if ("08,36,37,38,39 ".IndexOf(states) >= 0 )
{
city = "Perak";
}
else if ("09,40 ".IndexOf(states) >= 0 )
{
city = "Perlis";
}
else if ("10,41,42,43,44 ".IndexOf(states) >= 0 )
{
city = "Selangor";
}
else if ("11,45,46 ".IndexOf(states) >= 0 )
{
city = "Terengganu";
}
else if ("12,47,48,49 ".IndexOf(states) >= 0 )
{
city = "Sabah";
}
else if ("13,50,51,52,53 ".IndexOf(states) >= 0 )
{
city = "Sarawak";
}
else if ("14,54,55,56,57 ".IndexOf(states) >= 0 )
{
city = "Federal Territory of Kuala Lumpur";
}
else if ("15,58 ".IndexOf(states) >= 0 )
{
city = "Federal Territory of Labuan";
}
else if ("16 ".IndexOf(states) >= 0 )
{
city = "Federal Territory of Putrajaya & Cyberjaya";
}
else if ("60 ".IndexOf(states) >= 0 )
{
city = "Burnei";
}
else if ("61 ".IndexOf(states) >= 0 )
{
city = "Indonesia";
}
else if ("66 ".IndexOf(states) >= 0 )
{
city = "Singapore";
}
else if ("77 ".IndexOf(states) >= 0 )
{
city = "Saudi Arabia";
}
else if ("86 ".IndexOf(states) >= 0 )
{
city = "Austria, Armenia, Belgium, Cyprus, Denmark, France, Finland, Greece, Germany, Holy See (Vatican City), Italy, Luxembourg, Malta, Monaco, The Netherlands, Norway, Portugal, Sweden, Spain, Switzerland, Slovakia or Slovenia";
}
else if ("87 ".IndexOf(states) >= 0 )
{
city = "Britain or Ireland";
}
else if ("90 ".IndexOf(states) >= 0 )
{
city = "The Bahamas, Barbados, Belize, Costa Rica, Cuba, Dominica, El Salvador, Grenada, Guatemala, Haiti, Honduras, Jamaica, Mexico, Nicaragua, Panama, Puerto Rico or Trinidad & Tobago.";
}
else if ("91 ".IndexOf(states) >= 0 )
{
city = "Canada, Greenland, or United States";
}
else
{
city = "unknown (user may input the incorrect ic number)";
}
Console.WriteLine("\nName of resident : {0}", name);
Console.WriteLine("Date of birth: {0}/{1}/19{2}", day, month, year);
Console.WriteLine("City of Birth: {0}", city);
Console.WriteLine("Gender: {0}", gender ? "Male" : "Female");
}
catch
{
Console.WriteLine("\nuser may input the incorrect format, please try again.");
}
}
}
}
program 可以run 了
可是输入完之后 就这样 error 了
怎么办?
补充:.NET技术 , C#