您的位置:首页 > 编程语言 > C#

C#[编程实例]-编程入门试题

2013-10-30 22:56 344 查看
(1)整除之和

int i = 1, j = 0;

while (i <=100)

{

if (i % 2 == 0 || i % 3 == 0)

{ j = j + i;

}

i++;

}

Console.WriteLine("1到100之间能被2或者3整除的整数的和为:{0}", j);

Console.ReadKey();

(2)输密码

int i = 1;

for (i = 1; i <= i; i++)

{

Console.WriteLine ("请输入用户名:");

string a=Convert .ToString (Console .ReadLine ());

Console .WriteLine("请输入密码:");

string b = Convert.ToString(Console.ReadLine());

if (a == "aaa" && b == "888888")

{ Console .WriteLine ("登陆成功!");

break;

}

else

{ Console.WriteLine("您的用户名或密码有误,请重新输入"); }

}

Console.ReadLine();

(3)智能算数

int c = 1;

Console.WriteLine("请输入第一个数:");

int i1 = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("请输入第二个数:");

int i2 = Convert.ToInt32(Console.ReadLine());

if (i1 > 0 && i2 > 0)

{

i1 = i1 - 1;

c = i1 + i2;

Console.WriteLine("这两个数都为正数,计算结果为:{0}", c);

Console.ReadLine();

}

if (i1 < 0 && i2 < 0)

{

i1 = i1 - 10;

c = i1 * i2;

Console.WriteLine("这两个数都为负数,计算结果为:{0}", c);

Console.ReadLine();

}

if (i1 * i2 == 0)

{

Console.WriteLine("您所输的数据有误!");

Console.ReadLine();

}

else

c = -(i1 * i2);

Console.WriteLine("这两个数异号,计算结果为:{0}", c);

Console .ReadLine ();

(4)课本上的题 P60,2

int[,] a = new int[3, 4] { { 1, 10, 100, 1000 }, { 2, 20, 200, 2000 }, { 3, 30, 300,3000 } };

for (int i = 0; i < a.GetLength (0); i++)

{

for (int j = 0; j < a.GetLength(1); j++)

{ Console.Write("{0} ",a[i, j]); }

Console .WriteLine ();

}

Console.Read();

(5)6行6个数

int i = 1;

for (i = 1; i <= 36; i++)

{

Console.Write("{0}", i);

if (i % 6 == 0)

Console.WriteLine();

(6)P28

using System;

using System.Collections.Generic;

using System.Text;

namespace ConsoleApplication10

{

using System;

struct rectangle

{

public int x, y;

public int width,height;

public rectangle(int a, int b, int w, int h)

{

x = a;

y = b;

width = w;

height = h;

}

}

class TestStruct

{

public static void Main()

{

//

rectangle myRect;

//

myRect.x = 20;

myRect.y = 30;

myRect.width = 200;

myRect.height = 300;

//

Console.WriteLine("My Rectange:");

Console.WriteLine("x = {0},y = {1}, width = {2}, heigh = {3}",myRect.x,myRect.y,myRect.width,myRect.height);

}

}

}

(7)矩形面积

class Program

{

static double tape(double L1, double W1)

{

double S;

S = L1 * W1;

Console.WriteLine("矩形的面积是{0}", S);

return S;

}

static void bijiao(double S1, double S2)

{

if (S1 - S2 > 0)

{ Console.WriteLine("矩形1的面积大于矩形2的面积"); }

if (S1 - S2 < 0)

{ Console.WriteLine("矩形1的面积小于矩形2的面积"); }

if (S1 - S2 == 0)

{ Console.WriteLine("矩形1的面积小于矩形2的面积"); }

}

static void Main(string[] args)

{

double s1, s2;

double a = 4, b = 5;

s1 = tape(a, b);

double c = 7, d = 3;

s2 = tape(c, d);

bijiao(s1, s2);

Console.Read();

}

}

}

(8) {

using System;

class TestOut

{

static int OutMultiValue(int a , out char b) //声明参数为输出参数

{

b = (char)a; return 0; //此方法为把字符型隐式转换为int类型

}

static void Main()

{

int t = 65 ;char m; //定义int类型变量t,r;char类型变量m

OutMultiValue(t ,out m); //调用方法

Console.WriteLine("m={0}" , m);

Console.ReadKey();

}

}

}

(9)比较两个矩形的大小

using System;

using System.Collections.Generic;

using System.Text;

namespace ConsoleApplication2

{

public class Rectangle

{

public double area;

public double Area()

{

Console.WriteLine("请输入矩形的长:");

double a = double.Parse(Console.ReadLine());

Console.WriteLine("请输入矩形的宽:");

double b = double.Parse(Console.ReadLine());

Console.WriteLine("矩形的长和宽为:{0}和{1}", a, b);

area = a * b;

Console.WriteLine("矩形的面积为:{0}",area );

return area;

}

public static void bijiao(double ss1, double ss2)

{

if (ss1 > ss2)

{ Console.WriteLine("s1的面积大于s2的面积"); }

else if (ss1 < ss2)

{ Console.WriteLine("s1的面积小于s2的面积"); }

else

Console.WriteLine("s1的面积等于s2的面积");

}

}

public class test

{

static void Main()

{

Rectangle s1 = new Rectangle();

Rectangle s2 = new Rectangle();

double areas1 = s1.Area();

double areas2 = s2.Area();

Rectangle.bijiao(areas1, areas2);

Console.Read();

}

}

}

P200 4,(2)

using System.IO;

namespace WindowsApplication1

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

SaveFileDialog sfdlg = new SaveFileDialog();

sfdlg.Filter = "所有文件*.*|*.*|文本文件*.txt|*.txt|C#文件|*.cs|C文件|*.c";

sfdlg.InitialDirectory = "c:\\user";//设置对话框显示的初始路径

sfdlg.Title = "保存文本文件";

sfdlg.FilterIndex = 2; //把文件过滤器设为第二项“*.txt”

//如果用户单击文件保存对话框的“保存”按钮,向文件写入“This is test”

if (sfdlg.ShowDialog() == DialogResult.OK)

{

FileStream fs = new FileStream(sfdlg.FileName, FileMode.OpenOrCreate, FileAccess.Write);

StreamWriter fileStream = new StreamWriter(fs);

fileStream.WriteLine(textBox1.Text);

fileStream.Close();

}

}

private void button2_Click(object sender, EventArgs e)

{

OpenFileDialog duqu = new OpenFileDialog();

duqu.Filter = "所有文件*.*|*.*|文本文件*.txt|*.txt|C#文件|*.cs|C文件|*.c";

duqu.InitialDirectory = "C:";

duqu.ShowDialog();

FileStream ff = new FileStream(duqu.FileName, FileMode.Open, FileAccess.Read);

StreamReader ffs = new StreamReader(ff);

label1.Text = ffs.ReadToEnd();

ffs.Close();

P200 4.(3)

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace WindowsApplication1

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)

{

OpenFileDialog duqu = new OpenFileDialog();

duqu.Filter = "所有文件*.*|*.*|文本文件*.txt|*.txt|C#文件|*.cs|C文件|*.c";

duqu.InitialDirectory = "C:";

duqu.ShowDialog();

label1.Text = duqu.FileName;

}

private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)

{

if (fontDialog1.ShowDialog() == DialogResult.OK)

fontDialog1.ShowColor = true;

label1.Font = fontDialog1.Font;

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: