void main(){
int k=2,i=2,m;
m=(k+=i*=k);
Console.WriteLine(“{0},{1}”,m,i);
}
运行结果:6,4
void main(){
char grade='C';
switch(grade){
case 'A':
Console.WriteLine(">=85.");
break;
case 'B':
Console.WriteLine(">=70.");
break;
case 'C':
Console.WriteLine(">=60.");
break;
case 'D':
Console.WriteLine("<60.");
break;
default:
Console.WriteLine("error.");
break;
}
}
运行结果:>=60.
说明本程序的功能。
int num;
int sum=0;
int avg=0;
for(int i=1;i<=10;i++){
Console.WriteLine("请输入第{0}个整数",i);
string strTemp=Console.ReadLine();
num=int.Parse(strTemp);
sum=sum+num;
}
avg = sum / 10;
Console.WriteLine("平均数值={0}",avg);
答:求从键盘输入的10个整数的平均值
完善如下程序:打印输出平方值小于20000的最大整数。
int i=0;
while(_i*i_<20000){
i++;
}
Console.WriteLine(_i_);
下列程序的作用是求出所有的水仙花数。(所谓水仙花数是指这样的数:该数是三位数,其各位数字的立方和等于该数)
using system;
class Example1{
static void Main(string[] args){
int a, i, b, c, t;
for (i = 100; i <= 999; i++){
t = i;
a = t % 10;
t = t / 10;
b = t % 10;
c = t / 10;
if (i == a * a * a + b * b * b + c * c * c)
Console.WriteLine("i={0}", i);
}
Console.ReadLine();
}
}
public class TEApp{
public static void ThrowException(){
throw new Exception();
}
public static void Main(){
try{
Console.WriteLine("try");
ThrowException();
}
catch(Exception e){
Console.WriteLine("catch");
}
finally{
Console.WriteLine("finally");
}
}
}
运行结果是
try
catch
finally
public class Teacher{
public Teacher(){}
public Teacher (string name){
Console.WriteLine("老师的名字叫"+ name);
}
}
public class Test:Teacher{
public Test(string name){
Console.WriteLine("学生的名字叫"+ name);
}
}
static void Main(string[] args){
Test t=new Test("小明");
}
运行结果:学生的名字叫小明
class Test{
static void Main(){
try{
int a = 10;
int b = 0;
int c = a / b;
Console.WriteLine(c);
}
catch{
Console.WriteLine("出现错误");
}
finally{
Console.WriteLine("运行结束");
}
}
}
在C#中,上述代码的运行结果是
出现错误
运行结束
下列程序是输出100以内所有能被7整除的数,请将程序补充完整。
using System;
class Output{
static void Main(){
for (int k=1;k<=100;___k++___){
if (k%7!=0){
___continue;___
}
Console.Write(___" "+k___);
}
}
}
运行结果: 7 14 21 28 35 42 49 56 63 70 77 84 91 98
写出下列函数的功能。求出从键盘上输入的一批常数的平均值,以-1作为结束输入的标志。
class Output
{
static float FH()
{
float y = 0, n = 0;
int x = Convert.ToInt32(Console.ReadLine()); //从键盘读入整型数据赋给x
while (x != -1)
{
n++;
y += x;
x = Convert.ToInt32(Console.ReadLine());
}
if (n == 0)
{
return y;
}
else
{
return y / n;
}
}
static void Main(string[] args) {
float num = FH();
Console.WriteLine(num);
Console.ReadLine();
}
}