我可以存储两个文本框值? [英] Where I Can Store Two Textbox Values?
问题描述
假设= Integer.Parse(txtResult.Text.Trim())'假设值手动输入结果= Val(txtInput1.Text.Trim())+ Val(txtInput2.Text.Trim())'实际结果
i希望存储这两个值'现在我将这些值存储在arraylist中它比较错误计数'
然后我将获取比较后正确和错误的结果计数
对于 每个 str1 在 myarr1
对于 每个 str 在 myarr
如果 str = str1 然后
x + = 1
txtCorrect.Text = x
txtIncorrect.Text = y
结束 如果
如果 str<> str1 然后
txtCorrect.Text = x
y + = 1
txtIncorrect.Text = y
结束 如果
下一步
请帮帮我
代码块添加 - OriginalGriff [/ edit]
首先,让我们整理一下,这样就可以更容易地看到发生了什么:
< pre lang =vb> 对于 每个 str1 在 myarr1
对于 每个 str In myarr
如果 str = str1 那么
x + = 1
否则
y + = 1
结束 如果
txtCorrect.Text = x
txtIncorrect.Text = y
下一步
下一步
这和你的代码相同,只是更清洁一点。现在,计数不能错误,因为x和y中的一个或另一个将总是递增 - 所以总x + y将等于myarr中的字符串数乘以myarr1中的字符串数。
因此,如果它没有产生你期望的数字,那么有几个可能的原因:
1)x和/或y在你进入外循环之前不是零
2)数组不包含你认为他们做的数据
3)你想要的结果不是不同的数字组合。
首先使用调试器:在第一行放置一个断点,在调试器中运行app,然后查看数组,并在x和y。
然后按照代码观察会发生什么。
假设arraylist = [1,2,3]
结果arraylist = [2,1,3]
i需要比较这些[1,2 ],[2,1],[3,3]
结果会像这样
correct = 1
不正确= 2
哦,对!这是 简单!
你甚至不需要像你一样复杂的代码。
Dim 假设 As 整数()= { 1 , 2 , 3 }
Dim 结果作为 整数()= { 2 , 1 , 3 }
Dim 相同作为 整数 = 0
Dim diff 正如 整数 = 0
如果 assume.Length = result.Lengt h 然后
对于 i As 整数 = 0 assume.Length - 1
如果假设(i)= result(i)然后
相同+ = 1
其他
diff + = 1
结束 如果
下一步
txtCorrect.Text =相同
txtIncorrect.Text = diff
结束 如果您只需要
如果
,如果它们的大小可能不同。
assumed = Integer.Parse(txtResult.Text.Trim()) 'Assumed value Manually entered result = Val(txtInput1.Text.Trim()) + Val(txtInput2.Text.Trim())'Actual result
i want to store these two values 'present i am storing these values in arraylist it is giving wrong count when compared'
then i will fetch the correct and incorrect result count after comparesion
For Each str1 In myarr1
For Each str In myarr
If str = str1 Then
x += 1
txtCorrect.Text = x
txtIncorrect.Text = y
End If
If str <> str1 Then
txtCorrect.Text = x
y += 1
txtIncorrect.Text = y
End If
Next
please help me
[edit]Code block added - OriginalGriff[/edit]
First off, let's tidy that up a little, so it's easier to see what is going on:
For Each str1 In myarr1 For Each str In myarr If str = str1 Then x += 1 Else y += 1 End If txtCorrect.Text = x txtIncorrect.Text = y Next NextThat's the same code as yours, just a little cleaner. Now, the count can't be "wrong" because one or the other of "x" and "y" will always be incremented - so the total "x + y" will equal the number of strings in "myarr" multiplied by the number of strings in "myarr1".
So if it doesn't produce the numbers you expect, then there are several possible reasons:
1) x and / or y are not zero before you enter the outer loop
2) The arrays do not contain the data you think they do
3) The result you wanted is not the number of different combinations.
So start by using the debugger: put a breakpoint on the first line, run you app in the debugger, and look at the arrays, and at x and y.
Then follow the code through watching what happens.
"assumed arraylist=[1,2,3]
result arraylist=[2,1,3]
i need to compare like these [1,2],[2,1],[3,3]
result will like this
correct=1
incorrect=2"
Oh, right! That's easy!
You don't even need code as complicated as you have already.
Dim assumed As Integer() = {1, 2, 3} Dim result As Integer() = {2, 1, 3} Dim same As Integer = 0 Dim diff As Integer = 0 If assumed.Length = result.Length Then For i As Integer = 0 To assumed.Length - 1 If assumed(i) = result(i) Then same += 1 Else diff += 1 End If Next txtCorrect.Text = same txtIncorrect.Text = diff End IfYou only need the
If
at all if they might be different sizes.
这篇关于我可以存储两个文本框值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!