查看完整版本: 一个简单的随机生成彩票号码的小程序

itachi007 2007-9-23 14:26

一个简单的随机生成彩票号码的小程序

刚刚开始学习Ruby,觉得好多东西都跟以前的用法不一样,呼呼,先简简单单的做了一个随机生成彩票的小东东。
请大家指点一二。
[code]#=========================================================================
# Class: A simply number generator.
#        Generate the rand number between the 0 and
#        the inputed Number.
#        [0 < result < input number]
#=========================================================================
class NumberGenerator
        @@resultCnt = 0
        @@numCnt = 0
        
        def initialize(numCount, resultCount)
                @@result = Array.new(resultCount)
               
                1.upto(resultCount) do |i|
                        @@result = 0
                end
               
                # the upper bound of the rand number.
                @@numCnt = numCount
               
                # the output number's count.
                @@resultCnt = resultCount
        end
        
        def Generation()
               
                1.upto(@@resultCnt) do |i|
                        
                        loop do
                                randNum = rand(@@numCnt)
                                
                                flag = 0
                                
                                # Check if repeat.
                                1.upto(@@resultCnt) do |j|
                                        if randNum == @@result[j] then
                                                flag = 1
                                        end
                                end
                                
                                # no repeat, set
                                if flag == 0 then
                                        @@result = randNum
                                        break
                                end
                        end
                end
        end
   
    def OutputResult
            1.upto(@@resultCnt) do |i|
                    if i < @@resultCnt then
                            print @@result, ", "
                    else
                            puts @@result
                    end
            end
    end
end

#=========================================================================
# Module: Output the program infomation.
#=========================================================================
def outputPrgInfo
        # Clear the current screen.
        system("cls")
        
        # Output the program information.
        puts "#####################################################################"
        puts "#         lottery Generation Ver 1.0                                #"
        puts "#         powered by [email]starsoft007@sina.com[/email]                           #"
        puts "#####################################################################"
        puts ""
        puts "Input 'R' for replay, 'E' for exit."
        puts""
        
end

#=========================================================================
# Main process.
#=========================================================================

upperBound = 0
numberCounts = 0
response = ""

# Main loop.
loop do
        
        # Get the upper bound.
        loop do
                # Output the program information.
                outputPrgInfo
               
                # Get the upper bound of the numbers.
                puts "Please input the upper bound of the number's range:"
                STDOUT.flush
                upperBound = gets
                puts ""
               
                # Get the output counts.
                puts "Please input the counts of output number:"
                STDOUT.flush
                numberCounts = gets
                puts
               
                # Validation check.
                if numberCounts.to_i < 1 || upperBound.to_i < 1 ||
                   numberCounts.to_i >= upperBound.to_i then
                        puts "Input error!!!"
                        puts "Please input the data again."
                        tempChar = gets
                        
                        # Input again.
                        outputPrgInfo
                else
                        break
                end
        end
        
        loop do
                # Create a new object of NuberGeneration.
                numGen = NumberGenerator.new(upperBound.to_i + 1, numberCounts.to_i)
               
                # Generate the rand number between 0~43.
                numGen.Generation
               
                # Output the result.
                numGen.OutputResult
               
                # Get the input.
                response = gets
                response.chop!
        
                # Check wether exit.
                if response == "E" or response == "R" then
                        break
                end
        end
        
        if response == "E" then
                puts "Exit program!!!"
                break
        end
end
[/code]

[[i] 本帖最后由 itachi007 于 2007-9-23 14:32 编辑 [/i]]

itachi007 2007-9-23 14:34

有个问题想请教

不知道ruby中如何进行格式化输出呢。
例如像VB中的 format之类的函数。
或者是C中的printf(%3d, num)之类的。

xavier 2007-9-23 14:34

rn.rb:53:in `OutputResult': undefined method `sprint' for #<NumberGenerator:0x2b
b36d4> (NoMethodError)
        from rn.rb:51:in `upto'
        from rn.rb:51:in `OutputResult'
        from rn.rb:129
        from rn.rb:121:in `loop'
        from rn.rb:121
        from rn.rb:88:in `loop'
        from rn.rb:88

我猜你是想写"sprintf"

itachi007 2007-9-23 14:39

To xavier

你的速度还真是快呀。我已经把他改过来了。
对不起,一开始上传的有点问题。:)
页: [1]
查看完整版本: 一个简单的随机生成彩票号码的小程序