我之前发布了关于该项目的问题,在这里我再次感到困惑。我还在学习Visual Basic,我们的老师几乎没有解释循环以及如何使用它们。这是该计划:
创建一个Class Marks程序,提示用户提供班级中的学生人数。当用户单击“确定”时,程序应随机生成一个9位数的学生编号,该编号以" 071"并且标记在30到100之间,并在列表框控件中输出学生数字,标记和班级平均值。
到目前为止我的代码:
Dim total As Integer = Val(TextBox1.Text)
Dim randomNumber As New Random
Dim firstthree As String = "071"
Dim first As Integer = randomNumber.Next(0, 9)
Dim second As Integer = randomNumber.Next(0, 9)
Dim third As Integer = randomNumber.Next(0, 9)
Dim fourth As Integer = randomNumber.Next(0, 9)
Dim fifth As Integer = randomNumber.Next(0, 9)
Dim sixth As Integer = randomNumber.Next(0, 9)
Dim grade As Integer = randomNumber.Next(30, 100)
Dim studentNumber As String = firstthree & "-" & first & second & third & "-" & fourth & fifth & sixth
Dim counter As Integer = 0
If total = 5 Then
Do While counter < 5
Marks.Items.Add(studentNumber & vbTab & grade)
counter += 1
Loop
End If
我还在处理我的代码,我还在学习,所以我的代码可能不是最好的。我目前遇到的一个问题是当我添加&#34; studentNumber&#34;和&#34;成绩&#34;在列表框中,当它们全部不同时,它们都是相同的生成数字。我不知道如何在列表框中添加成绩以找到平均值,所以我不知道那部分。 如果有人可以帮助解决这个问题并帮助开发代码和程序,那将是一件幸事。
谢谢!
答案 0 :(得分:1)
修补程序:
将随机数的生成放在循环中,以便为个别学生提供不同的随机数。而不是
Do While counter < 5
Marks.Items.Add(studentNumber & vbTab & grade)
counter += 1
Loop
使用
Do While counter < 5
first = randomNumber.Next(0, 9)
second = randomNumber.Next(0, 9)
third = randomNumber.Next(0, 9)
fourth = randomNumber.Next(0, 9)
fifth = randomNumber.Next(0, 9)
sixth = randomNumber.Next(0, 9)
grade = randomNumber.Next(30, 100)
studentNumber = firstthree & "-" & first & second & third & "-" & fourth & fifth & sixth
Marks.Items.Add(studentNumber & vbTab & grade)
counter += 1
Loop
答案 1 :(得分:0)
您应该将学生表示为一个类来存储学生的ID和标记,并且具有一个返回标记平均值的函数。您可以在Class的新构造函数中创建学生ID和随机标记。
要在30-100之间创建随机标记,请使用Random.Next(30, 101)
,指定最低可能数字应为30,最高可能应低于101.
看看这个控制台应用程序示例:
Imports System
Public Module Module1
Private r As New Random
Public Sub Main()
'Declare a variable to keep track of the number of students
Dim count As Byte = 0
'Prompt for the number of students
Console.Write("# of Students: ")
'Loop until the user enters a valid value
Do Until Byte.TryParse(Console.ReadLine(), count) AndAlso count > 0
Console.WriteLine("Please enter a valid number between 1 and 255")
Loop
'Loop through the count to create a new student and then display their respective ID and average
For counter As Integer = 1 To count
Dim s As New Student(r)
Console.WriteLine("ID: {0} Average: {1}", s.ID, s.Average())
Next
End Sub
End Module
Public Class Student
Public Property ID As String
Public Property Marks As Integer()
Public Function Average() As Double
'Declare a placeholder variable to keep track of the total
Dim total As Double = 0
'Loop through each value in the collection
For Each mark As Double In Me.Marks
'Increment the total variable
total += mark
Next
'Return the average
Return total / Me.Marks.Length
End Function
Public Sub New(ByVal r As Random)
'Set the ID to a 9 digit code starting with 071, separated by hyphens after every third number
Me.ID = "071-" &
r.Next(0, 10) & r.Next(0, 10) & r.Next(0, 10) & "-" &
r.Next(0, 10) & r.Next(0, 10) & r.Next(0, 10)
'Create 10 random values between 30-100
ReDim Me.Marks(9)
For counter As Integer = 1 To 10
Me.Marks(counter - 1) = r.Next(30, 101)
Next
End Sub
End Class
小提琴:Live Demo
答案 2 :(得分:0)
我想我发现了一些你遇到的问题。
'For loop
For n As Integer = 1 To Integer.Parse(numStudents)
'Here, n will increment with every iteration of the loop, and stop when i > numStudents
'This is where you'll want to calculate the ID number for each student.
Next
randomNumber.Next(0, 9)
在值0和8之间返回一个随机数。如果您在循环内的代码中执行操作,这将为您提供所需的内容,但我建议您获得相同的结果但使用
更简洁studNumber = prefix & "-" & randomNumber.Next(0, 999).ToString("D3") & "-" & randomNumber.Next(0, 999).ToString("D3")
如果您需要课程平均分,您可能希望存储学生总数以及每个学生的个人分数。类似的东西:
Dim totStudents, totPoints, classAvg, points As Integer
'Get totStudents from input
totPoints = 0
For i As Integer = 1 To Integer.Parse(totStudents)
points = rando.Next(30, 100)
totPoints += points 'totPoints is now whatever value it used to be plus the value of points
'This is also where you want to get the student's ID number and add it to the listbox.
Next
classAvg = totPoints / totStudents
我认为如果你以正确的方式将这三者放在一起,它会给你你正在寻找的东西。希望它有所帮助。