元文系プログラミング初心者
プログラミング歴
java2ヶ月、vbnet一週間のものです。
初歩的なことかもしれませんが、
ヒントでも良いので、教えてくだされば
ありがたいです。宜しくお願いします。
###前提・実現したいこと
VBNETで電卓を作っています。
割り算機能を実装している中で分からない事が発生しました。
###発生している問題・エラーメッセージ
例えば9/=とすると、ゼロ除算の+∞
が出るようになっているのですが、
これを9/=という事が出来ないようにしたいと思っております。
厳密に言うと、+,-,*,/の後に=を押しても何も起こらない
仕様にしたいと思っているのですが、
どのようにすればよいでしょか。
###該当のソースコード
Public Class Form1
'////////// 整数の電卓画面クラス //////////
'----------<< 変数宣言 >>---------- ' 今回押されたボタンの数字以外の値(+,=,C)を保存する変数 Private ope As String = "C" ' 今回押されたボタンの値(0~9,+,=,C)を保存する変数 Private beforeIn As String = "C" ' 演算結果を保存する変数 Private ans As Double = 0 ' 今回入力された数値を保存する変数 Private num As Long = 0 ' 数字1ボタンクリック処理 Private Sub BtnOne_Click(sender As Object, e As EventArgs) Handles Button1.Click If beforeIn >= "0" And beforeIn <= "9" Then ' 直前の押されたのが数字ボタンのとき ' かつ、8桁未満の数値が入力されているとき If LblAnser.Text.Length < 8 Then LblAnser.Text &= "1" End If Else ' 直前の押されたのが数字以外のボタンのとき LblAnser.Text = "1" End If beforeIn = "1" End Sub ' 数字2ボタンクリック処理 Private Sub BtnTwo_Click(sender As Object, e As EventArgs) Handles Button2.Click If beforeIn >= "0" And beforeIn <= "9" Then If LblAnser.Text.Length < 8 Then LblAnser.Text &= "2" End If Else LblAnser.Text = "2" End If beforeIn = "2" End Sub ' 数字3ボタンクリック処理 Private Sub BtnThree_Click(sender As Object, e As EventArgs) Handles Button3.Click If beforeIn >= "0" And beforeIn <= "9" Then If LblAnser.Text.Length < 8 Then LblAnser.Text &= "3" End If Else LblAnser.Text = "3" End If beforeIn = "3" End Sub ' 数字4ボタンクリック処理 Private Sub BtnFour_Click(sender As Object, e As EventArgs) Handles Button4.Click If beforeIn >= "0" And beforeIn <= "9" Then If LblAnser.Text.Length < 8 Then LblAnser.Text &= "4" End If Else LblAnser.Text = "4" End If beforeIn = "4" End Sub ' 数字5ボタンクリック処理 Private Sub BtnFive_Click(sender As Object, e As EventArgs) Handles Button5.Click If beforeIn >= "0" And beforeIn <= "9" Then If LblAnser.Text.Length < 8 Then LblAnser.Text &= "5" End If Else LblAnser.Text = "5" End If beforeIn = "5" End Sub ' 数字6ボタンクリック処理 Private Sub BtnSix_Click(sender As Object, e As EventArgs) Handles Button6.Click If beforeIn >= "0" And beforeIn <= "9" Then If LblAnser.Text.Length < 8 Then LblAnser.Text &= "6" End If Else LblAnser.Text = "6" End If beforeIn = "6" End Sub ' 数字7ボタンクリック処理 Private Sub BtnSeven_Click(sender As Object, e As EventArgs) Handles Button7.Click If beforeIn >= "0" And beforeIn <= "9" Then If LblAnser.Text.Length < 8 Then LblAnser.Text &= "7" End If Else LblAnser.Text = "7" End If beforeIn = "7" End Sub ' 数字8ボタンクリック処理 Private Sub BtnEight_Click(sender As Object, e As EventArgs) Handles Button8.Click If beforeIn >= "0" And beforeIn <= "9" Then If LblAnser.Text.Length < 8 Then LblAnser.Text &= "8" End If Else LblAnser.Text = "8" End If beforeIn = "8" End Sub ' 数字9ボタンクリック処理 Private Sub BtnNine_Click(sender As Object, e As EventArgs) Handles Button9.Click If beforeIn >= "0" And beforeIn <= "9" Then If LblAnser.Text.Length < 8 Then LblAnser.Text &= "9" End If Else LblAnser.Text = "9" End If beforeIn = "9" End Sub ' 数字0ボタンクリック処理 Private Sub BtnZero_Click(sender As Object, e As EventArgs) Handles BtnZero.Click If beforeIn >= "0" And beforeIn <= "9" Then If LblAnser.Text.Length < 8 Then LblAnser.Text &= "0" End If beforeIn = "0" Else LblAnser.Text = "0" End If End Sub ' .ボタンクリック処理 Private Sub BtnPoint_Click(sender As Object, e As EventArgs) Handles BtnPoint.Click If beforeIn >= "0" And beforeIn <= "9" Then If LblAnser.Text.Length < 8 Then LblAnser.Text &= "." End If beforeIn = "." Else LblAnser.Text = "." End If End Sub ' +ボタンクリック処理 Private Sub BtnPlus_Click(sender As Object, e As EventArgs) Handles BtnPlus.Click If beforeIn >= "0" And beforeIn <= "9" Then If ope = "C" Then ' 最初の入力数値をansに保管 ans = CInt(LblAnser.Text) Else ' ansに保管されている ' +ボタンクリック前の数値と加算 num = CInt(LblAnser.Text) ans = ans + num LblAnser.Text = ans End If Else ' 前回クリックが数字以外の時 Select Case ope Case "C" ' 前回がクリアボタン ans = CInt(LblAnser.Text) Case "=" ' 前回がイコールボタン ans = ans + num Case "+" num = ans End Select End If beforeIn = "+" ope = "+" End Sub ' -ボタンクリック処理 Private Sub BtnMinus_Click(sender As Object, e As EventArgs) Handles BtnMinus.Click If beforeIn >= "0" And beforeIn <= "9" Then If ope = "C" Then ' 最初の入力数値をansに保管 ans = CInt(LblAnser.Text) Else ' ansに保管されている ' -ボタンクリック前の数値と加算 num = CInt(LblAnser.Text) ans = ans - num LblAnser.Text = ans End If Else ' 前回クリックが数字以外の時 Select Case ope Case "C" ' 前回がクリアボタン ans = CInt(LblAnser.Text) Case "=" ' 前回がイコールボタン ans = ans - num Case "+", "-" num = ans End Select End If beforeIn = "-" ope = "-" End Sub ' ×ボタンクリック処理 Private Sub BtnMulti_Click(sender As Object, e As EventArgs) Handles BtnMulti.Click If beforeIn >= "0" And beforeIn <= "9" Then If ope = "C" Then ' 最初の入力数値をansに保管 ans = CInt(LblAnser.Text) Else ' ansに保管されている ' ×ボタンクリック前の数値と乗算 num = CInt(LblAnser.Text) ans = ans * num LblAnser.Text = ans End If Else ' 前回クリックが数字以外の時 Select Case ope Case "C" ' 前回がクリアボタン ans = CInt(LblAnser.Text) Case "=" ' 前回がイコールボタン ans = ans * num Case "*" num = ans End Select End If beforeIn = "*" ope = "*" End Sub ' ÷ボタンクリック処理 Private Sub BtnDiv_Click(sender As Object, e As EventArgs) Handles BtnDiv.Click If beforeIn >= "0" And beforeIn <= "9" Then If ope = "C" Then ' 最初の入力数値をansに保管 ans = CInt(LblAnser.Text) Else ' ansに保管されている ' ÷ボタンクリック前の数値と乗算 num = CInt(LblAnser.Text) ans = ans / num LblAnser.Text = ans End If Else ' 前回クリックが数字以外の時 Select Case ope Case "C" ' 前回がクリアボタン ans = CInt(LblAnser.Text) Case "=" ' 前回がイコールボタン ans = ans / num Case "/" num = ans End Select End If beforeIn = "/" ope = "/" End Sub ' =ボタンクリック処理 Private Sub BtnEqual_Click(sender As Object, e As EventArgs) Handles BtnEqual.Click Try If beforeIn >= "0" And beforeIn <= "9" And Not beforeIn = "+""-""*""/" Then num = CInt(LblAnser.Text) End If ' 演算子を判定して処理を振分 Select Case ope Case "+" ans = ans + num Case "-" ans = ans - num Case "*" ans = ans * num Case "/" ans = ans / num End Select LblAnser.Text = ans beforeIn = "C" Catch ex As DivideByZeroException End Try End Sub Private Sub BtnClear_Click(sender As Object, e As EventArgs) Handles BtnClear.Click ans = 0 num = 0 beforeIn = "C" ope = "C" LblAnser.Text = "0" End Sub Private Sub Label1_Click(sender As Object, e As EventArgs) Handles LblAnser.Click End Sub
End Class
###試したこと
ネットでの検索(4時間)
###補足情報(言語/FW/ツール等のバージョンなど)
IDE:VS2015
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/06/09 09:49