2017.8.7習題(進制)

1、風力預警系統(tǒng)有如下標準:普通風力小于5級,為藍色;大于5小于8,為黃色;大于8小于10為橙色;

大于10位紅色。如果風為龍卷風或臺風等特殊情況,大于8級即為紅色,實現(xiàn)上述判定標準,輸入風力級別,輸出預警顏色。

Console.WriteLine("請輸入風力大小1—12:");

string WindLevel = Console.ReadLine();? ? ? ? ?? ? ? ? ? ? //readline 為方法,返回值為string類型,所以用string接收

Console.WriteLine("當前是否為臺風或龍卷風,如果是請輸入1,否則輸入0:");

string isTyphoon = Console.ReadLine ();

//把string強轉(zhuǎn)為int

int WindLevel_num = int.Parse (WindLevel);

int isTyphhoon_num = int.Parse (isTyphoon);

if (WindLevel_num>0 && WindLevel_num <= 5) {

? ? ? ? ? ? ? ? ? ? ? ? Console.WriteLine ("藍色");

} else if (WindLevel_num > 5 && WindLevel_num <= 8) {

? ? ? ? ? ? ? ? ? ? ? ? Console.WriteLine ("黃色");

} else if (WindLevel_num > 8) {

? ? ? ? ? ? ? ? ? ? ? ? if (isTyphhoon_num == 1) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Console.WriteLine ("紅色");

} else {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Console.WriteLine ("橙色");

}

} else if (WindLevel_num > 10) {

? ? ? ? ? ? ? ? ? ? ? ? Console.WriteLine ("紅色");

} else {

? ? ? ? ? ? ? ? ? ? ? ? ?Console.WriteLine ("mdzz");

}


2、實現(xiàn)一個程序,控制臺實現(xiàn)如下效果,現(xiàn)在控制臺輸出如下語句。

請輸入編號選擇您想購買的商品:

1、筆? ? ? 2、作業(yè)本? ? 3、橡皮擦? ? 4、直尺

用戶輸入編號后打印出用戶選擇的商品

Console.WriteLine ("輸入你想要的編號:\n1、筆? ? ? 2、作業(yè)本? ? 3、橡皮擦? ? 4、直尺");

string Num = Console.ReadLine ( );

int Num_num = int.Parse (Num);

