Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使代码显示信息?_C# - Fatal编程技术网

C# 如何使代码显示信息?

C# 如何使代码显示信息?,c#,C#,我试图调试C#代码,但找不到为什么它不显示数据和没有错误消息的逻辑错误 首先,代码是大块的,因此我尝试将代码拆分为较小的方法,但仍然无法找到错误所在 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Thread

我试图调试C#代码,但找不到为什么它不显示数据和没有错误消息的逻辑错误

首先,代码是大块的,因此我尝试将代码拆分为较小的方法,但仍然无法找到错误所在

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace Drivers_license_exam
{
    public partial class Form1 : Form
    {
        private string[] studentAnsArray = new string[20];
        private string[] correctAnsArray = { "B", "D", "A", "A", "C", "A","B", "A", "C", "D", "B","C",
                                    "D", "A", "D", "C","C", "B", "D", "A" };

        public Form1()
        {
            InitializeComponent();
        }

        private void GetFile(out string fileName)
        {
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                fileName = openFile.FileName;
            }
            else
            {
                fileName = "";
            }
        }

        private void GetAndReadFile(string fileName)
        {
            string results;
            StreamReader inputFile = File.OpenText(fileName);

            while (!inputFile.EndOfStream)
            {
                results = inputFile.ReadLine();
                studentAns.Items.Add(results);
                //studentAnsArray[index] = inputFile.ReadLine();
                //correctAns.Items.Add(studentAnsArray);
            }
            foreach (string answers in correctAnsArray)
            {
                correctAns.Items.Add(answers);
            }

            inputFile.Close();
        }

        private int TotCorrect()
        {
            int correct = 0;

            for (int index = 0; index < correctAnsArray.Length; index++)
            {
                if (studentAnsArray[index]== correctAnsArray[index])
                {
                    correctTxt.Text = index.ToString();
                    correct++;
                }
            }

            return correct;
        }

        private int TotIncorrect()
        {
            int incorrect = 0, questNum = 1;

            for (int index = 0; index < correctAnsArray.Length; index++)
            {
                if (studentAnsArray[index] == correctAnsArray[index])
                {
                    incorrectAns.Items.Add("Question: " + questNum);
                    incorrectAns.Items.Add("Incorrect");
                    incorrect++;
                }
            }

            return incorrect;

        }
        private bool PassedTest()
        {
            bool pass = false;

            if (TotCorrect() >= 15)
            {
                passFailedTxt.Text = "Passed";
            }
            if (TotIncorrect() < 15)
            {
                passFailedTxt.Text = "Failed";
            }

            return pass;
        }

        private void displayRes_Click(object sender, EventArgs e)
        {
            string studentAns;

            GetFile(out studentAns);
            GetAndReadFile(studentAns);
            TotCorrect();
            TotIncorrect();
            PassedTest();

        }

        private void exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Clear_Click(object sender, EventArgs e)
        {

        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.IO;
命名空间驱动程序\u许可证\u考试
{
公共部分类Form1:Form
{
私有字符串[]学生数组=新字符串[20];
私有字符串[]correctAnsArray={“B”、“D”、“A”、“A”、“C”、“A”、“B”、“A”、“C”、“D”、“B”、“C”,
“D”、“A”、“D”、“C”、“C”、“B”、“D”、“A”};
公共表格1()
{
初始化组件();
}
私有void GetFile(输出字符串文件名)
{
if(openFile.ShowDialog()==DialogResult.OK)
{
fileName=openFile.fileName;
}
其他的
{
fileName=“”;
}
}
私有无效GetAndReadFile(字符串文件名)
{
字符串结果;
StreamReader inputFile=File.OpenText(文件名);
而(!inputFile.EndOfStream)
{
结果=inputFile.ReadLine();
学生。项目。添加(结果);
//studentAnsArray[index]=inputFile.ReadLine();
//更正。项目。添加(学生安排);
}
foreach(更正数组中的字符串应答)
{
更正。项目。添加(答案);
}
inputFile.Close();
}
私有int-TotCorrect()
{
int correct=0;
for(int index=0;index=15)
{
passFailedTxt.Text=“已通过”;
}
如果(TOT不正确()<15)
{
passFailedTxt.Text=“失败”;
}
回程通行证;
}
私有void displayRes\u Click(对象发送方,事件参数e)
{
弦乐学生;
GetFile(校外学生);
GetAndReadFile(学生);
TotCorrect();
tot不正确();
通过测试();
}
私有无效退出\单击(对象发送者,事件参数e)
{
这个。关闭();
}
私有无效清除\u单击(对象发送者,事件参数e)
{
}
}
}
我希望代码能够输出以下内容:

  • 显示不正确的结果
  • 在文本框中显示通过或失败
  • 在文本框中显示正确答案和错误答案的总数
我从这段代码中得到的输出是: -不会在列表框中显示错误答案 -尽管文本和数组中的答案正确,但显示失败 -不显示正确答案和错误答案的总数

可能重复的答案。简而言之,你的逻辑可能在某个地方是错误的。您建议存在许多问题,因此可能值得您花时间调试代码,并将问题缩小到特定位置。的可能重复。简而言之,你的逻辑可能在某个地方是错误的。您认为存在许多问题,因此花时间调试代码并将问题缩小到特定位置可能是值得的。