【C#】【平时作业】习题-2-数据类型运算符表达式
目录
1、请设计程序,验证算数运算符和赋值运算符。
Int a,b,c
C=a+b
+=
privatevoid button1_Click(object sender, EventArgse)
{
tb.Text = "";
tb.Text = tb.Text +Environment.NewLine + "【题1】" +Environment.NewLine + Environment.NewLine + "1、请设计程序,验证算数运算符和赋值运算符。" + Environment.NewLine + " int a,b,c "+ Environment.NewLine + " c=a+b "+ Environment.NewLine + " 使用 += 验证";
tb.Text = tb.Text +Environment.NewLine + Environment.NewLine + "【解答】 "+ Environment.NewLine + Environment.NewLine + "int a=1, b=2, c=0; ";
int a = 1, b = 2, c =0;
c = a + b;
tb.Text = tb.Text + Environment.NewLine + "c=a+b,c的结果为: "+c.ToString();
a = 1; b = 2; c = 0;
a +=b;
tb.Text = tb.Text +Environment.NewLine + "a+=b,a的结果为: " + a.ToString();
a = 1; b = 2; c = 0;
b += a;
tb.Text = tb.Text +Environment.NewLine + "b+=a,b的结果为: " + b.ToString();
tb.Text = tb.Text +Environment.NewLine + Environment.NewLine + "----------萌狼蓝天解答完毕----------- ";
}
2、请设计程序,验证关系运算符和逻辑运算符。
Bool r;
Int a=6,b=7;
R=a>b;
Bool f=true;
Bool g=false;
R=f&&g
privatevoid button2_Click(object sender, EventArgs e)
{
tb.Text = "";
tb.Text = tb.Text +Environment.NewLine + "【题2】" + Environment.NewLine + Environment.NewLine + "2、请设计程序,验证关系运算符和逻辑运算符。" + Environment.NewLine + "" +
"bool r;"+ Environment.NewLine +
"int a = 6, b = 7;" + Environment.NewLine +
" r = a > b;" + Environment.NewLine +
"bool f = true;" + Environment.NewLine +
"bool g = false;" + Environment.NewLine +
Environment.NewLine + " 使用 R = f && g 验证";
tb.Text = tb.Text +Environment.NewLine + Environment.NewLine + "【解答】 " + Environment.NewLine;
bool r;
int a = 6, b = 7;
r = a > b;
bool f = true;
bool g = false;
tb.Text = tb.Text +Environment.NewLine + "r=a>b ,结果为: " + (r=a>b).ToString();
tb.Text = tb.Text +Environment.NewLine + "r=f&&g,运算的结果为: " + (r = f && g).ToString();
tb.Text = tb.Text + Environment.NewLine + Environment.NewLine + "----------萌狼蓝天解答完毕----------- ";
}
3、什么是短路运算,请举例说明?
privatevoid button3_Click(object sender, EventArgse)
{
tb.Text = "";
tb.Text = tb.Text +Environment.NewLine + "【题3】" + Environment.NewLine + Environment.NewLine + "3、什么是短路运算,请举例说明" + Environment.NewLine + "" +
"短路运算符,当第一个条件起到决定作用的时候,不再执行后面的条件,例如:" +Environment.NewLine +
"if(a && b) 中 如果a为false,则结果一定为false,则后面的b无需再进行判断" + Environment.NewLine +
"if(a || b) 中 如果a为true,则结果一定为true,则后面的b无需再进行判断" + Environment.NewLine;
}
4、项目实训题,要求如图:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
//实训题目:
//从键盘输入学生姓名及其5门课的成绩(语文,数学,英语,物理,化学)的总成绩及其平均分
Console.Write("请输入学生姓名:");
String name = Console.ReadLine();
Console.Write("请输入语文成绩:");
String chinese = Console.ReadLine();
int a = changeInt(chinese);
Console.Write("请输入数学成绩:");
String math = Console.ReadLine();
int b = changeInt(math);
Console.Write("请输入英语成绩:");
String english = Console.ReadLine();
int c = changeInt(english);
Console.Write("请输入物理成绩:");
String physics = Console.ReadLine();
int d = changeInt(physics);
Console.Write("请输入化学成绩:");
String chemistry= Console.ReadLine();
int e = changeInt(chemistry);
//计算
int sum = a + b + c + d + e;
int avg = (sum) / 5;
//输出
Console.Write("\n学生姓名:" + name) ;
Console.Write("\n学生总成绩:" + sum.ToString());
Console.Write("\n学生平均成绩:" +avg.ToString());
//等待
Console.ReadKey();
}
public static int changeInt(String cj) {
int rq;
if (!int.TryParse(cj, out rq))
{
Console.Write("请输入正确的成绩:");
String recj = Console.ReadLine();
changeInt(recj);
}
return rq;
}
}
}
5、以下哪些标识符合法,哪些不合法
张三
_23
int
Int
a
b11_
123
李四+
Length
版 权 声 明