From f81ba724e7e3ebc41fe0119d53a8d6ebb08ff828 Mon Sep 17 00:00:00 2001 From: lulunac27a <100660343+lulunac27a@users.noreply.github.com> Date: Fri, 19 Apr 2024 22:17:21 -0500 Subject: [PATCH] Add commas for thousand seperator in command line application --- dice-game/src/Main.hx | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/dice-game/src/Main.hx b/dice-game/src/Main.hx index 476583f..91c7ab4 100644 --- a/dice-game/src/Main.hx +++ b/dice-game/src/Main.hx @@ -5,6 +5,19 @@ 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 = []; // initialize empty array of dice @@ -12,8 +25,8 @@ class Main { 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 } }