99网
您的当前位置:首页C#代码

C#代码

来源:99网
C#代码

1. 第一个程序,helloworld? 代码相当简单,见下:

Console.WriteLine(\"HelloWorld\");

2. 如何进行与运算和或运算? 答:代码见下:

namespace ConsoleApplication1 {

class Program {

static void Main(string[] args) {

string name; int op1, op2;

Console.WriteLine(\"Welcome To This Procedure,Please enter your name \"); name = Convert.ToString(Console.ReadLine()); Console.WriteLine(\"Please give a number to op1\"); op1 = Convert.ToInt32(Console.ReadLine());

Console.WriteLine(\"Please give another number to op2\"); op2 = Convert.ToInt32(Console.ReadLine());

Console.WriteLine(\"Hello \\\"{0}\\\, name); Console.WriteLine(\"The result of op1&op2 is {0}\",op1&op2 ); Console.WriteLine(\"The result of op1|op2 is {0}\",op1|op2); Console.ReadKey(); } }

3. 如何收缩代码?

#region Using directives using System;

using System.Collections.Generic; using System.Linq; using System.Text; #endregion

4. 如何给一个数移动位数,使之变大2n倍或减少2n倍?

其代码是:

using System;

using System.Collections.Generic; using System.Linq; using System.Text;

namespace ConsoleApplication6 {

class Program {

static void Main(string[] args) { int var1, var2;

Console.WriteLine(\"Please enter the first number: \");//输入的原始数据 var1 = Convert.ToInt32(Console.ReadLine());

Console.WriteLine(\"Please enter the second number: \");//移动的位数 var2 = Convert.ToInt32(Console.ReadLine ());

Console.WriteLine(\"向左移动{0}位的结果是{1}\",var2,var1<>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(); } } }

因篇幅问题不能全部显示,请点此查看更多更全内容