我正在為學校撰寫一個程式,一旦某個設備檢測到水分含量高于某個值,就會向用戶發送電子郵件。我不知道 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("[email protected]", "RandomPasswordRandomNumber"),
EnableSsl = true,
};
smtpClient.Send("[email protected]", "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("[email protected]", "RandomPasswordRandomNumber"),
EnableSsl = true,
};
smtpClient.Send("[email protected]", "My Name", "Plant Moisture", "Your plant is a little dry, go water it!");
}
}
uj5u.com熱心網友回復:
在編程中,我們努力減少重復。您的代碼可以簡化為:
if (MoistureValue != 2) {
var smtpClient = new SmtpClient("smtp.gmail.com", 587) {
Credentials = new NetworkCredential("[email protected]", "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("[email protected]", "My Name", "Plant Moisture", msg);
}
uj5u.com熱心網友回復:
你的牙套不平衡。你應該得到這樣的情況,即首先不會發生任何事情。所以也許你會去。
// When everything is okay, do nothing.
if (MoistureValue == 2) return;
var smtpClient = new SmtpClient("smtp.gmail.com")
{
Port = 587,
Credentials = new NetworkCredential("[email protected]", "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("[email protected]", "My name", "Plant Moisture", message);
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/369989.html
