传智播客C#练习答案

更新时间:2023-04-06 02:13:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

class HelloWorld

{

public void Print() //类中方法,定义了名称为Print()的方法。

{

Console.WriteLine("Hello,World");//调用Console.WriteLine()方法

}

static void Main(string[] args) //定义Main()方法

{ /*对象是具体的事物,是类的实例。定义一个类之后,

就可以创建该类的实例。通过对象调用类的实例字段和方法。*/

HelloWorld helloworld = new HelloWorld(); //创建对象,定义一个HelloWorld类的对象helloworld helloworld.Print(); //通过对象来调用方法,使用对象(helloworld)调用方法Print()

Console.ReadKey();

}

}

/*转义符*/

class program

{

static void Main(string[] args)

{

string s = "\"ab\""; //输出"ab"

Console.WriteLine(s);

string ss = @"\\\\"; //@表示字符串中的\不当成转义符,注意@只对转义符\有意义

Console.WriteLine(ss);

Console.ReadKey();

}

}

/*类型转换*/

class program

{

static void Main(string[] args)

{

Console.WriteLine("请输入第一个数字?");

string s1 = Console.ReadLine();

int i1=Convert.ToInt32(s1);

Console.WriteLine("请输入第二个数字?");

string s2 = Console.ReadLine();

int i2 = Convert.ToInt32(s2);

Console.WriteLine("{0}+{1}={2}", i1, i2, i1 + i2);

Console.ReadKey();

}

}

class program

{

static void Main(string[] args)

{

Console.WriteLine("请输入第一个数字?");

int i1 = Convert.ToInt32(Console.ReadLine());

int i2 = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("{0}+{1}={2}", i1, i2, i1 + i2);

Console.ReadKey();

}

}

/*赋值*/

class program

{

static void Main(string[] args)

{

int i = 10;

i = i + 1;

Console.WriteLine(i);

i = i + i;

Console.WriteLine(i);

Console.ReadKey();

}

}

/*交换变量*/

class program

{

static void Main(string[] args)

{

int i1 = 10;

int i2 = 20;

Console.WriteLine("i1={0},i2={1}", i1, i2);

int i3;

i3 = i1;

i1 = i2;

i2 = i3;

Console.WriteLine("i1={0},i2={1}", i1, i2);

Console.ReadKey();

}

}

/*if语句*/

class program

{

static void Main(string[] args)

{

Console.WriteLine("请输入你的年龄?");

string s = Console.ReadLine();

int age = Convert.ToInt32(s);

if (age > 20)

{

Console.WriteLine("成年人");

}

else if (age > 10)

{

Console.WriteLine("儿童");

}

else

{

Console.WriteLine("婴幼儿");

}

Console.ReadKey();

}

}

class program

{

static void Main(string[] args)

{

Console.WriteLine("请输入密码?");

string password = Console.ReadLine();

if (password == "888888")

{

Console.WriteLine("密码正确!");

}

else

{

Console.WriteLine("密码错误,请重新输入?");

password=Console.ReadLine();

if (password == "888888")

{

Console.WriteLine("密码正确!");

}

else

{

Console.WriteLine("密码错误!");

}

}

Console.ReadKey();

}

}

class program

{

static void Main(string[] args)

{

Console.WriteLine("请输入您的数学成绩");

int math = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("请输入您的语文成绩");

int chinese = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("您的总成绩为:{0}分", math + chinese);

Console.ReadKey();

}

}

/*布尔类型*/

class program

{

static void Main(string[] args)

{

int i = 30;

Console.WriteLine("{0}", i == 1);

Console.WriteLine("{0}", i = 1);

Console.ReadKey();

}

}

class program

{

static void Main(string[] args)

{

int age = 20;

int wight = 120;

bool result = age >= 18 && wight >= 100;

Console.WriteLine(result);

Console.ReadKey();

}

}

class program

{

static void Main(string[] args)

{

Console.WriteLine("************************************");

Console.WriteLine("* 欢迎使用本软件*");

Console.WriteLine("************************************");

Console.ReadKey();

}

}

/*定义3个变量,分别存储一个人的姓名(张三),性别(男),年龄(28),电话号码(0555-*******)和工资(7600.33).

然后在屏幕上显示,我叫XX,性别是X,今天X岁了,电话号码是X,我的工资是XX元*/

