c# 编写 10以内整数的加减法题目输入答案后,程序自动判断对错,并给出提示“wrong”或“correct”。

如题所述

using System;using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleAppMath{
class Program
{
static void Main(string[] args)
{
int count = 1;//记录答题数量
int error = 0;//记录答题错误数量
do
{

Random r = new Random();
int a = r.Next(0, 11);//定义一个初始数0到10,包含10
int b = r.Next(1, 3);//定义一个标识,若为1,则计算加法,否则,计算减法
if (b == 1) {
int c = r.Next(0, 10 - a);//定义一个和初始数相加小于10 的随即数
Console.WriteLine("第{0}题:{1} + {2} = ? ", count, a, c);
try
{
int result = Convert.ToInt32(Console.ReadLine());//获取输入结果
if (result == a + c)
{
Console.WriteLine("恭喜你,回答正确!");
}
else
{
error++;
Console.WriteLine("回答错误,正确答案应该是:{0}", a + c);
}
}
catch (Exception)
{
//若输入非数字,则该题作废,不计入总的答题数量
Console.WriteLine("你输入的不是数字,此题作废!");
continue;
}

} else
{
int c = r.Next(0, a);
Console.WriteLine("第{0}题:{1} - {2} = ? ", count, a, c);
try
{
int result = Convert.ToInt32(Console.ReadLine());
if (result == a - c)
{
Console.WriteLine("恭喜你,回答正确!");
}
else
{
error++;
Console.WriteLine("回答错误,正确答案应该是:{0}", a - c);
}
}
catch (Exception)
{
Console.WriteLine("你输入的不是数字,此题作废!");
continue;
}
}

Console.WriteLine("按任意键开始下一道,输入 'e'结束!");
count++;

} while (Console.ReadLine() != "e");
int zhengque = count - 1 - error;//计算共答对多少题
int s = Convert.ToInt32((zhengque * 100) / (count - 1));//按照比例计算分数100分为满分
Console.WriteLine("答题结束,你一共做了{0}道题,正确{1}道,错误{2}道,得分:{3}", count - 1, count - 1 - error, error, s);
Console.ReadLine();
}
}
}

//根据一楼的改动了一下,写的小例子,是一个控制台程序,测试了一下,能正常用,单细节可能还需完善。
//望采纳
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-04-19
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Exp05_01
{
class Program
{
static void Main(string[] args)
{
int c = 1;
do
{
Random r = new Random();
int a = r.Next(0, 9);
int b = r.Next(0, 9);
c = -c;
if (c == 1)
{
Console.WriteLine(a + "+" + b + "=");
}
else
{
Console.WriteLine(a + "-" + b + "=");
}
Console.WriteLine("请输入答案:");
int ret = 0;
if (int.TryParse(Console.ReadLine(), out ret))
{
if (c == 1)
{
if (ret == (a + b))
{
Console.WriteLine("correct");
}
else
{
Console.WriteLine("wrong");
}
}
else
{
if (ret == (a - b))
{
Console.WriteLine("correct");
}
else
{
Console.WriteLine("wrong");
}
}
}
else
{
Console.WriteLine("wrong");
}
Console.WriteLine("按任意键开始下一道,输入 'e'结束!");
} while (Console.ReadLine() != "e");
}
}
}

第2个回答  2013-04-19
需求能详细点吗?是不是要你输入一个表达式 程序自动判断ture or false?

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网