在程序退出之前为用户提供一定数量的尝试

时间:2015-11-20 13:20:16

标签: arrays ruby

我正在创建一种方法,用户输入“PIN”号码来访问不同的方法..是的,这是一台ATM ..

我正在试图弄清楚如何让用户获得特定数量的尝试,直到程序exit为止。

经过一系列的研究后,我发现没有什么有用的东西..我发现的唯一一件事就是使用.to_s.split(//)来将try的数量添加到一个空数组中。这对我来说没有意义,因为你为什么要把一个整数变成一个字符串..?

所以我的问题是,你是如何做到的,以便用户只有一定的尝试次数,3,直到他们被赶出程序?

主要来源:

#!/usr/bin/env ruby

################
#ATM Rewrite 
#                                
#Creator Lost Bam      Not completed yet.
#                        
#11/19/15                
##################

require_relative 'checking.rb'
require_relative 'savings.rb'
require_relative 'exit.rb'
require_relative 'loan.rb'
require_relative 'transfer.rb'
require_relative 'redirect.rb'

class ATM
    attr_accessor :name, :checking_account, :savings_account, :pin_number, :transfer, :loan
        def initialize( name, checking_account, savings_account )
            @name = name
            @checking_account = checking_account
            @savings_account = savings_account
            @pin_number = pin_number
        end
    end
    ##############
    def pin
        x = []
        puts "Enter PIN Number:"
        input = gets.chomp.to_i
        if input == 1234
            menu
        else
            x += 1.to_s.split(//) #<= This is what I found to convert integer to Array
            puts "Invalid PIN, try again:"
            input = gets.chomp
            if x == 3
                bad_pin
            end
        end
    end
    ############
    def menu #add #{name} on line 41
        puts <<-END.gsub(/^\s*>/, ' ')
            >
            >Welcome thank you for choosing Bank of Bam.
            >You may choose from the list below of what you would like to do
            >For checking inquiries press '1'
            >For savings account information press '2'
            >To transfer funds press '3'
            >To either apply for a loan, or get information on a loan press '4'
            >To exit the system press '5'
            >
        END
        input = gets.chomp
        case input.to_i
        when 1
            checking_information
        when 2
            savings_information
        when 3
            transfer_funds
        when 4
            loan_info
        when 5
            exit_screen
        else
            puts "Invalid option please try again"
            menu
        end
    end

    def bad_pin
        abort('Invalid PIN exiting sytem..')
        exit
    end
    pin

尝试新事物:

def pin
    x = 3
    puts "Enter PIN(#{x} attempts left):"
    pin_num = gets.chomp
    case pin_num.to_i
    when 1234
        menu
    else
        puts "Invalid PIN"
        x -=1
        return pin
        if x == 0
            bad_pin
        end
    end
end

它不会增加数字,只是继续说3次尝试:

Enter PIN(3 attempts left):
4567
Invalid PIN
Enter PIN(3 attempts left):
45345
Invalid PIN
Enter PIN(3 attempts left):
6456
Invalid PIN
Enter PIN(3 attempts left):
4564
Invalid PIN

1 个答案:

答案 0 :(得分:1)

您的问题是每次调用该方法时,x的值都会重置。你需要在pin方法中设置一个循环来跟踪尝试。

def pin
    x = 3
    while (x > 0) do 
        puts "Enter PIN(#{x} attempts left):"
        pin_num = gets.chomp
        case pin_num.to_i
        when 1234
            menu
        else
            puts "Invalid PIN"
            x -=1
            puts "no tries left" if x == 0
            break if x == 0
        end
    end
end

留在方法中。回想一下这个方法会让你三次尝试。