class program

{

static void Main(string[] args)

{

string name = "张三";

char sex = '男';

int age = 28;

age = 30;

string phone = "0555-*******";

decimal salary = 7600.33m;

Console.WriteLine("我叫{0};\n性别是{1};\n今年{2}岁了;\n电话号码是{3};\n我的工资是{4}元。",

name, sex, age, phone, salary);

Console.ReadKey();

}

}

/*编程实现计算几天(如46天)是几周零几天. */

class program

{

static void Main(string[] args)

{

try

{

Console.WriteLine("请输入天数");

int Day = Convert.ToInt32(Console.ReadLine());

int week = Day / 7;

int day = Day % 7;

Console.WriteLine("{0}天是{1}周{2}天", Day, week, day);

}

catch

{

Console.WriteLine("请按正确格式输入");

}

Console.ReadKey();

}

}

/*.定义两个变量如:a b分别赋值为10和5,写程序交换两个变量的值*/

class program

{

static void Main(string[] args)

{

//int a = 10;

//int b = 5;

//int temp;

//temp = a;

//a = b;

//b = temp;

//Console.WriteLine("a={0}\nb={1}", a, b);

//Console.ReadKey();

}

}

/*1.练习:问用户喜欢吃什么水果,假如用户输入”苹果”,则显示”哈哈,这么巧呀,我也喜欢吃苹果”*/ class program

{

static void Main(string[] args)

{

Console.WriteLine("请输入你喜欢吃的水果?");

string input = Console.ReadLine();

Console.WriteLine("这么巧呀,我也喜欢吃{0}。",input );

Console.ReadKey();

}

}

/*2.练习:请用户输入姓名,然后在屏幕上显示“你好,XXX". XXX为用户刚刚输入的姓名.*/

class program

{

static void Main(string[] args)

{

Console.WriteLine("请问你叫什么名字?");

string name = Console.ReadLine();

Console.WriteLine("你好,{0}。", name);

Console.ReadKey();

}

}

/*转义字符*/

class program

{

static void Main(string[] args)

{

Console.WriteLine("C#中的字符要用\"\"号引起来。");

char ch = '\"'; //传义字符是一个char类型

Console.WriteLine(ch);

Console.WriteLine("1\t2\t3\t4\t5\t");//\t具有对齐功能

Console.WriteLine("11\t231\t6789\t9");

Console.WriteLine("\\\\");

Console.WriteLine(@"""""");//两个""等于一个"

Console.WriteLine(@"http://business.\tsohu.\ncom/");//@放在前面表示不转义

Console.ReadKey();

}

}

/*算术运算符*/

class program

{

static void Main(string[] args)

{

int chinese = 90;

int math = 80;

int english = 91;

int sum = chinese + math + english;

int avg = sum / 3;

Console.WriteLine("你的总成绩为{0},平均成绩为{1}", sum, avg);

Console.ReadKey();

double pi = 3.14;

double r = 5; //参与运算的操作数类型必须一致(即相同),这里不要定义int r = 5

double s = pi * r * r;

Console.WriteLine("s = {0}",s);

Console.ReadKey();

}

}

/*:某商店T恤的价格为35元/件,裤子的价格为120元/条.小明在该店买了3件T恤和2条裤子,请计算并显示小明应该付多少钱?假如商店为小明打8.8折,那么小明应付多少钱呢?*/ class program

{

static void Main(string[] args)

{

double tshirtPrice = 35;

double trousersPrice = 120;

double totalMoney = 3 * tshirtPrice + 2 * trousersPrice;

double disMoney = totalMoney * 0.88;

Console.WriteLine("购物总计:{0}元,打8.8折后应付:{1}元。",totalMoney,disMoney);

Console.ReadKey();

}

}

class program

{

static void Main(string[] args)

{

int a = 10;

int b = 3;

int mod = a % b;

//int quo = a / b;

//double quo = 1.0 * a / b; //乘以1.0后,隐式转换成double类型

double quo = (double)a / b; //强制类型转换

Console.WriteLine(mod);

Console.WriteLine(quo);

// Console.WriteLine("a % b = {0}, a / b = {1}", mod, quo);

Console.ReadKey();

}

}

