GIT地址 | https://github.com/1402120950 |
---|---|
GIT用戶名 | 1402120950 |
學號後五位 | 24115 |
博客地址 | https://www.cnblogs.com/Amazing-zyj/ |
做業連接 | https://edu.cnblogs.com/campus/xnsy/GeographicInformationScience/homework/7582 |
對vs2015採用最簡單的輸出代碼進行環境測試。git
5.輸入地址進行克隆。github
用if函數判斷答題的對或錯算法
主函數dom
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _Random
{
class Program
{
static void Main(string[] args)
{
mathvoid op = new mathvoid();
int a = 0;
int n = 0;
int i = 0;
string Z = "";
Random r = new Random();
a = r.Next(0, 4);
Console.WriteLine("");
Console.WriteLine("請輸入出題數目:");
n = int.Parse(Console.ReadLine());
for (i = 0; i < n; i++)
{
Z = a.ToString();
switch (Z)
{
case "1":
op.mathjia();
continue;
case "2":
op.mathjian();
continue;
case "3":
op.mathcheng();
continue;
case "4":
op.mathchu();
continue;
}
}
Console.WriteLine("總共答對" + op.getright() + "道題!答錯"+op.getwrongt ()+"道題!");
}
}
}函數
調用的方法 (加,減,乘,除)工具
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _Random
{
public class mathvoid
{
public static int right = 0; //記錄答對的總數!
public static int wrong = 0; //記錄答錯的總數!
public void mathjia() //加法運算!
{
int a, b;
int result;
Random rd = new Random();
a = rd.Next(0, 11);
b = rd.Next(0, 11);
Console.WriteLine("請計算:{0}+{1}=?", a, b);
result = Convert.ToInt32(Console.ReadLine());
if (result == a + b)
{
Console.WriteLine("回答正確!");
right++;
}
else
{
Console.WriteLine("錯誤,繼續努力!");
wrong++;
}
}
public void mathjian() //減法運算!
{
int a, b;
int result;
Random rd = new Random();
a = rd.Next(0, 11);
b = rd.Next(0, 11);
Console.WriteLine("請計算:{0}-{1}=?", a, b);
result = Convert.ToInt32(Console.ReadLine());
if (result == a - b)
{
Console.WriteLine("回答正確!");
right++;
}
else
{
Console.WriteLine("錯誤,繼續努力!");
wrong++;
}
}
public void mathcheng() //乘法運算!
{
int a, b;
int result;
Random rd = new Random();
a = rd.Next(0, 11);
b = rd.Next(0, 11);
Console.WriteLine("請計算:{0}{1}=?", a, b);
result = Convert.ToInt32(Console.ReadLine());
if (result == a b)
{
Console.WriteLine("回答正確!");
right++;
}
else
{
Console.WriteLine("錯誤,繼續努力!");
wrong++;
}
}
public void mathchu() //除法運算!
{
int a, b;
int result;
Random rd = new Random();
a = rd.Next(0, 11);
b = rd.Next(0, 11);
if (b != 0)
{
Console.WriteLine("請計算:{0}/{1}=?", a, b);
result = Convert.ToInt32(Console.ReadLine());
if (result == a / b)
{
Console.WriteLine("回答正確!");
right++;
}
else
{
Console.WriteLine("錯誤,繼續努力!");
wrong++;
}
}
else
{
if (a != 0)
{
Console.WriteLine("請計算:{0}/{1}=?", b, a);
result = Convert.ToInt32(Console.ReadLine());
if (result == b / a)
{
Console.WriteLine("回答正確!");
right++;
}
else
{
Console.WriteLine("錯誤,繼續努力!");
wrong++;
}
}
}
}
public int getright() //統計結果!
{
return right;
}
public int getwrongt() //統計結果!
{
return wrong ;
}
}
}單元測試
結果截圖
測試
由於沒有對代碼進行改動,因此無需進行迴歸測試spa
雖然將代碼進行更改(如下爲更改部分,大幅度增長循環次數,不用選擇算法可是仍是由於代碼的侷限性,因此沒法進行幾百萬次計算)設計
int i = 0, z = 0; andom r = new Random(); long n = 10000000000000; Console.WriteLine("-------------------------------四則運算-------------------------"); Console.WriteLine(""); Console.WriteLine("請選擇您使用的運算方法:1.加法 2.減法 3.乘法 4.除法 5.退出!"); for (i = 0; i < n; i++) { z = r.Next(1, 4); Z = z.ToString(); switch (Z)
分析結果如圖