c# - How do I make a command to add a variable and let the print command print the number or string of it -
class program { static void main(string[] args) { console.backgroundcolor = consolecolor.blue; console.clear(); { //print command string print = console.readline(); if (print.tolowerinvariant().startswith("print: ")) { string p2 = print.substring(print.indexof(' ') + 1); console.writeline(p2); } string var = console.readline(); if (var.tolowerinvariant().startswith("var ")) { string v2 = var.substring(var.indexof(' ') + 1); } } while (true); } } i want how can create var typing var , set number or string , able print number or string
var keyword. solve problem renaming var variable:
class program { static void main(string[] args) { console.backgroundcolor = consolecolor.blue; console.clear(); { //print command string print = console.readline(); if (print.tolowerinvariant().startswith("print: ")) { string p2 = print.substring(print.indexof(' ') + 1); console.writeline(p2); } string variable = console.readline(); if (variable.tolowerinvariant().startswith("var ")) { string v2 = variable.substring(variable.indexof(' ') + 1); } } while (true); } } edit: new requirements, code came it's without error handling:
using system; using system.collections.generic; namespace variables { class program { static void main(string[] args) { console.backgroundcolor = consolecolor.blue; console.clear(); var dict = new dictionary<string, int>(); { //print command string command = console.readline(); if (command.tolowerinvariant().startswith("print: ")) { string p2 = command.substring(command.indexof(' ') + 1); if (dict.containskey(p2)) console.writeline(dict[p2]); else console.writeline("variable {0} undefined!"); } if (command.tolowerinvariant().startswith("var ")) { string v2 = command.substring(command.indexof(' ') + 1); string[] parts = v2.split(new char[]{'='}, 2, stringsplitoptions.removeemptyentries); parts[0] = parts[0].trim(); parts[1] = parts[1].trim(); dict.add(parts[0], int.parse(parts[1])); } } while (true); } } }
Comments
Post a Comment