/*让用户输入他的语文和数学成绩,计算他的总成绩并显示出来?*/

class program

{

static void Main(string[] args)

{

Console.WriteLine("请输入你的语文成绩?");

//string chinese = Console.ReadLine();

//int chineseScore=Convert.ToInt32(chinese);

/*把输入的数字字符串转换成int类型,

如果输入的是字母,则不能转换*/

int chinese = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("请输入你的数学成绩?");

//string math = Console.ReadLine();

//int mathScore=Convert.ToInt32(math);

int math = Convert.ToInt32(Console.ReadLine());

//Console.WriteLine("你的总成绩为:{0}", chineseScore + mathScore);

Console.WriteLine("你的总成绩为:{0}", chinese + math);

Console.ReadKey();

}

}

/*让学生输入其姓名和语文\数学\英语,编程求总分和平均分,并在屏幕上显示:××你的总分数为xx分,平均为××分.*/

class program

{

static void Main(string[] args)

{

try

{

Console.WriteLine("请输入你的姓名?");

string name = Console.ReadLine();

Console.WriteLine("请输入你的语文成绩?");

int chinese = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("请输入你的数学成绩?");

int math = Convert.ToInt32(Console.ReadLine())

;

Console.WriteLine("请输入你的英语成绩?");

int english = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("{0}的三科成绩总分数为{1},平均分为{2}.",

name, chinese + math + english, (chinese + math + english)/3);

}

catch

{

Console.WriteLine("你刚刚输入的数据有误,程序无法运行,请重新运行本程序!");

}

Console.ReadKey();

}

}

/*编程实现计算几天(如46天)是几周零几天. */

class program

{

static void Main(string[] args)

{

try

{

Console.WriteLine("请输入天数");

int days = Convert.ToInt32(Console.ReadLine());

int week = days / 7;

int mod = days % 7;

Console.WriteLine("{0}天中共有{1}周零{2}天", days, week, mod);

}

catch

{

Console.WriteLine("请按正确格式输入");

}

Console.ReadKey();

}

}

/*编程实现107653秒是几天几小时几分钟几秒? */

class program

{

static void Main(string[] args)

{

Console.WriteLine("请输入你要计算的秒数?");

int second = Convert.ToInt32(Console.ReadLine());

//int second = 107653;

int day = second / (60 * 60 * 24);

int mod = second % (60 * 60 * 24);

int hour = mod / (60 * 60);

mod = mod % (60*60);

int min = mod / 60;

mod = mod % 60;

Console.WriteLine("在{0}秒中,有{1}天{2}小时{3}分钟{4}秒", second, day, hour, min, mod);

Console.ReadKey();

}

}

/*前++与后++的区别*/

class program

{

static void Main(string[] args)

{

int age = 18;

int sum = age++ - 10; //后++,取原值参与运算,然后自增

//int sum = ++age - 10; //前++,先自增,然后取新值参与运算

Console.WriteLine("age = {0}\nsum ={1}", age, sum);

Console.ReadKey();

}

}

class program

{

static void Main(string[] args)

{

//int var1;

//int var2 = 5;

//int var3 = 6;

//var1 = var2++ * --var3;

//Console.WriteLine("var1={0},var2={1},var3={2}", var1, var2, var3);

int var1;

int var2 = 5;

int var3 = 6;

var1 = ++var2 * var3--;

Console.WriteLine("var1={0},var2={1},var3={2}", var1, var2, var3);

Console.ReadKey();

}

}

/*bool类型*/

class program

{

static void Main(string[] args)

{

//int zsAge = 18;

//int lsAge = 20;

//bool isRight = zsAge > lsAge; //用< 、== 、!= 来试试

//Console.WriteLine(isRight);

//Console.ReadKey();

int age = 20;

int weight = 120;

bool result = age >= 18 && weight >= 100;

Console.WriteLine(result);

Console.ReadKey();

}

}

/*让用户输出张三的语文和数学成绩,输出以下判断是否正确,正确输出true,错误输出false

1)张三的语文和数学成绩都大于90分

2)语文和数学有一门是大于90分的*/

class program

