c# - How do I store my numbers into an array? -
in code, trying generate 5 numbers. if of numbers equal 4, want store 4 array. i'm having trouble , code not store 4 array.
static void main() { random rand = new random(); int total = 0, randint; console.writeline("the computer rolling dice..."); int[] fours = new int[total]; (int = 0; < 5; i++) { randint = rand.next(10); console.writeline("the computer rolls {0:f0}", randint); if (randint == 4) { total +=fours[i]; //here trying store 4 array of 'fours'. } } console.writeline(total); //this prints 0, regardless of 4's random number generator has generated. want print out how many 4's have rolled. console.readline(); }
this:
total +=fours[i]
will attempt increment total
int
found @ index i
of array (which 0, because int defaults 0).
this:
fours[i] = 4;
is how assign 4 ith index in array.
read how assignment operator works in c#
the = operator called simple assignment operator. assigns value of right operand variable, property, or indexer element given the left operand.
Comments
Post a Comment