C#存储问题编号并输出问题编号 [英] C# store the question number and outut the question number

查看:54
本文介绍了C#存储问题编号并输出问题编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,所以我有一个逻辑错误,我似乎无法存储和填写问题变量



程序规格人力资源部门要求你写一个对候选人的技术技能进行评分的计划。考试有20个多项选择题。以下是正确的答案:1。E 2. D 3. D 4. B 5. A 6. C 7. E 8. B 9. D 10. C 11. D 12. A 13. A 14. D 15。 E 16. E 17. A 18. E 19.答20.发展议程候选人必须正确回答20个问题中的15个才能通过考试并进入下一级面试。考试题目打印在纸上,考生使用您编写的程序输入问题的答案。在候选人输入问题的所有答案后,程序将显示一条消息,表明他们通过了考试,并且正确答案的总数或表明他们未通过考试的消息以及错误答案的总数和列表候选人回答错误的问题数字。



样本数据预期结果

通过20个正确答案。

通过15个正确答案。

10个错误答案失败。不正确的问题:6,7,8,9,10,16,17,18,19,20如果我需要对我的代码/问题进行更改请告诉我,我会尽量使其可读







谢谢



我尝试过:



Hello, so i am having a logical error i cant seem to store and incrament the questions variable

Program Specifications The human resources department has asked you to write a program that grades the candidate’s technical skills. The exam has 20 multiple choice questions. Here are the correct answers: 1. E 2. D 3. D 4. B 5. A 6. C 7. E 8. B 9. D 10. C 11. D 12. A 13. A 14. D 15. E 16. E 17. A 18. E 19. A 20. D A candidate must correctly answer 15 out of 20 questions to pass the exam and advance to the next level of interviews. The exam questions are printed on paper and the candidate enters their answers to the questions using the program you write. After the candidate enter all the answers to the questions, the program will either display a message indicating that they passed the exam and the total number of correct answers or a message indicating that they failed the exam and the total number of incorrect answers and the list of question numbers that the candidate answered incorrectly.

Sample data Expected result
Passed with 20 correct answers.
Passed with 15 correct answers.
Failed with 10 incorrect answers. Incorrect questions: 6, 7, 8, 9, 10, 16, 17, 18, 19, 20 if i need to make alterations to my code/ questions please LET ME KNOW, i will try and make it readable as possible



Thank you

What I have tried:

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

namespace CPSC1012_CorePortfolioAssignment3_EmilyJohnson
{
    class Program
    {
        static void Main(string[] args)
        {
            //Declare variables
            char[] anwserKey = { 'E', 'D', 'D', 'B', 'A', 'C', 'E', 'B', 'D', 'C', 'D', 'A', 'A', 'D', 'E', 'E', 'A', 'E', 'A', 'D' };
            char anwser;

            int[] questions = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
            int correct = 0;
            int incorrect = 0;
            
            int question = questions.Length;
            int wrong = 0;
           
            for (int i = 0; i < questions.Length; i++)
            {
                question = i + 1;
                Console.Write("{0}: ", question);
                anwser = char.Parse(Console.ReadLine().ToUpper());

                if (anwser == anwserKey[i])
                {
                    correct++;
                }
                else
                {
                    incorrect++;
                    
                    if(anwser != anwserKey[i])
                    {                        
                        questions[i] = i + 1;
                        question = questions[i];
                    }
                }
            }
            if (correct >= 15)
            {
                Console.WriteLine("Passes! you got {0} questions correct", correct);
            }

            else
            {
                Console.WriteLine("Failed! you got {0} questions wrong", incorrect);
                Console.WriteLine("Incorrect Question: ");
                for (int n = 0; n < questions.Length; n++)
                {

                    Console.Write("{0} ", questions[incorrect]);
                }


            } Console.ReadLine();

        }

    }
}

推荐答案

1。看看这部分:

1. Look at this part:
if(anwser != anwserKey[i])
{
    questions[i] = i + 1;
    question = questions[i];
}



你的意思是:


do you mean:

if(anwser != anwserKey[i])
{
    i = i + 1;
    question = questions[i];
}



?即便如此,这也是错误的。问题的增加与答案的正确性无关。

2. answerKey是一个数组,每个答案元素都由索引号标识,为什么不使用这个索引号作为问题编号而不是创建另一个称为问题的数组?



3.两个变量的正确和不正确总是加起来总问题,你需要两个吗?

+++++ [第2轮] +++++

4.如果你想输出有错误答案的问题编号,那么你将需要存储这些问题编号,我建议使用List,例如


? Even that, it is wrong. The increment of question have nothing to do with the correctness of answer.
2. The answerKey is an array, each of its answer element is identified by an index number, why not use this index number as the question number rather than creating another array called questions?

3. The two variables correct and incorrect always add up to total number of questions, do you need both?
+++++ [Round 2 ] +++++
4. If you want to be able to output the question numbers that have the wrong answers, then you will need to store these question numbers, I suggest using a List, e.g.

List<int> questionWithWrongAnswer = new questionWithWrongAnswer<int>();
// other code omitted
 
if (anwser == anwserKey[i])
{
    correct++;
}
else
{
    incorrect++;
                    
    questionWithWrongAnswer.Add(i);
}



学习 C#列表 [ ^ ]

5.最后但并非最不重要的,你怎么看?这个:


Learn C# List[^]
5. Last but not least, what do you think of this:

incorrect = answerKey.Length - correct;


首先我会简化

First I would simplify
for (int i = 0; i < questions.Length; i++)
{
    question = i + 1;
    Console.Write("{0}: ", question);
    anwser = char.Parse(Console.ReadLine().ToUpper());

    if (anwser == anwserKey[i])
    {
        correct++;
    }
    else
    {
        incorrect++;

        if(anwser != anwserKey[i])
        {
            questions[i] = i + 1;
            question = questions[i];
        }
    }
}



删除奇怪的无用代码


by removing weird useless code

for (int i = 0; i < questions.Length; i++)
{
    question = i + 1;
    Console.Write("{0}: ", question);
    anwser = char.Parse(Console.ReadLine().ToUpper());

    if (anwser == anwserKey[i])
    {
        correct++;
    }
    else
    {
        incorrect++;
    }
}



然后,我会启动调试器以查看代码正在做什么。

然后你必须考虑如何停止已经包含问题数字的问题,以便能够判断哪些是失败的。



你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就接近了一个错误。



[更新]

问题出在你的身上介意。

问题的问题位置存储问题编号如果问题尚未在同一位置包含问题编号。

您必须定义存储在问题中的内容以及何时,以便知道哪个问题失败了。


Then, I would kick the debugger to see what the code is doing.
Then you have to think how to halter questions, which already contain questions numbers, to be able to tell which ones was failed.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.

[Update]
The problem is in your mind.
Storing the question number at question position in questions would ne a good idea if questions was not already containing the question number at same position.
You have to define what you store in questions and when, in order to know which question was failed.


这篇关于C#存储问题编号并输出问题编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