{

static void Main(string[] args)

{

Console.WriteLine("请输入你的语文成绩?");

int chinese = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("请输入你的数学成绩?");

int math = Convert.ToInt32(Console.ReadLine());

/*张三的语文和数学成绩都大于90分*/

bool first = chinese > 90 && math > 90;

/*语文和数学至少一门是大于90分的*/

bool sec = chinese > 90 || math > 90;

Console.WriteLine("\"张三的语文和数学成绩都大于90分\"这句话的结果为{0}", first);

Console.WriteLine("\"语文和数学至少一门是大于90分的\"这句话的结果为{0}", sec);

Console.ReadKey();

}

}

/*写下判断闰年的表达式,设待判断的年份变量为year. 润年的判定(符合下面两个条件之一): 年份能够被400整除.(2000) 年份能够被4整除但不能被100整除.(2008) 让用户输入一个年份,如果是润年,则输出true,如果不是,则输出false. */

class program

{

static void Main(string[] args)

{

Console.WriteLine("请输入一个年份?");

int year = Convert.ToInt32(Console.ReadLine());

bool result = (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0));

Console.WriteLine(result);

Console.ReadKey();

}

}

/*逻辑与、逻辑或的短路问题*/

class program

{

static void Main(string[] args)

{

int a = 10;

int b = 5;

//bool result = ++a > 50 && ++b > 1;//第一个表达式已经不成立(false),第二个表达式就不参与运算

bool result = ++a > 5 || ++b > 1;

Console.WriteLine("a={0},b={1}", a, b);//观察b的值

Console.ReadKey();

}

}

class program

{

static void Main(string[] args)

{

int chinese = 95;

int math = 80;

//bool result = (chinese > 90 && math > 90);

bool result = (chinese > 90 || math > 90);

Console.WriteLine(result);

Console.ReadKey();

}

}

class program

{

static void Main(string[] args)

{

int a = 3;

int b = 10;

Console.WriteLine(a.ToString() + b);//在这里+号是连接的意思

Console.ReadKey();

}

}

/*编程实现:如果张三的考试成绩大于90分,那么爸爸奖励他100元*/

class program

{

static void Main(string[] args)

{

Console.WriteLine("请输入张三的考试成绩?");

int zsScore = Convert.ToInt32(Console.ReadLine());

if (zsScore > 90)

{

Console.WriteLine("爸爸奖励他100元");

}

Console.ReadKey();

}

}

/*让用户输入年龄,如果输入的年龄大于18(含)岁,则给用户显示你已成年.*/

class program

{

static void Main(string[] args)

{

Console.WriteLine("请输入你的年龄?");

int age = Convert.ToInt32(Console.ReadLine());

if (age>=18)

{

Console.WriteLine("你已成年!");

}

Console.ReadKey();

}

}

/*如果张三的语文成绩大于90并且音乐成绩大于80 语文成绩等于100并且音乐成绩大于70,则奖励100元.*/

class program

{

static void Main(string[] args)

{

Console.WriteLine("请输入你的语文成绩?");

int chinese = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("请输入你的音乐成绩?");

int music = Convert.ToInt32(Console.ReadLine());

if((chinese > 90 && music > 80)||(chinese ==100 && music > 70))

{

Console.WriteLine("奖励100元,买糖吃去吧!");

}

Console.ReadKey();

}

}

/*让用户输入用户名和密码,如果用户名为admin,密码为mypass,则提示登录成功. */

class program

{

static void Main(string[] args)

{

Console.WriteLine("请输入你的用户名?");

string userName = Console.ReadLine();

Console.WriteLine("请输入你的密码?");

string password = Console.ReadLine();

if ((userName == "admin") && (password == "mypass"))

{

Console.WriteLine("登录成功!");

}

else

{

Console.WriteLine("用户名或密码错误!");

}

Console.ReadKey();

}

}

/*编程实现:如果张三的考试成绩大于90(含)分,那么爸爸奖励他100元,

否则的话,爸爸就让张三写学习总结*/

class program

{

static void Main(string[] args)

{

Console.WriteLine("请输入张三的考试成绩?");

int zsScore = Convert.ToInt32(Console.ReadLine());

if (zsScore >= 90)

{

Console.WriteLine("爸爸奖励他100元");

}

else

{

Console.WriteLine("不准出去玩,写总结去吧!");

}

Console.ReadKey();

}

}

