java - Reading values from user in C# -
this code works fine, if user enters numbers in separate lines i.e. 10 12 result : 22
however, when try input there error. 10 12
now, know console.readline()
reads entire line, , space isn't int
, error. however, when used code in c, there scanf
function, specifying datatype expect user, run both cases. also, think java's scanner.nextint()
have not allowed such troubles. there simple way ask c# read data separated spaces, or lines in same manner? reason why never used c# in coding competitions. don't understand how solve problem.
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; namespace test { class program { static void main(string[] args) { int result=0,a, b; = int.parse(console.readline()); b = int.parse(console.readline()); result = + b; console.writeline("the result {0}", result); console.readkey(); } } }
here example extract number(s) string regex
string input = "there 4 numbers in string: 40, 30, , 10."; // split on 1 or more non-digit characters. string[] numbers = regex.split(input, @"\d+"); foreach (string value in numbers) { if (!string.isnullorempty(value)) { int = int.parse(value); console.writeline("number: {0}", i); } } console.readkey();
Comments
Post a Comment