gpt4 book ai didi

c# - "<= is an invalid expression term"和 ") expected"

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

我正在使用 C# 并正在制作原型(prototype) GUI(没有附加游戏,只是在按钮和按钮颜色上摆弄)。我遇到了一个错误:

private void temperValue_Load(object sender, EventArgs e)
{
int temperInt = 23;
temperInt = Convert.ToInt32(temperValue.Text);

if (temperInt >= 70)
{
temperButton.BackColor = System.Drawing.Color.Red;
}
else if (temperInt >= 40 & <= 69)
{
temperButton.BackColor = System.Drawing.Color.DarkOrange;
}
}

在“else if”行中,“<=”和“69)”均出现错误。 “<=”错误是“Invalid expression term '<='”,“69)”的四个错误是“) expected”、“Invalid expression term ')'”和两个“; expected”错误。

在这段代码之外没有任何变量影响这段代码。每个调用的变量都在代码片段中定义。

(对于任何好奇的人,“脾气”代表“温度”)

最佳答案

你不能在这样的 bool 条件中走捷径。

else if (temperInt >= 40 & <= 69)

必须改写为:

else if (temperInt >= 40 && temperInt <= 69)

请注意,在进行 bool 比较时,您通常需要使用双符号 &&。这会导致短路(只有在左侧成功时才评估两侧),这通常是需要的。正如我所说,您需要两次都包含 temperInt 标识符——您不能像在 SQL BETWEEN 中那样说“某个变量大于一个值且小于另一个值” 子句。

更新:根据 Eric 的建议修正了答案。

关于c# - "<= is an invalid expression term"和 ") expected",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8920751/

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