|
关于书上90页的练习题(请教,有2个问题都在里面的注释)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 习题6_11
{
class class1
{
static void Main(string[] args)
{
Console.WriteLine("(2) if语句练习:");
IfStatement(-2);
Console.WriteLine("(3) switch语句练习:");
SwitchStatement(-3);
Console.WriteLine("(4) for语句练习:");
ForStatement(5);
Console.WriteLine("(5) foreach语句练习:");
ForeachStatement( ); //这里怎么传数组进去呢?格式是怎样的?
Console.WriteLine("(6) while语句练习:");
WhileStatement(0);
Console.WriteLine("(7) do while语句练习:");
DoStatement(0);
Console.WriteLine("(8) try语句练习:");
TryStatement(3);
Console.Read();
}
static void IfStatement(int a)
{
if (a > 0)
{
Console.WriteLine("正数");
}
else
{
Console.WriteLine("非正数");
}
}
static void SwitchStatement(int b)
{
switch (b)
{
case 1:
Console.WriteLine("case为1"); break;
case 2:
Console.WriteLine("case为2"); break;
case 3:
Console.WriteLine("case为3"); break;
default:
Console.WriteLine("除1,2,3已外的数"); break;
}
}
static void ForStatement(int c)
{
int sum = 0;
for(c = 0;c <= 100;c++)
{
sum = c + sum;
}
Console.WriteLine(sum.ToString());
}
static void ForeachStatement(int[] d)
{
int sum = 0;
foreach (int i in d)
{
sum = sum + i;
}
Console.WriteLine(sum.ToString());
}
static void WhileStatement(int e)
{
int sum = 0;
while (e <= 100)
{
sum = e + sum;
e++;
}
Console.WriteLine(sum.ToString());
}
static void DoStatement(int f)
{
int sum = 0;
do
{
sum = f + sum;
f++;
} while (f <= 100);
Console.WriteLine(sum.ToString());
}
static void TryStatement(int g)
{
int k = 0;
try
{
int sum = g / k;
}
catch
{
Console.WriteLine("被除数不能为0");
}
finally
{
Console.WriteLine("计算结果为:" + sum.ToString()); //为什么这里用sum会出错呢?要怎么改才能用到sum呢?
}
}
}
} |
在这个辽阔的世界面前,可笑的梦想将成为你们前进的方向。 |
|