求算法的表示统计正负数字 运用选择,循环,要VB中的

2024-12-22 13:26:24
推荐回答(5个)
回答1:

下面是比较简短的代码,.使用一个标签记录数据,虽然LZ没要求,但直观一点。操作很简单,双击窗体,出现输入框,键入数字后回车,自动要求输下一个数,直到输入0结束,显示统计结果。 

Private Sub Form_DblClick() 

Do 

n = Val(InputBox("输入整数,0结束")) 

If n > 0 Then zs = zs + 1 Else If n < 0 Then fs = fs + 1 

If n <> 0 Then Label1 = Label1 & n & "," 

Loop Until (n = 0) 

Print "正数个数=" & zs & " 负数个数=" & fs 

End Sub 

效果如下:

回答2:

Private Sub Form_Click()
Form1.Cls
Dim A As Integer, B As Integer, St As String, N As Integer, S As Integer
N = 0
Do
St = InputBox("输入第" + Str(N + 1) + "个整数,0——结束", "", N + 1)
If IsNumeric(St) Then
S = Val(St)
If Int(S) = S Then
If S > 0 Then
A = A + 1
ElseIf S < 0 Then
B = B + 1
Else
Exit Do
End If
N = N + 1
Print N; S; Space(3);
If N Mod 5 = 0 Then Print
Else
MsgBox "请输入整数!"
End If
Else
MsgBox "请输入数字!"
End If
Loop
Print
Print "您共输入"; N; "个数字,其中正数"; A; "个,负数"; B; "个。"
End Sub

Private Sub Form_Load()
Form1.AutoRedraw = True
Print "单击窗口开始......"
End Sub

解释一下:1、本程序有较强的容错能力;2、记录数字是便于检查数字是否输入错误。若把这两个功能去掉,程序虽然变得简短,并能完成基本要求,但不能算一个完整的好程序。
已经发到指定邮箱中,请查收......

回答3:

Private Sub Command1_Click()
Dim n As Single
Dim s As String
Dim poscount As Integer
Dim negcount As Integer

Dim n As Single

s = InputBox("输入数字, 输入0结束。", "输入")
n = CSng(s)
If n > 0 Then
poscount = poscount + 1
ElseIf n < 0 Then
negcount = negcount - 1
End If

While n <> 0
s = InputBox("输入数字, 输入0结束。", "输入")
n = CSng(s)
If n > 0 Then
poscount = poscount + 1
ElseIf n < 0 Then
negcount = negcount - 1
End If
Wend

Print "正数:"; poscount
Print "负数:"; negcount

End Sub

回答4:

Dim s As String, z As Integer, f As Integer
Do Until s = "0"
s = InputBox("输入数据,0结束", "输入")
select case cint(s)
case is > 0
z = z + 1
case is < 0
f = f + 1
end select
Loop

MsgBox "正数有" & z & "个,负数有" & f & "个" '- - - 显示数量

最简单的程序!楼上的为什么都要绕来绕去记录数字?

回答5:

Private Sub Form_Load()
Dim a() As Single, b As String
Dim P As Integer
Dim N As Integer
Dim i As Integer
i = 0
Do
ReDim Preserve a(i)
b = InputBox("请输入数据", "数据输入")
If b = "0" Then
Exit Do
ElseIf IsNumeric(b) Then
a(i) = CSng(b)
If a(i) > 0 Then P = P + 1
If a(i) < 0 Then N = N + 1
Debug.Print a(i)
i = i + 1
End If

Loop
Debug.Print "正数的个数为:" & P
Debug.Print "负数的个数为:" & N
End Sub