Skip to main content
 首页 » 编程设计

c#之如果数字在 X 和 Y 之间

2024年02月06日11bluestorm

我正在尝试创建一个简单的 Windows 窗体应用程序 (Visual Studio 2013) 以将(学校)成绩转换为文本,例如输入 1 -> “非常好”等等。

private void btn_note_Click(object sender, EventArgs e) 
{ 
      if (txtb_note.Text == "1") 
      { 
           lbl_ergebnis.Text = "very good"; 
      } 
 
      if (txtb_note.Text == "2") 
      { 
           lbl_ergebnis.Text = "good"; 
      }  
 
      // Etc... 
} 

等等。但我想补充一点,如果我输入 99 或只是一些文本,它应该显示“无效输入”。

但是我该怎么做呢? 我试过了

if (txtb_note.Text == "?????") 
{ 
      if (txtb_note.Text == "1") 
      { 
          lbl_ergebnis.Text = "very good"; 
      } 
} 
else  
{ 
    lbl_ergebnis.Text = "invalid"; 
} 

对于??????我需要一些写着“1 到 6 之间的数字”的东西,所以 7 会导致“无效”。我可以在那里使用什么?

希望你能理解我的问题。

顺便说一句:我对 C# 和 Visual Studio 还很陌生...

请您参考如下方法:

尽管其他答案显示了这样做的方法。我会反对他们,我会使用正确的工具。

从概念上讲,您想要的是一个将成绩(整数)转换为字符串的字典,所以我会采用这种方式:

  // We are going to use integer keys, with string values 
  var stringGrades = new Dictionary<int, string>() 
  { 
    {1, "good"}, // 1 is the Key, "good" is the Value 
    {2, "decent"}, 
    {3, "bad"} 
  }; 
 
  int integerGrade; 
  string textGrade; 
    // try to convert the textbox text to an integer 
  if(!Int32.TryParse(txtb_note.Text, out integerGrade)  
    // if successful, try to find the resulting integer  
    // (now in "integerGrade") among the keys of the dictionary 
     || !stringGrades.TryGetValue(integerGrade, out textGrade)) 
     // Any of the above conditions weren't successful, so it's invalid 
     lbl_ergebnis.Text = "Invalid value"; 
  else 
     // It worked, so now we have our string value in the variable "textGrade" 
     // obtained by the "out textGrade" parameter on TryGetValue 
     lbl_ergebnis.Text = textGrade; 

在您的特定情况下,您将原始成绩用作文本框中的字符串,因此如果您愿意,可以采用该路线:

  // This time, we directly use string as keys 
  var stringGrades = new Dictionary<string, string>() 
  { 
    {"1", "good"}, 
    {"2", "decent"}, 
    {"3", "bad"} 
  }; 
 
  string textGrade; 
  // Try to get the value in the dictionary for the key matching the text 
  // on your textbox, no need to convert to integer 
  if(!stringGrades.TryGetValue(txtb_note.Text, out textGrade)) 
     lbl_ergebnis.Text = "Invalid value"; 
  else 
     lbl_ergebnis.Text = textGrade; 

自 TryGetValue和 out参数可能会令人困惑,这是另一种方法,如果您是编程新手,可能更容易阅读(我们将使用与上面相同的 <string, string> 字典):

  // If our dictionary doesn't contain the key we are looking for, 
  // the input is invalid 
  if(!stringGrades.ContainsKey(txtb_note.Text)) 
     lbl_ergebnis.Text = "Invalid value"; 
  else 
     // it contains the key, so let's show its value: 
     lbl_ergebnis.Text = stringGrades[txtb_note.Text]; 

如果你想循环循环,可以将其转换为单行代码,例如:

  lbl_ergebnis.Text = stringGrades.ContainsKey(txtb_note.Text) ? 
      stringGrades[txtb_note.Text] : "Invalid value"; 

(不要被最后一段代码搞糊涂了,正如我所说,那只是“循环”)

其他方法(使用 switch 或 if-else )有效,但不是正确的工具。从概念上思考,您要做的是将一个值转换为另一个值:这是一个字典,并且在 .NET 中有相应的工具(Dictionary<T,T2> 类)

如果您以后需要其他成绩,只需将它们添加到字典中,转换字符串的代码将继续运行。此外,该词典不需要存储在代码中:它可以从文本文件、Web 服务、数据库或其他任何内容中检索,然后它甚至无需重新编译您的应用程序即可工作。