vb 2013 输入框字符串转换错误中的输入验证 [英] input validation in vb 2013 input box string conversion error

查看:32
本文介绍了vb 2013 输入框字符串转换错误中的输入验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在输入框中输入字母字符或将输入框留空时,我的程序崩溃.为什么我的验证 if 语句不起作用?

Option Strict On
Public Class frmPickUpSticks
Dim playersTurn As Boolean = False
Dim remainingSticks As Integer 'count sticks
Dim losses As Integer = 0 'count player losses
Private Sub btnNewGame_Click(sender As Object, e As EventArgs) Handles btnNewGame.Click
    lblOutput.Text = ""
    remainingSticks = CInt(InputBox("How many matchsticks would you like (5 - 25)?", "Pick Number of Matches!"))
    'Validate input
    If IsNumeric(remainingSticks) Then
        If (remainingSticks >= 5) And (remainingSticks <= 25) Then
            DisplayMatches()
            If (remainingSticks Mod 4 = 1) Then
                MessageBox.Show("You go first!")
                playersTurn = True
                turns()
            Else
                MessageBox.Show("I go first.")
                turns()
            End If
        Else
        MessageBox.Show("Please enter a number between 5 and 25.")
    End If
Else
    MessageBox.Show("Input must be numeric.", "Input Error")
End If

推荐答案

你应该在输入框中使用字符串变量

You should use a string variable in the inputbox

将 st 变暗为字符串

dim st as string

st = InputBox("你想要多少根火柴棍 (5 - 25)?", "选择火柴的数量!"))

st = InputBox("How many matchsticks would you like (5 - 25)?", "Pick Number of Matches!"))

剩余棒数 = val(st)

remainingSticks = val(st)

...

这篇关于vb 2013 输入框字符串转换错误中的输入验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