c#newbie错误变量,但用作方法
请帮助我,因为我对c#还是新手,不知道如何解决这个问题。我正试图为一个人根据其国籍必须缴纳的税款制定一个代码。但每当我试图运行代码时,就会出现错误“a是一个变量,但用作方法” 另一个问题也发生在我身上,即使我完全删除了一个,那就是即使我清楚地说明了高棉或越南的情况(person=高棉,person=越南),我仍然继续采用泰国的条件 谢谢你抽出时间c#newbie错误变量,但用作方法,c#,C#,请帮助我,因为我对c#还是新手,不知道如何解决这个问题。我正试图为一个人根据其国籍必须缴纳的税款制定一个代码。但每当我试图运行代码时,就会出现错误“a是一个变量,但用作方法” 另一个问题也发生在我身上,即使我完全删除了一个,那就是即使我清楚地说明了高棉或越南的情况(person=高棉,person=越南),我仍然继续采用泰国的条件 谢谢你抽出时间 int a=0; bool person,khmer,viet,thai; khmer=true; t
int a=0;
bool person,khmer,viet,thai;
khmer=true;
thai = true;
viet = true;
person = khmer;
String b;
b= "";
if (person == khmer)
{
a = 0;
b="khmer";
}
if (person == viet)
{
a = 10;
b = "viet";
}
if (person == thai)
{
a = 15;
b = "thai";
}
else
{
a = 20;
b = "alien";
}
Console.WriteLine("he pays " +a ("and he is from ")+b);
Console.In.ReadLine();
换一行,
Console.WriteLine("he pays " +a ("and he is from ")+b);
到
对于第二个问题,这是因为它们具有相同的值。在代码中
bool person,khmer,viet,thai;
khmer=true;
thai = true;
viet = true;
person = khmer;
即使你改变了person=khmer,person=thai或person=越南。人的价值永远是真实的
所以在你的if声明中
if (person == khmer)
{
a = 0;
b="khmer";
}
if (person == viet)
{
a = 10;
b = "viet";
}
if (person == thai)
{
a = 15;
b = "thai";
}
它将始终输入if括号,因为它们的值相同(true)。尝试将thai、Vieta、kmer更改为整数或枚举
int person,khmer,viet,thai;
khmer = 1;
thai = 2;
viet = 3;
person = khmer;
您需要在“a”之后添加a+
在您的最后一篇文章中,您可以这样做:
Console.WriteLine("he pays " +a ("and he is from ")+b);
对于编译器来说,它看起来像是一个方法调用。您正在使用参数“调用”a
,而他来自“
”
您刚刚错过了一个+
。应该是:
Console.WriteLine("he pays " + a + "and he is from " + b);
或者更好,使用WriteLine版本:
Console.WriteLine("he pays {0} and he is from {1}", a, b);
关于你问题的第二部分: 另一个问题也发生在我身上,即使我完全删除了一个,那就是即使我清楚地说明了高棉或越南的情况(person=高棉,person=越南),我仍然继续采用泰国的条件 你的逻辑有缺陷 您已将所有国籍硬编码为
True
。设置person=khmer
实际上只是设置person=True
,这并不能告诉您代码后面的内容
最后运行每个if
语句,因此最终的if(person==thai)
获胜,并且每次都运行a=15
和b=“thai”
您必须更改代码以接受参数
我建议用代表国籍的枚举替换字符串和数字
// You can specify nationalities, and even assign each one the correct numerical value
public enum Nationality
{
khmer = 0,
viet = 10,
thai = 15,
alien = 20,
}
// Then in your method, just cast the selected nationality to an int to get the
// numerical value, and call ToString() to get the name
private void ReportNationality(Nationality nationality)
{
Console.WriteLine("he pays {0} and he is from {1}",
(int)nationality, nationality.ToString());
Console.In.ReadLine();
}
啊,谢谢你的先生,但是关于我的第二个问题,我的代码仍然只接受泰国的条件,你能帮我解决这个问题吗。谢谢,串接字符串看起来更干净。@RonaldEstacion Grant的意思是你可以使用-它为你进行格式化,你不必调用字符串。你自己格式化。@Blorgbeard Yea,在你第一次评论时就看过了。感谢您提供的信息,最好使用Select Case语句,而不是If命令。这比我还快抱歉,因为我还是c#的新手,有没有办法解决这个问题或解决它?啊,谢谢。很抱歉犯了这样一个新手错误,耽误了你的时间。
Console.WriteLine("he pays {0} and he is from {1}", a, b);
// You can specify nationalities, and even assign each one the correct numerical value
public enum Nationality
{
khmer = 0,
viet = 10,
thai = 15,
alien = 20,
}
// Then in your method, just cast the selected nationality to an int to get the
// numerical value, and call ToString() to get the name
private void ReportNationality(Nationality nationality)
{
Console.WriteLine("he pays {0} and he is from {1}",
(int)nationality, nationality.ToString());
Console.In.ReadLine();
}