一越平、編寫一個C#程序拌阴,實(shí)現(xiàn)輸出1~100中所有不能被7整除的數(shù),并求其和。
Console.WriteLine("1-100之間不能被整除的數(shù)據(jù)為:");
int [] a =new int [87];
int c =1;
int sum = 0;
for (int i = 1; i < 101; i++)
{
if (i % 7 != 0)
{
a[c] = i;
c++;
}
}
for (int b = 1; b < c; b++)
{
Console.Write(a[b] + "\t");
if (b % 4 == 0)
{
Console.WriteLine("");
}
}
for (int d = 0; d <c ; d++)
{
sum = sum + a[d];
}
Console.WriteLine("\n" + "數(shù)據(jù)之和為:{0}", sum);
Console.ReadKey();
二舟肉、
假設(shè)一個簡單的在ATM的取款過程如下:首先提示用戶輸入密碼(password)锰镀,最多只能輸入3次知纷,超過3次則提示用戶“密碼錯誤橘洞,請取卡”結(jié)束交易。如果用戶密碼正確办陷,再提示用戶輸入金額(amount),ATM只能輸出100元的紙幣貌夕,一次取線數(shù)要求最低0元,最高1000元民镜。如果用戶輸入的金額符合上述要求啡专,則打印輸入用戶取的錢數(shù),最后提示用戶“交易完成制圈,請取卡”们童,否則,提示用戶重新輸入金額鲸鹦。
假設(shè)用戶密碼111111慧库。
Console.WriteLine("請輸入密碼:");
string password = "111111";
int amount =0;
for (int a = 0; a < 3; a++)
{
string pass= Console.ReadLine();
if (pass != password)
{
Console.WriteLine("密碼錯誤,請重新輸入");
continue;
}
else
{
Console.WriteLine("請輸入金額:");
amount =Convert.ToInt32(Console.ReadLine());
break;
}
}
for (int a = 0; a < 3; a++)
{
if (amount / 100 == 0 | amount < 0 | amount > 1000)
{
Console.WriteLine("你輸入的金額不合法馋嗜,請重新輸入");
amount = Convert.ToInt32(Console.ReadLine());
continue;
}
else
{
Console.WriteLine("您取了{(lán)0}元", amount);
Console.WriteLine("交易完成齐板,請取卡");
break;
}
}
Console.ReadKey();