c# - The array dont save my numbers -
i trying learn c# , doing questions googeld. task do:
*"beginner level: task make dice game user throws 3 each 12-sided dice (numbers shall randomly selected , stored in array / field or list). add total of dice , show on screen. create function / method accepts figure (the total sum of dice). function / method should return text "good throw" if figure higher or equal 20. in other cases, text "sorry" returned. call function / method in main method , prints total , text.
advanced level: extension of task must use class simulate dice. user shall have option of writing x y-sided dice himself. if total sum of roll of dice generates score is> 50% of maximum score, words "good throw" displayed. logic can in main method. create class described in class diagram , use appropriate way in code."*
the thing cant work, array in class not save numbers im typing in... reslut 0. think have done big misstake cant see...
this main code:
static void main(string[] args) { list<dice> _dice = new list<dice>(); int = 0; int times = int.parse(interaction.inputbox("write how many times want repeat game:")); while (a != times) { int antthrow = int.parse(interaction.inputbox("write how many times want each dice thrown:")); int xchoice = int.parse(interaction.inputbox("write how many dice want throw:")); int ychoice = int.parse(interaction.inputbox("write how many sides want each dice should have:")); _dice.add(new dice(xchoice,ychoice, antthrow)); a++; } int e = 1; foreach (var item in _dice) { interaction.msgbox(string.format("result of game {0}: {1}", e++, item.tostring())); } }
this dice class:
class dice { static int _xchoice, _ychoice, _throw; static list<int> sum = new list<int>(); static int w = 0; static int _sum; static int[,] dice = new int[_xchoice, _ychoice]; public string tostring() { int half = _sum / 2; if (half <= _sum/2) { return "good throw!" + _sum; } else { return "bad throw!"; } } void random() { random rnd = new random(); while (w != _throw) { (int = 0; < dice.getlength(0); i++) { (int j = 0; j < dice.getlength(1); j++) { dice[i, j] = rnd.next(1, _ychoice); _sum += dice[j, i]; sum.add(_sum); } } w++; } } public tarning(int xchoice, int ychoice, int throw) { _throw = thorw; _xchoice = xchoice; _ychoice = ychoice; } }
class program { static void main(string[] args) { var dice = new list<dicelogic>(); int = 0; int times = gettimes(); while (a != times) { int antthrow = getantthrow(); int xchoice = getxchoice(); int ychoice = getychoice(); dice.add(new dicelogic(xchoice, ychoice, antthrow)); a++; } int e = 1; foreach (var item in dice) { console.writeline("result of game {0}: {1}", e++, item.tostring()); } console.readline(); } private static int gettimes() { while (true) { console.writeline("write how many times want repeat game:"); int times; var result = int.tryparse(console.readline(), out times); if (result) return times; console.writeline("value must number."); } } private static int getantthrow() { while (true) { console.writeline("write how many times want each dice thrown:"); int antthrow; var result = int.tryparse(console.readline(), out antthrow); if (result) return antthrow; console.writeline("value must number."); } } private static int getxchoice() { while (true) { console.writeline("write how many dice want throw:"); int getxchoice; var result = int.tryparse(console.readline(), out getxchoice); if (result) return getxchoice; console.writeline("value must number."); } } private static int getychoice() { while (true) { console.writeline("write how many sides want each dice should have:"); int getxchoice; var result = int.tryparse(console.readline(), out getxchoice); if (result) return getxchoice; console.writeline("value must number."); } } } public class dicelogic { public string tostring() { int maxscore = _dicesides*_dices; if (_result >= maxscore / 2) { return "good throw! " + _result; } return "bad throw! " + _result; } private readonly int _dices; private readonly int _dicesides; private readonly int _throwdice; private int _result; private void calculateresult() { var rnd = new random(); (int = 0; < _dices; i++) { int currentresult = 0; (int j = 0; j < _throwdice; j++) { currentresult = rnd.next(0, _dicesides); } _result += currentresult; } } public dicelogic(int dices, int dicesides, int throweachdice) { _dices = dices; _dicesides = dicesides; _throwdice = throweachdice; calculateresult(); } }
this example of how implement asking, go through te code line line debugger understand going on.
Comments
Post a Comment