Skip to content

Commit f81ba72

Browse files
committed
Add commas for thousand seperator in command line application
1 parent 880cab6 commit f81ba72

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

dice-game/src/Main.hx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,28 @@ class Main {
55
rollDice(Sys.stdin()); // get input to roll the dice based on input
66
}
77

8+
static function numberWithCommas(number:Int):String {
9+
var numString:String = Std.string(number);
10+
var numLen:Int = numString.length;
11+
var output:String = "";
12+
for (i in 0...numLen) {
13+
if ((numLen - i) % 3 == 0 && i > 0) {
14+
output += ",";
15+
}
16+
output += numString.charAt(i);
17+
}
18+
return output;
19+
}
20+
821
static function rollDice(input:haxe.io.Input):Void {
922
var numberOfDice:Int = Std.parseInt(input.readLine()); // parse input to integer
1023
var dice:Array<Int> = []; // initialize empty array of dice
1124
var sum:Int = 0; // initialize dice sum to 0
1225
for (i in 0...numberOfDice) { // loop to entered number of dice based on input
1326
dice[i] = Std.random(6) + 1; // add dice array to rolled die and make dice result is between 1 to 6
1427
sum += dice[i]; // add die value to sum
15-
trace('Dice ${i + 1}: ${dice[i]}'); // print die roll value
28+
trace('Dice ${numberWithCommas(i + 1)}: ${numberWithCommas(dice[i])}'); // print die roll value
1629
}
17-
trace('Sum of dice: ${sum}'); // print sum of all dice
30+
trace('Sum of dice: ${numberWithCommas(sum)}'); // print sum of all dice
1831
}
1932
}

0 commit comments

Comments
 (0)