如果数字在X和Y之间

问题描述:

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

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 ...

+3

如果你想处理*数字*,但此刻你有*字符串*,第一步应该是解析字符串。 'Int32.TryParse'可能是一个很好的开始... – 2015-02-05 18:44:25

+0

btw:你可以看一下枚举器 – jean 2015-02-05 18:44:47

+1

又一个变种:use [switch](https://msdn.microsoft.com/en-us/library/06tc147t .aspx)like switch(txtb_note.Text){case“1”:... break; case“2”:... break; ...; '' – Grundy 2015-02-05 18:49:03

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

你想要什么,从概念上讲,是字典是翻译的等级(整数),为一个字符串,所以我会走那个路线:

// 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; 

由于TryGetValueout参数可能会产生混淆,这里是另一种方式来做到这一点,这可能是更容易阅读,如果你是新的节目(我们将使用同样的<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"; 

(不通过这最后的代码混淆,正如我所说,这只是“循环回路”)

的其他方式(使用switchif-else)工作,但不是正确的OOL。从概念上思考,你想要做的是将一个值转换为不同的值:这是一个字典,并且在.NET中有相应的工具(Dictionary<T,T2>类)

如果你将来需要其他等级,你可以将它们添加到字典中,并且转换字符串的代码将继续工作。另外,该字典不需要存储在代码中:它可以从文本文件,Web服务,数据库或其他任何东西中检索,然后它甚至可以在没有重新编译应用程序的情况下工作。

+0

由于OP说他是编程新手,所以我已经编辑过对我的代码进行评论以显示它在做什么。我强烈** **不鼓励在生产代码上这样的评论(如果代码可以很容易阅读,则无需评论它在做什么) – Jcl 2015-02-05 19:20:00

+0

这是最好的答案。带有TryGetValue的字典 – 2015-02-05 21:17:20

你试过switch语句吗? 像这样

 string result; 
     switch (txtb_note.Text) 
     { 
      case "1": 
       result = "Very Good"; 
       break; 
      case "2": 
       result = "Good"; 
       break; 
      case "3": 
       result = "Normal"; 
       break; 
      case "4": 
       result = "Below Normal"; 
       break; 
      case "5": 
       result = "If you were Asian you would be dishonored"; 
       break; 
      default: 
       result = "Invalid Number"; 
       break; 
     } 
     return result; 

未在这些情况下,设置将陷入违约和返回“无效号码”

+2

你不需要在返回语句后中断 – 2015-02-05 18:51:37

+0

是的,我知道,但是如果他是编程新手,并且不知道switch语句并尝试再次使用它,他会得到一个错误,并不知道为什么,所以我把它留在那里,它不会杀死任何人 – Patrick 2015-02-05 18:52:39

+0

@帕特里克无法访问的代码是无法访问的代码,无论你是否是新编程或不。我会从代码中删除它(如果你想教他'switch'语句,然后在下面添加一个注释,显示正确的用法) – Jcl 2015-02-05 18:57:10

,你可以用这样的方式一切“如果... else”或“开关” 例如

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

这是我的第一个想法,但后来1-5说无效。只有6个表示“不足”。 – Jan 2015-02-05 18:55:13

+0

这是不可能的。你可以设置一个断点并通过单步调试功能。 – happybai 2015-02-05 19:10:57

首先,你需要把它转换成一个int!也许尝试像这样的 。

try{ 
int gradeNumber = int.Parse(txtb_note.Text); 
if(gradeNumber > 6) MessageBox.Show("Please enter a number between 1 and 6!"); 
else if(gradeNumber == 1) lbl_ergebnis.Text = "very good"; 
else if(gradeNumber == 2) lbl_ergebnis.Text = "good"; 
// and so on :) 
} 
catch(Exception){ 
MessageBox.Show("please enter a valid int!"); 
} 
+2

您应该先TryParse,而不是抛出异常 – 2015-02-05 18:52:44

你可以只使用一个if-else if-else

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

还是一个switch

switch(txtb_note.Text) 
{ 
    case "1": 
     lbl_ergebnis.Text = "very good"; 
     break; 
    case "2": 
     lbl_ergebnis.Text = "good"; 
     break; 
    default: 
     lbl_ergebnis.Text = "invalid"; 
     break; 
} 

那么你也应该考虑解析字符串到int允许其他选项。

int val; 
if(!int.TryParse(txtb_note.Text, out val) 
{ 
    lbl_ergebnis.Text = "Not a valid integer"; 
} 
else 
{ 
    if(val >= 0 && val <=4) 
     lbl_ergebnis.Text = "Bad"; 
    // Additional conditions. 
} 

你应该考虑逻辑。 1.将文本框的值转换为整数。 2.比较数字1和7

int value = 0; 
//TryParse returns true if the string can be converted to integer and converts the string to integer and pass to variable "value" 
if(Integer.TryParse(txtb_note.Text, out value)) 
{ 
    if(value > 1 && value < 7) 
     lbl_ergebnis.Text = "very good"; 
else lbl_ergebnis.Text = "invalid"; 
} 
else lbl_ergebnis.Text = "invalid"; 

这很简单。试试这个:

private void btn_note_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      int score = int.Parse(txtb_note.Text); 
     if (score >=1 && score <7) 
     { 
      if (score == 1) 
      { 
       lbl_ergebnis.Text = "very good"; 
      } 
      . 
      . 
      . 
      // Keep going to 6 
     } 
     else 
     { 
      lbl_ergebnis.Text = "invalid"; 
     } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.ToString()); 
     } 
    } 

你也可以使用switch

private void btn_note_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      int score = int.Parse(txtb_note.Text); 
     switch (score) 
     { 
      case "1": 
       score = "Very Good"; 
       break; 
      case "2": 
       score = "Good"; 
       break; 
       . 
       . 
       . 
       . 
       // Keep going to 6 
      default: 
       score = "invalid"; 
       break; 
     } 
     return score; 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.ToString()); 
     } 
    } 
+0

如果输入不是一个整数,这将抛出一个无关的异常。 – juharr 2015-02-05 19:00:17

+0

@juharr我已经更新了答案。 – aliboy38 2015-02-05 19:06:41