我的第一堂课!!但它没有按预期工作 - 请帮助! [英] My FIRST Class!! But it doesn't work as expected - please HELP!

查看:51
本文介绍了我的第一堂课!!但它没有按预期工作 - 请帮助!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用过VB3 - VB6,所以学习所有这些OO的东西对我来说是相当新的

(虽然几年前我看过Java)。无论如何,我以为我会写一个小类开始,有一个属性和2个方法,

和一个构造函数(你在哪里使用New(),I想想,是吗?)。


所以,我决定用一个Roll()方法创建一个代表一个骰子的类,它可以滚动成b
- 并且有一个属性可以找出

掷骰后面部的值 - GetValue()。然后我用
添加了一个Display()方法,将模具的值打印到

控制台窗口。


现在我的想法是,在编写New()构造函数时,我应该

滚动()模具以获得第一个随机数值。确实,这工作好了。
罚款。但是,当我实例化(或者我说消耗?)班级

两次 - 骰子总是显示相同的值!!!


我看不到哪里出错了 - 有人可以指出

我的方式错误吗?


代码非常小 - 这里是班级(在Class1.vb)...


公共类死亡


私有_值作为Int16

Dim _generator As New随机


公共子新()

Me.Roll()

结束子


Public ReadOnly Property GetValue()As Int16

获取

返回_value

结束获取

结束物业


Public Sub Roll()

_value = _generator.Next(1,7)

End Sub


公共子显示()

Console.WriteLine(Me.GetValue)

结束子


结束类


Module1.vb文件保存此代码...


模块模块1


S. ub主要()


昏暗的一个新的死亡

昏暗的两个新的死亡




One.Display()

One.Roll()

Two.Display()

两个。 Roll()


循环直到UCase(Console.ReadLine)=" Q"


End Sub


结束模块


运行时,值列表如下......


1

1


2

2


6

6


5

5

3

3


为什么如果它们应该是随机的那么它们会重复 -

在Dim One / Two中的某些东西作为New Die我不喜欢

或者在构造函数New()中有一些我不理解的东西。


提前感谢您的帮助。

I have used VB3 - VB6, so learning all this OO stuff is reasonably new
to me (although I looked at Java a few years ago). Anyway, I thought I
would write a small class to begin with, with a property and 2 methods,
along with a constructor (where you use New(), I think, yes?).

So, I decided to create a class that represents a die, which can be
rolled - using a Roll() method - and which has a property to find out
the value on the face of the die after a roll - GetValue(). I then
added a Display() method that prints out the value of the die to the
console window.

Now, my thinking was that in writing the New() constructor I should
Roll() the die to get a 1st random number value. Indeed this worked
fine. However, when I instantiated (or do I say consumed?) the class
twice - the dice always show the same value!!!

I cannot see where this is going wrong - could someone please point out
the error of my ways?

The code is quite small - and here is the class (in Class1.vb)...

Public Class Die

Private _value As Int16
Dim _generator As New Random

Public Sub New()
Me.Roll()
End Sub

Public ReadOnly Property GetValue() As Int16
Get
Return _value
End Get
End Property

Public Sub Roll()
_value = _generator.Next(1, 7)
End Sub

Public Sub Display()
Console.WriteLine(Me.GetValue)
End Sub

End Class

The Module1.vb file holds this code...

Module Module1

Sub Main()

Dim One As New Die
Dim Two As New Die

Do

One.Display()
One.Roll()
Two.Display()
Two.Roll()

Loop Until UCase(Console.ReadLine) = "Q"

End Sub

End Module

When run, the values are listed like this...

1
1

2
2

6
6

5
5

3
3

Why do they duplicate if they are supposed to be random - there is
either something in the Dim One/Two as New Die that I don''t appreciate
or there is something in the Constructor New() that I don''t understand.

Thanks in advance for any help.

推荐答案

好吧,我会这样做

Public Class Die


私有_value作为Int16


公共子新()


随机化()


我。 Roll()


