Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么是无效的表达术语;“字符串”;出现了吗?_C#_Error Handling - Fatal编程技术网

C# 为什么是无效的表达术语;“字符串”;出现了吗?

C# 为什么是无效的表达术语;“字符串”;出现了吗?,c#,error-handling,C#,Error Handling,我认为错误与If语句有关,但我已经尝试搜索错误,大多数问题都是由语法错误引起的,而我的情况似乎不是这样。提前谢谢你的帮助 using System; namespace FirstConsoleProjectSolution { class MainClass { public static void Main (string[] args) // this is a method called "Main". It is called when the program s

我认为错误与If语句有关,但我已经尝试搜索错误,大多数问题都是由语法错误引起的,而我的情况似乎不是这样。提前谢谢你的帮助

using System;

namespace FirstConsoleProjectSolution
{

  class MainClass
  {

    public static void Main (string[] args) // this is a method called "Main". It is called when the program starts.
    {
        string square;
        string cylinder;

        Console.WriteLine ("Please enter a shape");

        if (string == square) { 

            double length;
            double width;
            double height;

            Console.WriteLine ("Please enter length");
            Console.ReadLine (Convert.ToDouble ());

            Console.WriteLine ("Please enter width");
            Console.ReadLine (Convert.ToDouble ());

            Console.WriteLine ("Please enter height");
            Console.ReadLine (Convert.ToDouble ());

            Console.WriteLine ("Your total volume is" + length * width * height);
        }

        if (string == cylinder) {

            double areaOfBase;
            double height;

            Console.WriteLine ("Please enter area of base");
            Console.ReadLine (Convert.ToDouble ());

            Console.WriteLine ("Please enter height");
            Console.ReadLine (Convert.ToDouble ());

            Console.WriteLine ("Your total volume is" + areaOfBase * height);

        }
    }

  }

}   

这是因为这句话:

if (string == square) {
string
关键字表示数据类型,无法比较数据类型和字符串

您打印出来的消息表明您正在尝试输入某些内容,但没有输入。我认为您正在尝试做一些类似的事情:

Console.WriteLine ("Please enter a shape");
string shape = Console.ReadLine();
if (shape == "square") {
  ...

稍后在代码中,当您尝试输入数字时,您将使用以下代码来解析字符串并将其放入变量中:

length = Convert.ToDouble(Console.ReadLine());

您没有名为
string
的变量。使用
字符串
也是非法的,因为它是该语言中的关键字

如果您满足于使用变量名
字符串
(我可能会避免使用它,因为在这种情况下它不是很具有描述性),您可以通过在变量名的开头添加
@
符号来转义关键字

您的代码有多个问题。首先,你在一开始并没有要求任何意见。如果你的意图是从用户那里获得输入,你应该考虑将控制台的值。你应该考虑一些诸如:

Console.WriteLine("Please enter a shape");
string shapeType = Console.ReadLine();

if (shape == "square")
{
    //do something
}
如果您坚持命名变量
字符串
,您必须说


string@string=Console.ReadLine()

您尚未分配字符串变量

        string square;
        string cylinder;
您尚未捕获用户的输入

解决方案

string square = "square";
string cylinder = "cylinder";
string input;

Console.WriteLine ("Please enter a shape");
input = Console.ReadLine();

if (input == square) { 

    // Do stuff

}
由于将基元类型声明“string”与类型为string的实例进行比较,因此出现错误

if (string == square) {

非常感谢。我没有意识到声明变量并不意味着声明它们的数据类型,而是发明一个“名称”。