/*盖茨买了一筐鸡蛋,如果坏蛋少于5个,他就吃掉,否则他就去退货*/

class program

{

static void Main(string[] args)

{

Console.WriteLine("请输入坏蛋的个数?");

int badEgg = Convert.ToInt32(Console.ReadLine());

if (badEgg < 5)

{

Console.WriteLine("吃掉吧!");

}

else

{

Console.WriteLine("退货!");

}

Console.ReadKey();

}

}

/*要求用户输入两个数a、b,如果a能被b整除或者a加b大于100,则输出a的值,否则输出b的值*/ class program

{

static void Main(string[] args)

{

Console.WriteLine("请输入一个数(a)?");

int a = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("请再输入一个数(b)?");

int b = Convert.ToInt32(Console.ReadLine());

if (a % b == 0 || a + b > 100)

{

Console.WriteLine("a = {0}", a);

}

else

{

Console.WriteLine("b = {0}", b);

}

Console.ReadKey();

}

}

/*对学员的结业考试成绩评测(考虑用if好还是用if-else好)

成绩>=90 :A

90>成绩>=80 :B

80>成绩>=70 :C

70>成绩>=60 :D

成绩<60 :E */

class program

{

static void Main(string[] args)

{

Console.WriteLine("请输入你的成绩: ");

int score = Convert.ToInt32(Console.ReadLine());

if (score >= 90)

{

Console.WriteLine("A");

}

else if (score >= 80)

{

Console.WriteLine("b");

}

else if (score >= 70)

{

Console.WriteLine("C");

}

else if (score >= 60)

{

Console.WriteLine("D");

}

else//这个else是可以省略的

{

Console.WriteLine("E");

}

Console.WriteLine("\n\n回车结束");

Console.ReadKey();

}

}

/*提示用户输入密码,如果密码是“888888”提示正确,否则要求再输入一次,如果密码是“888888”则提示正确,否则提示错误,程序结束*/

class program

{

static void Main(string[] args)

{

Console.WriteLine("请输入密码?");

string password = Console.ReadLine();

if (password == "888888")

{

Console.WriteLine("密码正确!");

}

else

{

Console.WriteLine("你刚刚输入的密码有误,请重新输入?");

password=Console.ReadLine();

if (password == "888888")

{

Console.WriteLine("密码正确!");

}

else

{

Console.WriteLine("密码错误!");

}

}

Console.ReadKey();

}

}

/*提示用户输入用户名,然后再输入密码,如果用户名是“admin”并且密码是“888888”,

则提示正确,否则,如果用户名不是“admin”,则提示用户用户名不存在,如果用户名是“admin”则提示密码错误*/

class program

{

static void Main(string[] args)

{

Console.WriteLine("请输入用户名?");

string userName = Console.ReadLine();

Console.WriteLine("请输入密码?");

string password = Console.ReadLine();

if (userName == "admin" && password == "888888")

{

Console.WriteLine("恭喜你,登陆成功!");

}

else if (userName != "admin")

{

Console.WriteLine("用户名不存在!");

}

else

{

Console.WriteLine("密码错误!");

}

Console.ReadKey();

}

}

/*提示用户输入年龄,如果大于等于18,则告知用户可以查看,如果小于10岁,则告知不允许查看,如果大于等于10岁并且小于18,则提示用户是否继续查看(yes、no),

如果输入的是yes则提示用户请查看,否则提示"退出,你放弃查看"。*/

class program

{

static void Main(string[] args)

{

Console.WriteLine("请输入你的年龄?");

int age = Convert.ToInt32(Console.ReadLine());

if (age >= 18)

{

Console.WriteLine("可以查看!");

}

else if (age >= 10)

{

Console.WriteLine("你确定要观看吗?(输入yes观看,其他退出)");

string input = Console.ReadLine();

if (input == "yes")

{

Console.WriteLine("请观看!");

}

else

{

Console.WriteLine("你放弃了观看!");

}

}

else

{

Console.WriteLine("年龄太小,还不能看这种片子!");

}

Console.ReadKey();

}

}

class program

