gpt4 book ai didi

c# - 为什么会出现 Invalid Expression Term "String"?

转载 作者:太空宇宙 更新时间:2023-11-03 23:43:45 26 4
gpt4 key购买 nike

我认为错误与 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());

关于c# - 为什么会出现 Invalid Expression Term "String"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28158534/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com