使用Visual Basic的并行数组雕刻器 [英] Parallel Array Carver with Visual Basic

查看:102
本文介绍了使用Visual Basic的并行数组雕刻器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,允许Carver教授根据他在输入框中输入的可能点数(strMax)来显示成绩.然后,用户在文本框中输入获得的积分(dblPoints)

然后我解决百分比. dblResults = dblPoints/CDbl(strMax)

第一步是将最小百分比存储在一维数组中.我做到了.

第二步是将等级存储在一维数组中.我做到了.

数组应为并行数组. < ------遇到问题

然后它应该显示来自strGrades数组的相应成绩< ----未完成.

我无法显示并行的数组.问题是for循环

我试过了

I''m writing a program the allows Professor Carver to display a grade based on the number of possible points he enters in the inputbox (strMax). Then the user enters the points he earned in the textbox (dblPoints)

Then I solve the percentage. dblResults = dblPoints/CDbl(strMax)

The first step is to store the minimum percentage points in one dimensional array. I did that.

The second step is to store the grades in one dimensional array. I did that.

The arrays should be parallel arrays. <------ Having trouble with it

It then should display the corresponding grade from the strGrades array <---- Not done.

I''m having trouble to display arrays in that are parallel. The problem is the for loop

I tried this

For intGrade As Integer = 0 To 4
         If dblResults <= dblPer(intGrade) Then
             lblGrade.Text = strGrade(intGrade + 1)
         ElseIf dblResults >= dblPer(0) Then
             lblGrade.Text = strGrade(0)
         Else
             lblGrade.Text = strGrade(4)
         End If
     Next intGrade


我收到错误消息:索引超出了数组的范围." strGrade(intGrade + 1)出了点问题,但不知道如何解决.

我也尝试过这种方式


I''m getting the error that says "Index was outside the bounds of the array." There''s something wrong with the strGrade(intGrade+1), but no idea how to fix this.

I also tried this way

For intGrade As Integer = 0 To 4
    If dblResults >= dblPer(intGrade) Then
        lblGrade.Text = strGrade(intGrade + 1)
    End If
Next intGrade



仍然出现相同的错误?

对if语句有帮助吗?我不明白怎么了.

谢谢.

这是您不理解的代码



Still getting the same error?

Any help with the if statements? I don''t understand what''s wrong.

Thanks.

Here is the code in case you don''t understand

Private Sub btnDisplay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        '' display the grade the student gets
        Dim dblPoints As Double
        Dim dblResults As Double
        Dim strGrade() As String =
            {"A", "B", "C", "D", "F"}
        Dim dblPer() As Double =
            {0.9, 0.8, 0.7, 0.6, 0.5}

        Double.TryParse(txtPoints.Text, dblPoints)
        '' displays the percentage the student recieves
        dblResults = dblPoints / CDbl(strMax)
        '' determines the grade

        For intGrade As Integer = 0 To 4
            If dblResults >= dblPer(intGrade) Then
                lblGrade.Text = strGrade(intGrade + 1)
            End If
        Next intGrade


    End Sub
   Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '' get the total number of possible points
        strMax =
            InputBox("Enter the total possible points.", "Professor Carver")

    End Sub

推荐答案

这是一个简单的错误;代替lblGrade.Text = strGrade(intGrade + 1)使用lblGrade.Text = strGrade(intGrade).索引应从0strGrade.Length - 1.

重要说明:使用4的立即常数不可靠的.实际上,这是另一个错误,暂时不会显现出来,但是以后很容易困扰您.始终将Length用于数组,将Count用于集合等.

一个额外的建议:摆脱匈牙利符号.对于.NET,这根本没有任何意义.仅使所有名称具有语义,并使用适当的大小写;通常,找到(好的)Microsoft命名约定并使用它们.实际上,这不是很重要的细节,不,这很重要—在实践中.

—SA
This is a simple bug; instead of lblGrade.Text = strGrade(intGrade + 1) use lblGrade.Text = strGrade(intGrade). The indexes should run from 0 to strGrade.Length - 1.

An important note: using immediate constant of 4 is not reliable. Actually, this is another bug which will not be manifested… just yet, but can easily haunt you later. Always use Length for arrays, Count for collections, etc.

A bonus advice: get rid of you Hungarian notation. For .NET, it makes no sense at all. Just make all names semantic, use proper case; generally, find (good) Microsoft naming conventions and use them. In practice, this is not insignificant detail, no, this is important — in practice.

—SA


除了SAKryukov在解决方案2中发布的内容外,确实需要做的事情!这是我对您的代码的修改,以解决您的问题.

改变
In addition to what SAKryukov posted in solution 2 which does indeed need to be done! This is my modifications to your code to fix your problems.

change
Dim dblPer() As Double =
    {0.9, 0.8, 0.7, 0.6, 0.5}



对此



to this

Dim dblPer() As Double =
    {0.9, 0.8, 0.7, 0.6, 0.0}



然后更改此



then change this

For intGrade As Integer = 0 To 4
    If dblResults >= dblPer(intGrade) Then
        lblGrade.Text = strGrade(intGrade + 1)
    End If
Next intGrade



对此



to this

For intGrade As Integer = 0 To dblPer.Length
    If dblResults >= dblPer(intGrade) Then
        lblGrade.Text = strGrade(intGrade)
        Exit For
    End If
Next intGrade



我知道有点晚了,但希望对您有所帮助.



A little late I know but I hope it helps.


这篇关于使用Visual Basic的并行数组雕刻器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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