2009年4月4日 星期六

入門C#----3.4習題

廢言
昨天一回到家
就開始清病毒
搞得我昨天沒把習題打完
居然連個手機有能當成病毒的傳媒阿囧
還搞得我的紅傘再次無法更新!!
就醬浪費了一整晚!!
不過利用了掃毒的時間看了掃地勇者還ok雖然純粹就是在亂搞

﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍
這章的習題有五題
5題基本上都算簡單
題目一
依據成績給予不同評語
程式碼


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

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int a;
while (true)
{
Console.Write("請輸入成績:");
a = int.Parse(Console.ReadLine());
switch (a/10) {
case 10:
case 9 :
Console.WriteLine("太強了");
break;
case 8:
Console.WriteLine("表現不錯");
break;
case 7:
Console.WriteLine("在加油,在加油。");
break;
case 6:
Console.WriteLine("剛好及格");
break;
default :
Console.WriteLine("準備被當吧!");
break;
}
}
}
}
}



題目2
印出99乘法表
程式碼


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

namespace ConsoleApplication1
{
class Program
{

static void Main(string[] args)
{
int a, b;
for (a = 1; a <= 9; a++) {
for (b = 1; b <= 9; b++) {
Console.WriteLine("{0} * {1} = {2}", a, b, a * b);
}
Console.WriteLine();
}
Console.ReadLine();

}
}
}



題目三
印出1-1000的質數
這個順便說明一下
原來沒有直接能判斷是不是質數的公式喔囧
害我還上網去找了一下說
另外我順便做了個進階版
顯示1~多少的質數並算出總共有幾個
程式碼


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

namespace ConsoleApplication1
{
class Program
{

static void Main(string[] args)
{
bool n;
int a, b,an;

Console.WriteLine("1 ~ ? 的質數");
while (true)
{
int t = 0;
int nu = 0;
Console.Write("請輸入最大值 ");
an = int.Parse(Console.ReadLine());
for (a = 2; a <= an; a++)
{
n = true;
for (b = 2; b <= an; b++)
{
if ((a % b) == 0 && a != b)
{
n = false;
break;
}
}
if (n)
{
nu += 1;
t += 1;
Console.Write(a + " ");
if (t == 10)
{
Console.WriteLine();
t = 0;
}
}
}
Console.WriteLine("\n共有{0}個質數", nu);
}
}
}
}




另外如果想加大範圍並加速的話
等到陣列可以另外在改
題目4
重複輸出指定的符號
程式碼


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

namespace ConsoleApplication1
{
class Program
{

static void Main(string[] args)
{
string a;
int b,t;
while (true)
{
Console.WriteLine();
Console.Write("請輸入欲顯示的符號: ");
a = Console.ReadLine();
Console.Write("請輸入重複的次數: ");
t = int.Parse(Console.ReadLine());
for (b = 1; b <= t; b++) Console.Write(a);
Console.WriteLine();
Console.Write("請按 Y 或 y 繼續 ? ");
a = Console.ReadLine();
if (a != "y" && a != "Y") break;

}
}
}
}



題目5
判斷是何種三角形?
順帶說一下
應該要讓a為最長邊
雖然題目啥都沒說囧
程式碼


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

namespace ConsoleApplication1
{
class Program
{

static void Main(string[] args)
{
int a, b, c,d;
while (true)
{
Console.WriteLine("輸入三角形的三個邊長 ");
Console.Write("A = "); a = int.Parse(Console.ReadLine());
Console.Write("B = "); b = int.Parse(Console.ReadLine());
if (a < b) { d = a; a = b; b = d; }
Console.Write("C = "); c = int.Parse(Console.ReadLine());
if (a < c) { d = a; a = c; c = d; }
d = b*b + c*c;
if (d == a * a)
Console.WriteLine("此為一直角三角形");
else if (a * a > d) Console.WriteLine("此為一鈍角三角形");
else Console.WriteLine("此為一銳角三角形");
Console.WriteLine();

}
}
}
}



﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍
話說
我好像把之前上傳的to神人影片
給忘的一乾二淨了
就順便貼在這啦

沒有留言:

張貼留言