洗牌
//弄一副牌
string[] poker = new string[52];
for (int i = 0; i < 51; i++)
poker[i] = (i + 1).ToString();
//洗牌
Random r = new Random();
int t = 0;//抓r的數字
string tmp = "";
for (int i = 0; i < poker.Length; i++)
{
t = r.Next(52);
tmp = poker[i];
poker[i] = poker[t];
poker[t] = tmp;
}
//發牌
string p1 = "", p2 = "", p3 = "", p4 = "";
for (int i = 0; i < poker.Length; i++)
{
switch (i % 4) //餘數為0是p1 1是p2 2是p3 3是p4
{
case 0:
p1 += "<img src='poker_img/" + poker[i] + ".gif'/>";
break;
case 1:
p2 += "<img src='poker_img/" + poker[i] + ".gif'/>";
break;
case 2:
p3 += "<img src='poker_img/" + poker[i] + ".gif'/>";
break;
case 3:
p4 += "<img src='poker_img/" + poker[i] + ".gif'/>";
break;
}
}
Response.Write("p1:"+p1+"<br/>p2:"+p2+"<br/>p3:"+p3+"<br/>p4:"+p4);
請利用回圈寫一九九乘法表
//99乘法表第一種解法
//for(int i=1;i<=9;i++)//十位數字
//{
// string[] x = { "1", "2", "3", "4", "5", "6", "7", "8", "9", };
// foreach (string a in x)
// {
// Response.Write(i+"*"+a+"<br/>");
// }
//}
//第八題第二種解法
for (int i = 2; i <= 9; i++)
{
for (int z = 1; z <= 9; z++)
{
Response.Write(i + "*" + z + "=" + i * z + "<br/>");
}
}
- 請利用回圈顯示出下方圖形。(不可以使用巢狀回圈)
//string s = "";
//for(int x=1;x<=5;x++)
//{
// s = s + "*";
// Response.Write(s+"<br/>");
//}
計算1~1000中除了3倍數外所有數的總合。
int x = 1; //第六題作法二
int z = 0;
for (x = 1; x <= 10; x++)
{
if (x % 3 != 0)
{
z += x;
}
}
Response.Write(z + "<br/>");
寫一顯示1~100整數中,不是5的倍數的程式。
for (int x = 1; x <= 100; x++)
{
if (x % 5 != 0)
{
Response.Write(x + " ");
}
}
請利用switch敘述句,分別試寫判斷成績等第之程式。90分以上為優等,80~89為甲等,70~79為乙等,60~69為丙等,其餘為丁等(不可另外搭配if 敘述句)。
int score = 89;
switch (score / 10)
{
case 10:
case 9:
Response.Write("成績是優等");
break;
case 8:
Response.Write("成績是甲等");
break;
case 7:
Response.Write("成績是乙等");
break;
case 6:
Response.Write("成績是丙等");
break;
default:
Response.Write("不及格");
break;
}
質數判斷(必須用回圈)13,即顯示「13是質數」。(ps. 質數的定義為除了1與本身之外,沒有其他的因數存在)
第一題第二種解法
int x = 13;
bool flag = false;
for (int a = 2; a < x; a++)
{
if (x % a == 0)
{
flag = true;
break;
}
}
if (flag)
Response.Write(x + "不是質數");
else
Response.Write(x + "是質數");
求最大公因數(必須用回圈)12及18,即顯示「 12及18 之最大公因數為6」(ps. 最大公因數的定義為某幾個整數所共同擁有的最大因數)
第二題第一種解法
int a = 90, b = 60;//設定兩個自然數
for (int j = a + b; j >= 0; j--)
{
if (a % j == 0 && b % j == 0)
{
Response.Write(j + "是" + a + "跟" + b + "的最大公因數");
break;
}
}
第二題第二種解法,輾轉相除法
int x = 12, y = 16;
int xx = x, yy = y, z = 0; //xx永遠是被除數,yy永遠當除數,z放餘數
while (xx % yy != 0)
{
z = xx % yy;
xx = yy;
yy = z;
}
Response.Write(x + "與" + y + "的最大公因數為" + yy);
迴文判斷(必須用回圈)12321,即顯示「12321是迴文」。(ps.迴文的定義為一個數字,由左唸至右及由右唸至左時,皆一模一樣)
int k = 12321;
int r = 0, q = 0, kk = k;
string t = "";
for (int i = 0; i < 9; i++)
{
r = kk % 10;
q = kk / 10;
t += r;
if (q == 0)
break;
else
kk = q;
}
if(k.ToString()==t)
Response.Write(k + "是回文");
else
Response.Write(k + "不是回文");
留言列表