1.编写一段程序,运行时向用户提问“你考了多少分?(0~100)”,接受输入后判断其等级并显示出来。
判断依据如下:等级={优 (90~100分);良 (80~89分);中 (60~69分);差 (0~59分);}
static void Main(string[] args)
{
Console.WriteLine("你考了多少分?(1~100)");
int a = int.Parse(Console.ReadLine());
switch (a / 10)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
Console.WriteLine("等级为差");
break;
case 6:
Console.WriteLine("等级为中");
break;
case 7:
case 8:
Console.WriteLine("等级为良");
break;
case 9:
case 10:
Console.WriteLine("等级为优");
break;
default:
Console.WriteLine("输入有误,请输入0-100的数。");
break;
}
Console.ReadLine();
}
1.编写一个控制台程序,分别输出1~100之间整数的平方、平方根、自然对数(log)。
static void Main(string[] args)
{
{
for (int i = 1; i < 101; i++)
{
Console.Write("{0}的平方是:{1}\t", i, i * i);
Console.Write("{0}的平方根是:{1}\t", i, Math.Sqrt(i));
Console.WriteLine("以e为底{0}的对数值是:{1}\t", i, Math.Log(i));
}
}
}
3.编写一个控制台应用程序,完成下列功能,并写出运行程序后输出的结果。
1)创建一个类A,在A中编写一个可以被重写的带int类型参数的方法MyMethod,并在该方法中输出传递的整型值后加10后的结果。
public class A
{
public virtual void MyMethod(int num)
{
num += 10;
Console.WriteLine(num);
}
}
2)再创建一个类B,使其继承自类A,然后重写A中的MyMethod方法,将A中接收的整型值加50,并输出结果。
public class B : A
{
public override void MyMethod(int num)
{
num += 50;
Console.WriteLine(num);
}
}
3)在Main方法中分别创建A和类B的对象,并分别调用MyMethod方法。
class Program
{
static void Main(string[] args)
{
A newa = new A();
newa.MyMethod(2);
B newb = new B();
newb.MyMethod(2);
Console.ReadLine();
}
}
4.编写出一个通用的人员类(Person),该类具有姓名(Name)、年龄(Age)、性别(Sex)。然后对Person 类的继承得到一个学生类(Student),该类能够存放学生的5门课的成绩,并能求出平均成绩,要求对该类的构造函数进行重载,至少给出三个形式。实例化student类,传值并计算平均值。
class Program
{
static void Main(string[] args)
{
Student s = new Student("张三", 22, "男", 78.6, 77, 93.2, 95.3, 88.9);
Console.WriteLine("平均成绩:" + s.Average());
}
}
//person类
public class Person
{
//域表示与类或对象相关联的变量,注释部分可剩
string name, sex;
int age;
public Person() { }
public Person(string name, int age, string sex)
{
this.name = name;
this.age = age;
this.sex = sex;
}
}
//student类
public class Student : Person
{
double english, chinese, mathematics, music, history;
public Student() { }
public Student(string name, int age, string sex)
: base(name, age, sex)
{
this.english = 0;
this.chinese = 0;
this.mathematics = 0;
this.music = 0;
this.history = 0;
}
public Student(string name, int age, string sex, double english, double chinese, double mathematics, double music, double history)
: base(name, age, sex)
{
this.english = english;
this.chinese = chinese;
this.mathematics = mathematics;
this.music = music;
this.history = history;
}
public double Average()
{
return (english + chinese + mathematics + music + history) / 5;
}
}
5.编程判断输入的一个正整数是否既是5的倍数又是7的倍数,若是则输出yes,若不是则输出no
static void Main(string[] args)
{
Console.WriteLine("请输入一个正整数:");
int cInt = int.Parse(Console.ReadLine());
if (cInt % 5 == 0 && cInt % 7 == 0)
{
Console.WriteLine("yes");
}
else
{
Console.WriteLine("no");
}
}