>var2); Console.ReadKey(); } } }5. IF语句的example:
int v1 ,v2;
string v3;
Console.WriteLine(\"Please enter the first number:\"); v1=Convert.ToInt32(Console.ReadLine ());
Console.WriteLine(\"Please enter the second number:\"); v2 = Convert.ToInt32(Console.ReadLine()); if (v1 > v2)
v3 = \"greater than\"; else
if (v1 == v2) v3 = \"equal to\"; else v3 = \"less than\";
Console.WriteLine(\"the first number is {0} the second number !\",v3 );
Console.ReadKey();
6. Switch语句的example:
int v1 ,v2;string v3;
Console.WriteLine(\"Please enter the student's grade:\"); v1=Convert.ToInt32(Console.ReadLine ()); v2 = v1 / 10; switch (v2)
{case 1:case 2:case 3:case 4:case 5: v3=\"flunk\"; break ; case 6: v3=\"pass\"; break ; case 7: v3=\"C\"; break; case 8: v3=\"B\";
break; case 9: v3=\" A\"; break; case 10: v3=\"very good!\"; break; default: v3=\"error!\";
break; };
Console.WriteLine(\"this student's grade is {0}\",v3); Console.ReadKey(); } } }
7. 如何确定投资收益?
代码:namespace ConsoleApplication2
{ class Program
{ static void Main(string[] args)
{ int year=0;
double balance,targetbalance,interestRate;
Console.WriteLine(\"Welcome To This Procedure,Please enter your current balance:\"); balance= Convert.ToDouble(Console.ReadLine());
Console.WriteLine(\"What balance would you like to have?\"); targetbalance= Convert.ToDouble(Console.ReadLine());
Console.WriteLine(\"What's your current interest rate(in%)\"); interestRate = Convert.ToDouble(Console.ReadLine ())/100+1; do
{ balance *= interestRate;
++year; } while (balance < targetbalance);
Console.WriteLine(\"In {0} year{1} you will have a balance of
{2}\",year,year==1?\"\":\"s\",targetbalance );
Console.ReadKey();
} } }
8. 如何用C#画一个葫芦型图案: 代码:namespace ConsoleApplication4
{ class Program
{ static void Main(string[] args)
{ double realcoord, imgcoord,realtmp,realtmp2,arg,imgtmp; int iterations;
for (imgcoord = 1.2; imgcoord >= -1.2;imgcoord -=0.05)
{ for (realcoord =-0.6; realcoord <= 1.77;realcoord +=0.03) { iterations = 0; realtmp = realcoord; imgtmp = imgcoord; arg = (realcoord * realcoord)+ (imgcoord * imgcoord); while ((arg <4) && (iterations <40))
{ realtmp2 = (realtmp * realtmp) - (imgtmp * imgtmp)
- realcoord;
imgtmp = (2*realtmp * imgtmp ) - imgcoord; realtmp = realtmp2;
arg = (realtmp * realtmp) + (imgtmp * imgtmp); iterations +=1; }
switch (iterations%4)
{ case 0: Console.Write(\".\"); break ; case 1:
Console.Write(\"1\"); break; case 2:
Console.Write(\"0\"); break; case 3:
Console.Write(\"@\"); break; } }
Console.Write(\"\\n\"); }
Console.ReadKey(); } }
}
9. 变长数组的输出: 代码如下:
namespace ConsoleApplication9 { class Program
{ static void Main(string[] args) { double[][] height;//定义变长数组 height = new double[2][];
height[0]=new double[4]{4,2,3,5};//赋值给第一个子数组 height[1] = new double[3]{11,23,45};//赋值给第二个子数组 foreach (double[] h in height)//先确定数组范围 {foreach (double a in h)//子数组范围里 { Console.Write(\"{0} \",a);} } Console.ReadKey(); } } }
10. 如何将输入的字符串反序输出? 代码:namespace ConsoleApplication7
{ class Program
{ static void Main(string[] args) { string there; int i;
Console.WriteLine(\"Please Enter a string\");//请输入字符串 there = Console.ReadLine();//得到字符串 i = there.Length;
char[] mychars = there.ToCharArray(); int b;
#region //反序输出 for (b=i-1; b>=0; b--)
{ Console.Write(\"{0}\",mychars[b]); }
#endregion
Console.ReadKey(); } } }
11. 如何将字符串中的no换成yes?(replace函数) 代码:
namespace ConsoleApplication11 {
class Program {
static void Main(string[] args)
{ string userresponse = Console.ReadLine(); userresponse = userresponse.ToLower(); userresponse=userresponse.Replace(\"no\",\"yes\"); Console.WriteLine(userresponse); Console.ReadKey(); } }
}
12. 给字符串中的每一个单词加上双引号:
代码:
namespace ConsoleApplication11 {
class Program {
static void Main(string[] args)
{ string userresponse = Console.ReadLine(); userresponse = userresponse.ToUpper(); char a = ' '; string[] words;
words = userresponse.Split(a);//将字符串分解成单词,即变成字符串数组
foreach (string word in words)
{ Console.Write(\"“{0}”\",word); }//输出字符串中的单词 Console.ReadKey(); } } }