class Card
VALUES = %w(2 3 4 5 6 7 8 9 10 J Q K A)
SUITS = %w(S H D C)
def initialize(suit, value)
@suit = suit
@value = value
end
end
class Deck
attr_accessor :cards
def initialize
@cards = []
Card::SUITS.each do |suit|
Card::VALUES.each do |value|
@cards << Card.new(suit, value)
end
end
end
end
deck = Deck.new #<--- Store object in a variable
p deck.cards #<--- Use accessor
当我运行这个错误&#34;你不能这样做&#34;没有出现,但它被视为无效并要求用户重新输入数字直到有效,我怎样才能显示错误行?
答案 0 :(得分:1)
你需要抛出异常才能捕获一个:
for (int counter = 0; counter < AccountList.size(); counter++){
if (AccountList.contains(Value)) { //If Input = ArrayList number then display "hi"
System.out.println("Hi");
x = 2;
} else {
throw new Exception();
}
}
编辑:正如评论中所述,for line不是必需的,因为算法知道您的AccountList中是否包含该值是由contains方法处理的。