结束子


Public ReadOnly属性GetValue()如Int16


获取


返回_value


结束获取


结束物业


Public Sub Roll()


''初始化随机数生成器。


随机化()


''生成1到6之间的随机值。


_value = CInt(Int((6 * Rnd())+ 1))


结束子


公共子显示()


Console.WriteLine(Me.GetValue)


End Sub


结束班


模块模块1


Sub Main()


昏暗的一个新的死亡


昏暗的两个新的死亡





One.Display()


One.Roll()


两个。显示()


Two.Roll()


循环直到UCase(Console.ReadLine)=" Q"


End Sub


结束模块


似乎按预期工作


问候


Michel Posseth [MCP]


Cap''n Ahab < SP *********** @ lycos.com>在消息中写道

news:11 ********************** @ g14g2000cwa.googlegr oups.com ...
well i would do it like this
Public Class Die

Private _value As Int16

Public Sub New()

Randomize()

Me.Roll()

End Sub

Public ReadOnly Property GetValue() As Int16

Get

Return _value

End Get

End Property

Public Sub Roll()

'' Initialize the random-number generator.

Randomize()

'' Generate random value between 1 and 6.

_value = CInt(Int((6 * Rnd()) + 1))

End Sub

Public Sub Display()

Console.WriteLine(Me.GetValue)

End Sub

End Class

Module Module1

Sub Main()

Dim One As New Die

Dim Two As New Die

Do

One.Display()

One.Roll()

Two.Display()

Two.Roll()

Loop Until UCase(Console.ReadLine) = "Q"

End Sub

End Module

wich seems to work as expected

regards

Michel Posseth [MCP]


"Cap''n Ahab" <sp***********@lycos.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
我使用过VB3 - VB6,所以学习所有这些OO的东西对我来说是相当新的(虽然几年前我看过Java)。无论如何,我以为我会写一个小类开头,有一个属性和2个方法,
以及一个构造函数(你在哪里使用New(),我想,是吗?)。所以,我决定创建一个代表模具的类,它可以使用Roll()方法进行滚动 - 并且具有查找值的属性。滚动后面部死亡 - GetValue()。然后我添加了一个Display()方法,将模具的值打印到
控制台窗口。

现在,我的想法是编写New()构造函数我应该滚动()模具以获得第一个随机数值。确实这很好用。然而,当我实例化(或者我说消耗?)两次类时 - 骰子总是显示相同的值!!!

我看不出这出错的地方 - 可能有人请指出我的方式错误?

代码非常小 - 这是类(在Class1.vb中)......

公开Class Die

私有_value为Int16
Dim _generator为新随机

公共子新()
Me.Roll()
结束Sub

Public ReadOnly Property GetValue()作为Int16
返回_value
结束获取
结束物业

Public Sub Roll()
_value = _generator.Next(1,7)
End Sub

Public Sub Display()
Console.WriteLine(Me.GetValue)

结束类

Module1.vb文件包含此代码......

模块模块1

> Sub Main()

Dim One As New Die
昏暗的两个新的死亡

一个。显示()
One.Roll()
两个。显示()
两个.Roll()

循环直到UCase(Console.ReadLine)=" Q"

End Sub

结束模块

运行时,值列出如下......

1

2
2
> 6
5
5

3

如果他们是应该是随机的 - 在Dim One / Two中有什么东西可以作为New Die我不喜欢
或者在构造函数New()中有一些我不明白的东西。

提前感谢您的帮助。
I have used VB3 - VB6, so learning all this OO stuff is reasonably new
to me (although I looked at Java a few years ago). Anyway, I thought I
would write a small class to begin with, with a property and 2 methods,
along with a constructor (where you use New(), I think, yes?).

So, I decided to create a class that represents a die, which can be
rolled - using a Roll() method - and which has a property to find out
the value on the face of the die after a roll - GetValue(). I then
added a Display() method that prints out the value of the die to the
console window.

