zoukankan      html  css  js  c++  java
  • C#基础之------控制台进程

    /*********************************************************************************

    File:C#实现100以内两个数随机数的加减乘除答题系统,带计时功能,分数计算功能。

    Date:2014年8月12日

    Author:小X

    problem:加入线程后,控制台每一秒中刷新一次,显示的时候占用后面的界面,造成排版出现错误。

    **********************************************************************************/

      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Text;
      5 using System.Threading.Tasks;
      6 using System.Threading;
      7 
      8 namespace CalcuSys
      9 {
     10     class Program
     11     {
     12         static int fenshu = 0;
     13         static void Main(string[] args)
     14         {
     15             int i = 0;
     16             Console.WriteLine("----------------简单计算测试!-------------------
    ");
     17             Console.WriteLine("按下任意键开始答题");
     18             StopWatch sw = new StopWatch();
     19             Console.ReadKey();
     20              sw.Start();
     21             while (true)
     22             {
     23                 Calculation();
     24                 i++;
     25                 if (i == 10)
     26                 {
     27                     Console.WriteLine(fenshu * 10 + "");
     28                     Console.WriteLine("结束请输入Y,继续测试请输入回车键");
     29 
     30                     if (Console.ReadLine().ToUpper() == "Y")
     31                         break;
     32                     i = 0;
     33                     fenshu = 0;
     34                 }
     35             }
     36         }
     37 
     38         static void Calculation()
     39         {
     40             var n = new Random();
     41             int num1 = n.Next(100);   //返回一个小于所指定最大值的非负随机数。
     42             int num2 = n.Next(100);
     43             int num3 = n.Next(4);
     44 
     45             string message = "请计算{0}{1}{2}的值";
     46             if (num3 == 0)
     47             {
     48                 Console.WriteLine(string.Format(message, num1, "+", num2));
     49                 int res;
     50                 if (int.TryParse(Console.ReadLine(), out res) && res == num1 + num2)
     51                 {
     52                     Console.WriteLine("回答正确!");
     53                     fenshu++;
     54                 }
     55 
     56                 else
     57                     Console.WriteLine("回答错误!");
     58             }
     59             else if (num3 == 1)
     60             {
     61                 Console.WriteLine(string.Format(message, num1, "-", num2));
     62                 int res;
     63                 if (int.TryParse(Console.ReadLine(), out res) && res == num1 - num2)
     64                 {
     65                     Console.WriteLine("回答正确!");
     66                     fenshu++;
     67                 }
     68                 else
     69                     Console.WriteLine("回答错误!");
     70             }
     71             else if (num3 == 2)
     72             {
     73                 Console.WriteLine(string.Format(message, num1, "*", num2));
     74                 int res;
     75                 if (int.TryParse(Console.ReadLine(), out res) && res == num1 * num2)
     76                 {
     77                     Console.WriteLine("回答正确!");
     78                     fenshu++;
     79                 }
     80                 else
     81                     Console.WriteLine("回答错误!");
     82             }
     83             else
     84             {
     85                 Console.WriteLine(string.Format(message, num1, "/", num2));
     86                 Console.WriteLine("商为:");
     87                 int res;
     88                 if (int.TryParse(Console.ReadLine(), out res) && res == num1 / num2)
     89                 {
     90                     Console.WriteLine("回答正确!");
     91                     Console.WriteLine("余为:");
     92                     if (int.TryParse(Console.ReadLine(), out res) && res == num1 % num2)
     93                     {
     94                         Console.WriteLine("回答正确!");
     95                         fenshu++;
     96                     }
     97                     else
     98                         Console.WriteLine("回答错误!");
     99                 }
    100                 else
    101                     Console.WriteLine("回答错误!");
    102             }
    103         }
    104 
    105         class StopWatch
    106         {
    107             private int Interval = 1000;              //时间间隔,单位毫秒
    108             private int Time = 0;                        //所显示的时间
    109             public void Start()
    110             {
    111                 Thread timer = new Thread(new ThreadStart(Timer));  //新建一个线程,该线程调用Timer()
    112                 timer.Start();                               //启动线程
    113                 Console.CursorVisible = false;   //隐藏光标
    114                 //Console.ReadKey(true);            //等待按任意键退出
    115                 //timer.Abort();                              //终止线程,用于停止秒表
    116             }
    117             private void Timer()
    118             {
    119                 while (true)
    120                 {
    121                     Display();                               //显示秒表计数
    122                     Thread.Sleep(Interval);         //等待1秒后再执行Timer()刷新计数
    123                     Time++;                                 //秒数加1
    124                 }
    125             }
    126             private void Display()
    127             {
    128                 Console.SetCursorPosition(0, 0);
    129                 Console.WriteLine("Time:" + Time.ToString());
    130             }
    131         }
    132     }
    133 }

    显示效果:

                      (不带进程显示正常)

    加入计时功能:

                    (加入进程之后显示错乱,未找到解决方法)

    欢迎大家一起交流 ,分享程序员励志故事。   幸福的程序员 QQ群:幸福的程序员
  • 相关阅读:
    Android控件之ListView探究二
    解决activity页面跳转时显示桌面
    TextView实现滚动显示的效果
    QPainter的坐标系系统的转换
    MODBUS_RTU通信协议
    收藏的博客 -- Qt有关的GitHub/Gitee开源项目
    VS联调多个解决方案的项目
    软件工具——GitGUI使用教程
    Python中对文件的操作
    VMware虚拟机三种网络模式详解 --------- NAT(地址转换模式)
  • 原文地址:https://www.cnblogs.com/1hua1ye/p/3908657.html
Copyright © 2011-2022 走看看