{

static void Main(string[] args)

{

Console.WriteLine("请输入你的年龄?");

int age = Convert.ToInt32(Console.ReadLine());

if (age >= 18)

{

Console.WriteLine("可以查看!");

}

else if (age < 10)

{

Console.WriteLine("不允许查看!");

}

else

{

Console.WriteLine("是否继续查看?(yes、no)");

string input = Console.ReadLine();

if (input == "yes")

{

Console.WriteLine("看吧!");

}

else if (input == "no")

{

Console.WriteLine("乖!");

}

else

{

Console.WriteLine("输入错误!");

}

}

Console.ReadKey();

}

}

/*依次提示用户输入两个整数(假设i1、i2)。如果i1、i2都是正数,

则将i1的值递增一个数,然后打印i1+i2的值;如果i1、i2都是负数,

则将i1的值递减10个数,然后打印i1*i2的值;如果i1、i2中任一个为0,则提示数据有错误;否则计算i1*i2的绝对值。*/

class program

{

static void Main(string[] args)

{

Console.WriteLine("输入第一个整数?");

string s1 = Console.ReadLine();

int i1 = Convert.ToInt32(s1);

Console.WriteLine("输入另一个整数?");

string s2 = Console.ReadLine();

int i2 = Convert.ToInt32(s2);

if (i1 > 0 & i2 > 0)

{

i1++;

Console.WriteLine(i1 + i2);

}

else if (i1 < 0 & i2 < 0)

{

i1 -= 10;

Console.WriteLine(i1 * i2);

}

else if (i1 == 0 || i2 == 0)

{

Console.WriteLine("数据有误!");

}

else

{

//Console.WriteLine(Math.Abs(i1 * i2));

Console.WriteLine(-(i1 * i2));

}

Console.ReadKey();

}

}

class program

{

static void Main(string[] args)

{

Console.WriteLine("请输入第一个整数?");

string s1 = Console.ReadLine();

int i1 = Convert.ToInt32(s1);

Console.WriteLine("请输入第二个整数?");

string s2 = Console.ReadLine();

int i2 = Convert.ToInt32(s2);

//如果i1,i2都是正数,则将i1的值递增一个数,然后打印i1+i2的值

if (i1 > 0 && i2 > 0)

{

i1++; //i1=i1+1

Console.WriteLine("{0}+{1}={2}", i1, i2, i1 + i2);

}

//如果i1,i2都是负数,则将i1的值递减10个数,然后打印i1*i2的值

else if (i1 < 0 && i2 < 0)

{

i1 = i1 - 10; //i1-=10;

Console.WriteLine("{0}*{1}={2}", i1, i2, i1 * i2);

}

//如果i1,i2中任一个为0,则提示数据有错误;

else if (i1 == 0 || i2 == 0)

{

Console.WriteLine("数据有错误!");

}

else//否则计算i1*i2的绝对值

{

int i3 = i1 * i2;

if (i3 < 0)

{

i3 = -i3;//i3= 0 -i3;

}

Console.WriteLine("{0}*{1}的绝对值是:{2}", i1, i2, i3);

}

Console.ReadKey();

}

}

/*李四的年终工作评定,如果定为A级,则工资涨500元,如果定为B级,则工资涨200元,如果定为C级,工资不变,如果定为D级,工资降200元,如果定为E级,工资降500元。设李四的原工资为5000元,请用户输入李四的评级,然后显示李四来年的工资。*/ class program

{

static void Main(string[] args)

{

Console.WriteLine("请输入你对李四的评定等级(A-E)?");

string input = Console.ReadLine();

decimal salary = 5000m;

if (input == "A")

{

salary += 500;//salary=salary+500

}

else if (input == "B")

{

salary += 200;

}

else if (input == "C")

{

}

else if (input == "D")

{

salary -= 200;

}

else if (input == "E")

{

salary -= 500;

}

else

{

Console.WriteLine("输入有误,只能输入大写字母A、B、C、D、E ");

}

Console.WriteLine("李四的工资为:{0}", salary);

Console.ReadKey();

}

}

class program