Now, my thinking was that in writing the New() constructor I should
Roll() the die to get a 1st random number value. Indeed this worked
fine. However, when I instantiated (or do I say consumed?) the class
twice - the dice always show the same value!!!

I cannot see where this is going wrong - could someone please point out
the error of my ways?

The code is quite small - and here is the class (in Class1.vb)...

Public Class Die

Private _value As Int16
Dim _generator As New Random

Public Sub New()
Me.Roll()
End Sub

Public ReadOnly Property GetValue() As Int16
Get
Return _value
End Get
End Property

Public Sub Roll()
_value = _generator.Next(1, 7)
End Sub

Public Sub Display()
Console.WriteLine(Me.GetValue)
End Sub

End Class

The Module1.vb file holds this code...

Module Module1

Sub Main()

Dim One As New Die
Dim Two As New Die

Do

One.Display()
One.Roll()
Two.Display()
Two.Roll()

Loop Until UCase(Console.ReadLine) = "Q"

End Sub

End Module

When run, the values are listed like this...

1
1

2
2

6
6

5
5

3
3

Why do they duplicate if they are supposed to be random - there is
either something in the Dim One/Two as New Die that I don''t appreciate
or there is something in the Constructor New() that I don''t understand.

Thanks in advance for any help.



" Cap''n Ahab" < SP *********** @ lycos.com> schrieb
"Cap''n Ahab" <sp***********@lycos.com> schrieb
Dim _generator As New Random
Dim _generator As New Random




随机数实际上不是随机数。获得更随机的数字,使用


Dim _generator As New Random(Environment.TickCount)

Armin



Random numbers are not really Random. To get a "more random" number, use

Dim _generator As New Random(Environment.TickCount)
Armin


> ;随机数不是真正的随机数。获得更随机的数字,使用
> Random numbers are not really Random. To get a "more random" number, use

Dim _generator As New Random(Environment.TickCount)



Armin ,,,,,它这样做自动如果你不提供初始编号

http://msdn.microsoft.com/library/de...mrandomize.asp

虽然当我重新考虑这种情况


这也可以解决他的问题,因为它现在内部会相同

多次调用随机化

>
公共类别死亡


私有_value为Int16


我.Roll()

结束子


公共ReadOnly属性GetValue()为Int16

获取

返回_value

结束获取

结束物业


Public Sub Roll()

Dim _generator作为新随机

_value = _gen erator.Next(1,7)

End Sub


Public Sub Display()

Console.WriteLine(Me.GetValue) )

结束子


结束课

问候


Michel Posseth [MCP ]

" Armin Zingler" < AZ ******* @ freenet.de>在消息中写道

news:%2 **************** @ TK2MSFTNGP14.phx.gbl ..." Cap''n Ahab" < SP *********** @ lycos.com> schrieb

Dim _generator As New Random(Environment.TickCount)

Armin ,,,,, it does this automaticly if you do not provide an init number

http://msdn.microsoft.com/library/de...mrandomize.asp
although when i rethink this situation

this would also solve his problem , cuase it will now internally be the same
as calling randomize multiple times

Public Class Die

Private _value As Int16

Public Sub New()
Me.Roll()
End Sub

Public ReadOnly Property GetValue() As Int16
Get
Return _value
End Get
End Property

Public Sub Roll()
Dim _generator As New Random
_value = _generator.Next(1, 7)
End Sub

Public Sub Display()
Console.WriteLine(Me.GetValue)
End Sub

End Class
regards

Michel Posseth [MCP]
"Armin Zingler" <az*******@freenet.de> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl... "Cap''n Ahab" <sp***********@lycos.com> schrieb
Dim _generator As New Random
Dim _generator As New Random



随机数实际上不是随机数。获得更随机的数字,使用

Dim _generator As New Random(Environment.TickCount)

Armin



Random numbers are not really Random. To get a "more random" number, use

Dim _generator As New Random(Environment.TickCount)
Armin



这篇关于我的第一堂课!!但它没有按预期工作 - 请帮助!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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