2015-02-05 49 views
1

我正在嘗試創建一個簡單的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

回答

2

儘管其他答案顯示了這樣做的方法。我會建議他們,我會使用正確的工具。

你想要什麼,從概念上講,是字典是翻譯的等級(整數),爲一個字符串,所以我會走那個路線:

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

1

你試過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

0

,你可以用這樣的方式一切「如果... 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

0

首先,你需要把它轉換成一個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

1

你可以只使用一個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. 
} 
0

你應該考慮邏輯。 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"; 
0

這很簡單。試試這個:

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

相關問題