switch(Num_num){

case 1:

{

? ? ? ? ? Console.WriteLine ("筆");

? ? ? ? ? break;

}

case 2:

{

? ? ? ? ? ? ? Console.WriteLine ("作業(yè)本");

? ? ? ? ? ? ? break;

}

case 3:

{

? ? ? ? ? ? ? ? Console.WriteLine ("橡皮擦");

? ? ? ? ? ? ? ? break;

case 4:

{

? ? ? ? ? ? ? ? ?Console.WriteLine ("直尺");

? ? ? ? ? ? ? ? ?break;

}

default:

{

? ? ? ? ? ? ? ? ? ?Console.WriteLine("你是瓜皮嗎");

? ? ? ? ? ? ? ? ? ? break;

? ? ? ? ? ?}

}


3、實現(xiàn)在控制臺輸入數(shù)據(jù)n, 計算n并打印出n的階乘;

// n! = 1 *2 *3 *4 *…* (n-1) *n

Console.WriteLine ("請輸入一個數(shù),計算其階乘");

int n = int.Parse(Console.ReadLine());

Int64 sum = 1;

if (n == 0) {

? ? ? ?sum = 0;

} else {

? ? ? ?for (Int64 i = 1; i <= n; ++i) {

? ? ? ? ? ? ? ? ? ? ?sum *= i;

? ? ? ? ? }

}

Console.WriteLine ("{0}!={1}",n,sum);


4、從控制臺輸入一個年份,判斷是否為閏年(閏年的條件:1、年份能被4整除? 2、年份若是100的整數(shù)倍的話,需被400整除)

Console.WriteLine("輸入一個年份");

string Year = Console.ReadLine ( );

int Year_num = int.Parse (Year);

if ((Year_num % 4 == 0 && Year_num % 100 != 0) || Year_num % 400 == 0) {

? ? ? ? ? ? ? ?Console.WriteLine ("{0}年是閏年");

} else {

? ? ? ? ? ? ? ? Console.WriteLine ("你是個假人");

}


5、連續(xù)輸入一周七天的溫度,同時求出該周平均溫度。

int sum = 0;

for(int i = 0;i<7;++i){

? ? ? ? ? ? ? ? Console.WriteLine ("請輸入星期{0}的溫度",i+1);

? ? ? ? ? ? ? ? string temp = Console.ReadLine ( );

? ? ? ? ? ? ? ? ?int temp_num = int.Parse (temp);

? ? ? ? ? ? ? ? ?sum += temp_num;

}

Console.WriteLine ("七天的氣溫平均值為{0}",sum/7.0f);


6、猴子第一天摘下若干個桃子,當即吃了一半,還不過癮,又多吃了一個,第二天早上又將剩下的桃子吃掉一半,

又多吃了一個。以后每天早上都吃了前一天剩下的一半零一個。到第10天早上想再吃時,見只剩下一個桃子了。

求第一天共摘了多少。

int x = 1; ? ? ? ? ? ? ?//第十天剩余的桃子數(shù)量

for (int i = 0; i < 9; ++i) {

? ? ? ? ? x = (x+1)*2;

}

Console.WriteLine ("第一天的桃子數(shù)量為{0}",x);



7、分析下列語句中a、b的結(jié)果

int a = 1, b = 3;

a += b %= 2;

//a = 2,b = 1


int a = 1,b = 4;

a = ++b/a++;

//a = 5,b= 5


8、如何用代碼將以下數(shù)據(jù)轉(zhuǎn)換成二進制和十六進制,1000和2049。

int x = 1000;

//十進制轉(zhuǎn)二進制 ?結(jié)果倒著讀

while (x>0) {

? ? ? ? ? ? ? int n = x % 2;

? ? ? ? ? ? ? Console.WriteLine ("{0}",n);

? ? ? ? ? ? ? x /= 2;

}


//數(shù)組改

int x = 1000;

int[] binary = new int[16];

int index = 0;

while (x>0) {

? ? ? ? ? ? ? ? int n = x % 2;

? ? ? ? ? ? ? ? binary [index] = n;

? ? ? ? ? ? ? ? ++index;

? ? ? ? ? ? ? ? ?x/= 2;

}

for (int i = binary.Length-1; i >= 0; --i) {

? ? ? ? ? ? ? ? ?Console.Write ("{0}",binary[i]);

}


int x = 2049;

//十進制轉(zhuǎn)十六進制

while (x>0) {

? ? ? ? ? ? ? ? ? ?int n = x % 16;

if (n >= 10) {

? ? ? ? ? ? ? ? ? ?Console.WriteLine ("{0}",(char)(n+55)); ? ? ? ? ? ? //轉(zhuǎn)字符ABC ?ACLL碼

} else {

? ? ? ? ? ? ? ? ? ?Console.WriteLine ("{0}",n);

}

? ? ? ? ? ? ? ? ? ? x /= 16;

}


數(shù)組改

int x = 2049;

int[] hexadecimal = new int[8];

int index = 0; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //下標

while (x>0) {

? ? ? ? ? ? ? ? ? ? ? int n = x % 16;

? ? ? ? ? ? ? ? ? ? ? hexadecimal[index] = n; ? ? ? ? ? ? ? ? ? ? ? ? //n替換index位置的數(shù)值

? ? ? ? ? ? ? ? ? ? ? ++index;

? ? ? ? ? ? ? ? ? ? ? x /= 16;

}

for (int i = hexadecimal.Length-1; i >=0; --i) {

? ? ? ? ? ? ? ? ? ? ? ?int n = hexadecimal [i];

if (n >= 10) {

? ? ? ? ? ? ? ? ? ? ? ?Console.Write ("{0}", (char)(n + 55));

} else {

? ? ? ? ? ? ? ? ? ? ? ?Console.Write("{0}",n);

? ? ? ? ? ? }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容