C#[編程實例]-編程入門試題
(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;
}
}
}
最後更新:2017-04-03 14:53:58