将用户输入存储在数组中 [英] Store user input in an array

查看:104
本文介绍了将用户输入存储在数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,

你可以在下面的代码中看到。这是一个程序,它接受用户输入并检查它们是否正确(基于数组),最后它表示你失败或通过。

这一切都很好。

但是
i只需要存储所有错误的答案并将其显示为结果。

我确定我必须在之后添加一些代码:

falselyAnswered = falselyAnswered + 1;



i不知道如何将每个错误答案存入阵列!!



我尝试过:



 静态  void  Main( string  [] args)
{
< span class =code-keyword> char [] studentAnswers = new char [< span class =code-digit> 20 ];
int [] wrongs = new int [ 20 ];
char [] answers = new char [] {' E'' D'' D'' B'' < span class =code-string> A',' C'' E'' B'' D'' C'' D'' A'' A'' D'' E'' E'' A'' E' ' A'' D'};

int correctAnswered = 0 ;
int falselyAnswered = 0 ;

for int i = 0 ; i < studentAnswers.Length; i ++)
{
studentAnswers [i] = InputOutput_v1.GetValidChar( 问题,i);

if (studentAnswers [i] == answers [i])
{
correctAnswered = correctAnswered + 1 ;
}
else
{
falselyAnswered = falselyAnswered + 1 ;
}

}
如果(正确答案> = 15
{
Console.WriteLine( 传递{0}正确答案,正确答案);
}
else
{
Console.WriteLine( {0}错误答案失败。答案不正确:,falselyAnswered,wrongs);
}
Console.ReadLine();
}

解决方案

问题:为什么错误 int 类型?你想储存什么?

  int  [] wrongs =  new   int  [ 20 ]; 



您正在寻找存储的Y / N类型吗?键入 bool 将是更好的选择。或者你想存储错误的答案?十,你需要一个类型 char



我建议如下:

  bool  [] wrongs =  new   bool  [ 20 ]; 



存储错误,你需要在这里添加你的代码:

  else  
{
falselyAnswered = falselyAnswered + 1 ;
wrongs [i] = true ; // 已添加代码
}





[edit:]更新了指定的请求。



要格式化输出,有许多不同的方法。您将更容易 String.Join Method(String,String []) (系统) [ ^ ]

  var  wrongList =  string  .Join( ,wrongAnswers); 


Hey guys,
as you can in see in the code below. This is a program that takes userinput and checks if they are correct or not (based on the array) and at last it says you failed or passed.
This is working all good.
BUT
i only need to store all wrong answers and show them as result.
im sure that i have to add some code after:
falselyAnswered = falselyAnswered + 1;

i dont know how to store each wrong answer into an array!!

What I have tried:

static void Main(string[] args)
{
    char[] studentAnswers = new char[20];
    int[] wrongs = new int[20];
    char[] answers = new char[] { 'E', 'D', 'D', 'B', 'A', 'C', 'E', 'B', 'D', 'C', 'D', 'A', 'A', 'D', 'E', 'E', 'A', 'E', 'A', 'D' };

    int correctlyAnswered = 0;
    int falselyAnswered = 0;

    for (int i = 0; i < studentAnswers.Length; i++)
    {
        studentAnswers[i] = InputOutput_v1.GetValidChar("Question ",i);

        if (studentAnswers[i] == answers[i])
        {
            correctlyAnswered = correctlyAnswered + 1;
        }
        else
        {
            falselyAnswered = falselyAnswered + 1;
        }

    }
    if (correctlyAnswered >= 15)
    {
        Console.WriteLine("Passed with {0} correct answers",correctlyAnswered);
    }
    else
    {
        Console.WriteLine("Failed with {0} incorrect answers. Incorrect answers are: ", falselyAnswered, wrongs );
    }
    Console.ReadLine();
}

解决方案

Question: Why wrong as int type? What are you trying to store?

int[] wrongs = new int[20];


Are you looking for a Y/N type stored? Type bool would be a better choice. Or are you wanting to store the incorrect answer? Ten you would need a type char .

I would suggest the following:

bool[] wrongs = new bool[20];


To store the wrongs, you would need to add your code here:

else
{
    falselyAnswered = falselyAnswered + 1;
    wrongs[i] = true; // code added
}



[edit:] Updated for specified request.

To format output there are a number of different ways. The easier for you would be String.Join Method (String, String[]) (System)[^]

var wrongList = string.Join(", ", wrongAnswers);


这篇关于将用户输入存储在数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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