抽奖arraylist验证

时间:2015-10-20 20:26:15

标签: c# random arraylist

我正在制作一个抽奖的c#程序,其中将绘制1-10个数字的随机数并将其放在// // OnozOmgEditingGuideView.m // CustomWatchFaceTest // // Created by Hamza Sood on 17/08/2015. // Copyright © 2015 Hamza Sood. All rights reserved. // #import "OnozOmgEditingGuideView.h" @implementation OnozOmgEditingGuideView { <---//Expected Method Body /* Initial setup steps: 1. Create the views and their corresponding labels 2. Set the label text 3. Constrain the views */ - (instancetype)initWithFrame:(CGRect)frame { if ((self = [super initWithFrame:frame])) { // 1 __strong UIView **viewsToCreate[] = { &_topView, &_bottomView }; __strong UILabel **labelsToCreate[] = { &_topLabel, &_bottomLabel }; for (int i = 0; i < sizeof(viewsToCreate)/sizeof(UIView**); i++) { UIView *view = [[UIView alloc]initWithFrame:CGRectZero]; [view setTranslatesAutoresizingMaskIntoConstraints:NO]; [self addSubview:view]; UILabel *label = [[UILabel alloc]initWithFrame:CGRectZero]; [label setTranslatesAutoresizingMaskIntoConstraints:NO]; [view addSubview:label]; NSLayoutAttribute labelAttributesToConstrain[] = { NSLayoutAttributeCenterX, NSLayoutAttributeCenterY }; for (int j = 0; j < sizeof(labelAttributesToConstrain)/sizeof(NSLayoutAttribute); j++) { NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:label attribute:labelAttributesToConstrain[j] relatedBy:NSLayoutRelationEqual toItem:view attribute:labelAttributesToConstrain[j] multiplier:1 constant:0]; [constraint setActive:YES]; } *viewsToCreate[i] = view; *labelsToCreate[i] = label; } // 2 [_topLabel setText:@"First Colour"]; [_bottomLabel setText:@"Second Colour"]; // 3 NSString *constraintsToAdd[] = { @"H:|[_topView]|", @"H:|[_bottomView]|", @"V:|[_topView]-(0)-[_bottomView(==_topView)]|" }; NSDictionary *constraintsViewsDictionary = NSDictionaryOfVariableBindings(_topView, _bottomView); for (int i = 0; i < sizeof(constraintsToAdd)/sizeof(NSString*); i++) { [NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:constraintsToAdd[i] options:0 metrics:nil views:constraintsViewsDictionary]]; } } return self; } @class - (void)setTopColor:(UIColor *)color { [_topView setBackgroundColor:color]; } - (void)setBottomColor:(UIColor *)color { [_bottomView setBackgroundColor:color]; } @end 上,如果数字已经绘制,那么它不应该放在数组中......我的问题是,即使我验证它,它仍然添加在阵列上。

这是我的代码:

ArrayList

2 个答案:

答案 0 :(得分:3)

您应该将第一个rnd.Next(1, 11)存储在变量中:

int x = rnd.Next(1, 11);
if (array.Contains(x))
{
     Console.WriteLine("Already Exist");
}
else
     array.Add(x);

答案 1 :(得分:2)

您没有存储

的结果
rnd.Next(1, 11)

所以,当它点击你的else语句时,它再次运行rnd.next()以获得一个绕过你的支票的新号码。

应该是:

case 1:
    int rndNumber = rnd.Next(1,11);
    if (array.Contains(rndNumber))
    {
        Console.WriteLine("Already Exist");
    }
    else
        array.Add(rndNumber);
相关问题