生成一个 7 位数的学号 [英] Generation of a 7 digit student number

查看:36
本文介绍了生成一个 7 位数的学号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用 vb.net,所以请在这里耐心等待.

I just started with vb.net so please be patient with me here.

我正在完成一项大学作业,需要建立一个小型学生注册计划.

I'm working on an uni assignment where I need to build a small student registration program.

每当用户点击注册时,我都需要创建一个 7 位数的学号,我的代码和验证以及与 DB 的连接工作正常,我只是在为生成学号而苦苦挣扎

I need to create a 7 digit student number whenever a user clicks register, my code and validation and connection to DB is working fine, Im just struggling with the generation of the student number

生成学生编号的说明

  1. 学号应为 7 位数字
  2. 前两位数字代表学生注册的年份(全年的最后两位数字),我得到这样的结果并且工作正常:Dim cyear = DateTime.Now.ToString("yy")

这是我的问题:接下来的 4 个数字代表到目前为止注册的学生人数,这个数字总是从左边开始用 0 填充,组成四位数字.

Here is my problem: the next 4 numbers represents the number of students who have registered thus far, this number is always padded with 0 from the left to make up four digits.

<小时>

示例

如果到目前为止(2014 年)已有 20 名学生注册,那么下一个学生将有学号140021

if 20 students have registered thus far (in 2014) then the next student will have student number 140021

4 最后一位是校验位,你把学号的前6个数字相加,结果除以10取余数,再减去10得到最后一位.很好,这很容易,但是如果到目前为止只有一名学生注册了怎么办?当然不能产生正确的结果

4 The last digit is a check digit which you get by summing the first 6 individual numbers of the student number, divide the result by 10 and take the remainder, and subtract the remainder by 10 to get last digit. Fine that is easy enough, but what if only one student has registered thus far? Surely that cant produce a correct result

我的问题

(请参阅上面的数字 3)如何生成数字 3 到 6,从左到右填充,每次新注册时递增 1?

(See Number 3 above) How do I generate numbers 3 to 6, padding from left to right incrementing with one with each new registration?

我尝试了以下代码,但离工作还很远

I tried the following code but it is far off from working

'Generate Student NR'
Dim newstudent As Integer
'displays 1st 2 letter of current year for student number'
Dim cyear = DateTime.Now.ToString("yy")
Dim lastdigit As Double
Dim lastdigitRemainder
Dim studentnr As Integer
'if statment to generate new student number for each registration'
If (register.Enabled = True) Then
    newstudent = cyear + 0 + 0 + 0 + 1
    'generate last digit of student nr'
    lastdigit = (cyear + newstudent) / 10
    lastdigitRemainder = lastdigit - 10
    studentnr = lastdigit + lastdigitRemainder
    MsgBox(studentnr)

我的界面是这样的

请注意,我不是要求某人为我完成此代码,我只是在寻找一些建议,可以为我指明正确方向的人等等. IF 语句也是我应该使用的正确选择结构学号的产生?

Please note Im not asking for someone to complete this code for me, im just looking for a bit of advice, someone who can point me in the right direction etc. Also is the IF statment the correct selection structure I should use for the generation of the student number?

推荐答案

  Dim intYear As Integer = TextBox1.Text 'The years last two digits'
  'Show the student id: pad the students number with 0's, works with single, double etc digits. The intStudents is the variable I used for the student totals.
  MessageBox.Show(CStr(intYear.ToString) & intStudents.ToString("0000")) 

更喜欢的方式...

  Dim strYear As String = TextBox1.Text
  MessageBox.Show(strYear & intStudents.ToString("D4")) 'D means the format and the number 4 is the length...

如果你问过你的问题...

Your issue with your if that you have asked...

'I assume this is your register button... if so you can do this..
 If (register.Enabled) Then
  'whatever else you need
 End If 

这是您完整编辑过的代码...

Heres you full edited code...

 'Generate Student NR'
 Dim newstudent As String
 Dim cyear As String = DateTime.Now.ToString("yy")
 Dim studentTotal As Integer = 13 'However many students registered so far...
'if statment to generate new student number for each registration'
 If (register.Enabled = True) Then
   newstudent = cyear & studentTotal.ToString("D4") 
   MessageBox.Show(newstudent) 'Use this as "MsgBox" is depreciated in newer frameworks'
 End If

'在用户变量中再添加一个...正如您所要求的 :)

'Add one more to user variable...As you requested :)

 Dim studentTotal As Integer = 13 

 'Put in your click event ...
 studentTotal = studentTotal += 1

这篇关于生成一个 7 位数的学号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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