{

static void Main(string[] args)

{

Console.WriteLine("请输入你对李四的评定等级(A-E)?");

string input = Console.ReadLine();

decimal salary = 5000m;

bool flag = false;

if (input == "A")

{

salary += 500;//salary=salary+500

}

else if (input == "B")

{

salary += 200;

}

else if (input == "C")

{

}

else if (input == "D")

{

salary -= 200;

}

else if (input == "E")

{

salary -= 500;

}

else

{

Console.WriteLine("输入有误,只能输入大写字母A、B、C、D、E ");

flag = true; //设一个标签

}

if (flag == false)

{

Console.WriteLine("李四的工资为:{0}", salary);

}

Console.ReadKey();

}

}

class program

{

static void Main(string[] args)

{

Console.WriteLine("请输入你对李四的评定等级(A-E)?");

string input = Console.ReadLine();

decimal salary = 5000m;

bool flag = false;

switch (input)

{

case"A":

salary += 500;

break;

case"B":

salary += 200;

break;

case"C":

salary += 0;

break;

case"D":

salary -= 200;

break;

case"E":

salary -= 500;

break;

default://注意匹配时和位置没有关系,只和值有关系。

Console.WriteLine("你的输入有误!");

flag = true;

break;

}

if (flag == false)

{

Console.WriteLine("李四的工资为:" + salary);

}

Console.ReadKey();

}

}

class program

{

static void Main(string[] args)

{

Console.WriteLine("请输入年份?");

int year = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("请输入月份?");

int month = Convert.ToInt32(Console.ReadLine());

switch (month)

{

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:

Console.WriteLine("31天");

break;

case 2:

{

if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0)

{

Console.WriteLine("29天"); //是闰年

}

else

{

Console.WriteLine("28天"); //是平年

}

}

break;

default:

Console.WriteLine("30天");

break;

}

Console.ReadKey();

}

}

/*对学员的结业考试成绩评测(改用Switch来做)

成绩>=90:A

90>成绩>=80:B

80>成绩>=70:C

70>成绩>=60:D

成绩<60:E */

class program

{

static void Main(string[] args)

{

Console.WriteLine("请输入你的成绩?");

int score = Convert.ToInt32(Console.ReadLine());

switch(score/10)

{

case 9:

Console.WriteLine("A");

break;

case 8:

Console.WriteLine("B");

break;

case 7:

Console.WriteLine("C");

break;

case 6:

Console.WriteLine("D");

break;

default:

Console.WriteLine("E");

break;

}

Console.ReadKey();

}

}

/*李四这次考试又粗心了,爸爸让他写1000遍"下次考试一定要细心". 如何编程实现上面的问题? */

循环

class program

{

static void Main(string[] args)

{

int i = 0;

while (i < 1000)

{

Console.WriteLine("下次考试一定要细心!" + i);

i++;

}

Console.ReadKey();

}

}

/*打印100次"欢迎您来传智播客学习" */

class program

{

static void Main(string[] args)

{

int i = 0;

while (i < 100)

{

Console.WriteLine("第{0}遍打印:欢迎您来传智播客学习!", i + 1);

i++;

}

Console.ReadKey();

}

}

/*输入班级人数,然后依次输入学员成绩,计算班级学员的平均成绩和总成绩*/

class program

{

static void Main(string[] args)

{

Console.WriteLine("请输入你们班有多少人?");

int count = Convert.ToInt32(Console.ReadLine());

double score = 0;

int i = 0;

double sum = 0;

while (i < count)

{

Console.WriteLine("请输入第{0}个学生的成绩?", i + 1);

score = Convert.ToDouble(Console.ReadLine()); //一定要转换成double类型

sum += score; // sum=sum+score

i++;

}

Console.WriteLine("本班共有{0}人,总成绩为{1}分,平均成绩为{2}分。", count, sum, sum / count);

Console.ReadKey();

}

}

/*老师问学生,这道题你会做了吗?如果学生答"会了(y)",则可以放学.

如果学生不会做(n),则老师再讲一遍,再问学生是否会做了......

直到学生会为止,才可以放学. 直到学生会或老师给他讲了10遍还不会,都要放学*/

class program

{

static void Main(string[] args)

{

Console.WriteLine("这道题你会了吗?(y/n)");

string answer = Console.ReadLine();

while (answer == "n")

{

Console.WriteLine("老师讲一遍题!");

Console.WriteLine("这道题你会了吗?");

answer = Console.ReadLine();

}

Console.WriteLine("放学了,回家吧!");

本文来源:https://www.bwwdw.com/article/ujfl.html

Top