2016-12-09 8 views
0

私はDnDキャラクター作成プログラムを作成していますが、私は自分自身を "経験/レベル"の領域にこだわっています。私が望むのは、1000回の経験ごとにレベルが上がるためです(0〜999はレベル0です)。経験/レベルアップシステムの作成方法

私は次のコードを使用してラベルを変更していません私は選択のケースを使用している場合(経験は2000年に変更された場合、または2)経験(txtExperience)が1000年に変更されてから1(lblLevel)。

Private Sub txtExperience_textChanged(sender As Object, e As EventArgs) Handles txtExperience.TextChanged 
    If txtExperience.Text = letters Then 
     lblLevel.Text = "0" 
    ElseIf txtExperience.Text >= 10000 Then 
     lblLevel.Text = "Maxed" 
    End If 
    Select Case txtExperience.Text 
     Case Is <= "999" 
      lblLevel.Text = "0" 
     Case "1000" To "1999" 
      lblLevel.Text = "1" 
     Case "2000" To "2999" 
      lblLevel.Text = "2" 
     Case "3000" To "3999" 
      lblLevel.Text = "3" 
     Case "4000" To "4999" 
      lblLevel.Text = "4" 
     Case "5000" To "5999" 
      lblLevel.Text = "5" 
     Case "6000" To "6999" 
      lblLevel.Text = "6" 
     Case "7000" To "7999" 
      lblLevel.Text = "7" 
     Case "8000" To "8999" 
      lblLevel.Text = "8" 
     Case "9000" To "9999" 
      lblLevel.Text = "9" 
    End Select 


End Sub 

私は正直なところだろう、私はわかりません...正しく、If文を使用しようとしたとき(そして、それぞれのケースでは... ...)、どちらもうまくいかないでしょう。

+1

文字列が数字ではありません。 "9"は "1000"より大きいと評価されます。 Option Strictをオンにし、数字(整数)を使用します。 – Plutonix

+0

テキストはテキストです。それを数字と比較することはできません。 'Integer.Parse()'または 'Integer.TryParse()'で最初にテキストを解析すれば、比較できる数値が得られます。 –

答えて

3

さらに簡素化することができます。基本的な数学を使用して、あなたは長いSelect Case声明を取り除くことができます。

Dim Experience As Integer = 0 
If Integer.TryParse(txtExperience.Text, Experience) = True Then 
    If Experience >= 10000 Then 
     lblLevel.Text = "Maxed" 
     Return 
    End If 

    lblLevel.Text = Math.Floor(Experience/1000).ToString() 
Else 
    MessageBox.Show("Input must be a whole number between 0 and 10000", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) 
End If 

は、ここに私のコードのオンラインテストです: https://dotnetfiddle.net/VtGFLx

0

Thereの違いですとstring

Select Case Integer.Parse(txtExperience.Text) 
     Case Is <= 999 
      lblLevel.Text = 0 
     Case 1000 To 1999 
      lblLevel.Text = 1 
     Case 2000 To 2999 
      lblLevel.Text = 2 
     Case 3000 To 3999 
      lblLevel.Text = 3 
     Case 4000 To 4999 
      lblLevel.Text = 4 
     Case 5000 To 5999 
      lblLevel.Text = 5 
     Case 6000 To 6999 
      lblLevel.Text = 6 
     Case 7000 To 7999 
      lblLevel.Text = 7 
     Case 8000 To 8999 
      lblLevel.Text = 8 
     Case 9000 To 9999 
      lblLevel.Text = 9 
    End Select 
+0

ああ、私は見る!引用符が問題でした!ありがとうございました! –

関連する問題