标题:
关于书上90页的练习题(请教,有2个问题都在里面的注释)
[打印本页]
作者:
yqxmoon
时间:
2009-11-26 23:01
标题:
关于书上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呢?
}
}
}
}
作者:
dorient
时间:
2009-11-28 15:40
int[] array = new int[10]; ///还需要赋值
ForeachStatement(array);
就可以传数组了
作者:
dorient
时间:
2009-11-28 15:41
try
{
int sum = g / k;
}
应该为:
int sum;
try
{
sum = g / k;
}
作者:
yqxmoon
时间:
2009-11-28 22:47
非常感谢!! :victory:
欢迎光临 万卷图书 (http://wanjuanchina.net/)
Powered by Discuz! 7.0.0