using System; using System.Collections.Generic; using System.Text; class Program { static void Main(string[] args) { Console.Write("Enter your name: "); string s; s = Console.ReadLine(); Console.WriteLine("Hello, {0} ", s); Console.Write("Enter your age: "); s = Console.ReadLine(); Console.WriteLine("You are {0} years old", s); } }
Author: coder
input a series of numbers separated by commas, parse them into integers and output the sum
using System;
class Class1 {
public static void Main(string[] args) {
Console.WriteLine(“Input a series of numbers separated by commas:”);
string input = Console.ReadLine();
char[] cDividers = { ',', ' ' };
string[] segments = input.Split(cDividers);
int nSum = 0;
foreach (string s in segments) {
if (s.Length > 0) {
if (IsAllDigits(s)) {
int num = Int32.Parse(s);
Console.WriteLine(“Next number = {0}”, num);
nSum += num;
}
}
}
Console.WriteLine(“Sum = {0}”, nSum);
}
public static bool IsAllDigits(string sRaw) {
string s = sRaw.Trim();
if (s.Length == 0) {
return false;
}
for (int index = 0; index < s.Length; index++) { if (Char.IsDigit(s[index]) == false) { return false; } } return true; } } [/csharp]
constructs sentences by concatenating user input until the user enters one of the termination characters
using System; public class MainClass { public static void Main(string[] args) { string sSentence = ""; for (; ; ) { Console.WriteLine("Enter a string"); string sLine = Console.ReadLine(); if (IsTerminateString(sLine)) { break; } sSentence = String.Concat(sSentence, sLine); Console.WriteLine(" You've entered: {0}", sSentence); } } public static bool IsTerminateString(string source) { string[] sTerms = {"EXIT","exit","QUIT","quit"}; foreach (string sTerm in sTerms) { if (String.Compare(source, sTerm) == 0) { return true; } } return false; } }
Convert input from control to upper case
using System; public class MainClass { static void Main() { Console.WriteLine("Enter command:"); string resp = (Console.ReadLine()).ToLower(); switch (resp) { case "a": Console.WriteLine("Doing Task A"); break; case "b": Console.WriteLine("Doing Task B"); break; case "c": Console.WriteLine("Doing Task C"); break; default: Console.WriteLine("Bad choice"); break; } } }
Use while(true) to read console input
using System; public class MainClass { static void Main() { string resp; while (true) { Console.WriteLine("Menu 1 - Task A"); Console.WriteLine("2 - Task B"); Console.WriteLine("E - E(xit)"); resp = (Console.ReadLine()).ToLower(); if (resp == "e") { break; } } } }
Use do while to read console input
using System; public class MainClass { static void Main() { string resp; do { Console.WriteLine("Menu 1 - Task A"); Console.WriteLine("2 - Task B"); Console.WriteLine("E - E(xit)"); resp = (Console.ReadLine()).ToLower(); } while (resp != "e"); } }
Terminate a control input
using System; public class MainClass { static void Main() { string resp; Console.WriteLine("Enter command ('x' to end):"); while ((resp = (Console.ReadLine()).ToLower()) != "x") { switch (resp) { case "a": Console.WriteLine("Doing Task A"); break; case "b": Console.WriteLine("Doing Task B"); break; default: Console.WriteLine("Bad choice"); break; } } } }