教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 学前教育 >

传智播客C#练习答案

来源:网络收集 时间:2026-08-11
导读: 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

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 = t …… 此处隐藏:19258字,全部文档内容请下载后查看。喜欢就下载吧 ……

传智播客C#练习答案.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/331771.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)