【问题标题】:C# - Expecting "{" in if statement but got "("C# - 在 if 语句中期待 "{" 但得到 "("
【发布时间】:2022-01-04 17:04:37
【问题描述】:

我正在为学校编写一个程序,一旦某个设备检测到湿度水平高于某个值,就会向用户发送电子邮件。我不知道 C/C#/C++(我必须用 C# 编写它,因为主程序是用 C# 编写的),但是我知道 python,所以结合我的 Python 知识使用主程序,我能够制作一个 if 语句.问题是,运行时它给出了预期的错误“{”但得到了“(”。因为这在 Python 中不是问题,所以我真的不知道我做错了什么。有什么帮助吗? 注意:为了我的安全,电子邮件、密码和姓名已被删除。

/// Adding time (In progress. Lines 2, 3, 12, 26)

// Send email if plant is too moist
if (MoistureValue > 2) {
{
    var smtpClient = new SmtpClient("smtp.gmail.com")
{
    Port = 587,
    Credentials = new NetworkCredential("testemail@email.com", "RandomPasswordRandomNumber"),
    EnableSsl = true,
   
};

smtpClient.Send("exampleemail@email.com", "My name", "Plant Moisture", "Your plant is above its reccomended moisture go check on it!.");
}
}
// Send email if plant is too dry
if (MoistureValue < 2) { 
{
    var smtpClient = new SmtpClient("smtp.gmail.com")
{
    Port = 587,
    Credentials = new NetworkCredential("testemail@email.com", "RandomPasswordRandomNumber"),
    EnableSsl = true,
    
};
    
smtpClient.Send("exampleemail@email.com", "My Name", "Plant Moisture", "Your plant is a little dry, go water it!");
}
}

【问题讨论】:

  • 你知道每个 if 的 一个 组括号就足够了;并且正确的缩进使您的代码更具可读性并且更容易发现语法错误?不过,错误似乎不在这段代码中。
  • 谢谢,那是我的事。绝对可以有更好的缩进,但是关于括号的部分我不知道。
  • 在 Python 中,您使用缩进来指示代码块。在 C 系列语言中,您使用 {curly-bracket-pairs}。一段代码(比如与if 语句关联的代码)用开闭花括号划分。例如:if (boolean-condition) { Func1(); Func2(); } 有效。
  • 好的,有道理。问题是当我这样做时,这行代码 smtpClient.Send("exampleemail@email.com", "My name", "Plant Moisture", "Your plant is above its reccomended moisture go check on it!."); 在 VSC 中变为蓝色,表示出现问题。
  • 在 VSC 中变蓝,表示有问题 - 从未将 VSC 用于 C#,但我认为这不正确/不准确。我很确定“错误”的东西在 VSC 中会得到一个红色的摆动下划线。他们在 JS 中做,这个 C#screenshot I found 似乎支持这一点 - 发布你的 VSC 的屏幕截图

标签: c# if-statement compiler-errors syntax-error


【解决方案1】:

您的牙套不平衡。你应该首先得到什么都不会发生的情况。所以也许你会去。

// When everything is okay, do nothing.
if (MoistureValue == 2) return;

var smtpClient = new SmtpClient("smtp.gmail.com")
    {
        Port = 587,
        Credentials = new NetworkCredential("testemail@email.com", "RandomPasswordRandomNumber"),
        EnableSsl = true,
    };

var message = MoistureValue < 2 ? "Your plant is a little dry, go water it!" : "Your plant is above its reccomended moisture go check on it!.";

smtpClient.Send("exampleemail@email.com", "My name", "Plant Moisture", message);

【讨论】:

  • 有一些多余的,但不,它们不平衡。
【解决方案2】:

在编程中,我们努力减少重复。您的代码可以简化为:

if (MoistureValue != 2) {

    var smtpClient = new SmtpClient("smtp.gmail.com", 587) {
        Credentials = new NetworkCredential("testemail@email.com", "RandomPasswordRandomNumber"),
        EnableSsl = true,
    };

    var msg = MoistureValue > 2 ? 
      "Your plant is above its recommended moisture go check on it!." : 
      "Your plant is a little dry, go water it!";
    
    smtpClient.Send("exampleemail@email.com", "My Name", "Plant Moisture", msg);
    
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 2021-08-15
    • 2022-12-22
    • 1970-01-01
    相关资源
    最近更新 更多