Skip to content

Commit

Permalink
Add commas for thousand seperator in command line application
Browse files Browse the repository at this point in the history
  • Loading branch information
lulunac27a committed Apr 20, 2024
1 parent 880cab6 commit f81ba72
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions dice-game/src/Main.hx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,28 @@ class Main {
rollDice(Sys.stdin()); // get input to roll the dice based on input
}

static function numberWithCommas(number:Int):String {
var numString:String = Std.string(number);
var numLen:Int = numString.length;
var output:String = "";
for (i in 0...numLen) {
if ((numLen - i) % 3 == 0 && i > 0) {
output += ",";
}
output += numString.charAt(i);
}
return output;
}

static function rollDice(input:haxe.io.Input):Void {
var numberOfDice:Int = Std.parseInt(input.readLine()); // parse input to integer
var dice:Array<Int> = []; // initialize empty array of dice
var sum:Int = 0; // initialize dice sum to 0
for (i in 0...numberOfDice) { // loop to entered number of dice based on input
dice[i] = Std.random(6) + 1; // add dice array to rolled die and make dice result is between 1 to 6
sum += dice[i]; // add die value to sum
trace('Dice ${i + 1}: ${dice[i]}'); // print die roll value
trace('Dice ${numberWithCommas(i + 1)}: ${numberWithCommas(dice[i])}'); // print die roll value
}
trace('Sum of dice: ${sum}'); // print sum of all dice
trace('Sum of dice: ${numberWithCommas(sum)}'); // print sum of all dice
}
}

0 comments on commit f81ba72

Please sign in to comment.