我是C#的新手,我有一个课堂项目要完成,涉及多项选择题考试。我已经为考试创建了程序代码,但是如果输入的内容不正确,现在我们需要显示一条错误消息。我尝试了许多不同的方法来完成此操作,但没有结果。我已经发布了整个程序,以便您可以了解我的使用情况。我知道我缺少什么。是的,我的确了解到一些入门课程和教程会极大地增加我的知识,但是在我这样做的时候;任何帮助将不胜感激。预先谢谢你。
using System;
using System.Text.RegularExpressions;
namespace Checkpoint_III
{
class Program
{
static void Main(string[] args)
{
string[] questions = //these are the questions for the quiz
{
"1) On which day was the United States Marine Corps born?",
"2) Where was the Marine Corps founded?",
"3) Robert Mullen was the first Marine Corps Recruiter.",
"4) Who was the Grand Old Man of the Marine Corps?",
"5) Marines are also referred to as “Devil Cats.”",
"6) Who was the first Commandant of the Marine Corps?",
"7) What is the Mascot of the Marine Corps?",
"8) The Blood Stripe worn by Marines is to signify the blood shed during the Battle of Chapultepec.",
"9) What is the Marine Corps motto?",
"10) Marines have often been referred to as “Leathernecks” due to the high leather collars worn to combat sword slashes."
};
string[] answers = //these are the selections for each of the questions
{
" a) 10 November 1776\n b) 4 July 1776\n c) 10 November 1775\n d) 8 December 1777\n",
" a) The White House, Washington D.C.\n b) Tuns Tavern, Philadelphia, PA\n c) Marine Corps Base Quantico, VA\n d) MCRD Parris Island, Beaufort, SC\n",
" True\n False\n",
" a) Archibald Henderson\n b) Chesty Puller\n c) Samuel Nichols\n d) Dan Daly\n",
" True\n False\n",
" a) Chesty Puller\n b) Samuel Nichols\n c) Ophae Mae Johnson\n d) None of the above\n",
" a) Pitbull\n b) Rottweiler\n c) English Bulldog\n d) German Shepard\n",
" True\n False\n",
" a) “Be Prepared”\n b) “Do a Good Turn Daily”\n c) “Semper Fidelis”\n d) None of the above\n",
" True\n False\n",
};
string[] correctanswer = //these are the correct answers for the questions
{
"c",
"b",
"t",
"a",
"f",
"b",
"c",
"t",
"c",
"t"
};
string[] validanswer = //These are the valid inputs accepted to not recieve error message
{
"a b c d t f A B C D T F"
};
int score = 0; //The initializing of the score board. Player starts at 0 while each question is worth 10 points. Final score will be out of 100.
int[] questionsIncorrect = new int[10];
//This is the beginning of my quiz program
Console.WriteLine("Jon Smith");
Console.WriteLine("Classwork");
Console.WriteLine("CP4\n");
Console.WriteLine("Marine Corps History Quiz\n");
Console.WriteLine("There are a series of multiple choice and true/false questions\n");
Console.WriteLine("Input the letter of the selection for multiple choice");
Console.WriteLine("For true/false, enter t or f\n");
Console.WriteLine("Shall we begin?");
Console.WriteLine();
int j = -1;
string check;
Console.WriteLine("Round One");
for (int i = 0; i < 10; i++)
{
System.Console.WriteLine("Q{0}", questions[i]);
System.Console.WriteLine("{0}", answers[i]);
System.Console.WriteLine("Enter Answer :: ");
check = Console.ReadLine();
Console.WriteLine();
//Begin error message code
if (check.Equals(validanswer))
continue;
else
{
Console.WriteLine("Invalid Entry");
}
while
(check.Equals(validanswer) == false);
//End error message code
if (check.Equals(correctanswer[i]))
{
score = score + 10;
}
else
{
j++;
questionsIncorrect[j] = i;
}
}
int k;
if (j > -1)
{
Console.WriteLine("Round II");
for (int i = 0; i <= j; i++)
{
k = questionsIncorrect[i];
System.Console.WriteLine("Q{0}", questions[k]);
System.Console.WriteLine("{0}", answers[k]);
System.Console.WriteLine("Enter Answer :: ");
check = Console.ReadLine();
Console.WriteLine();
if (check.Equals(correctanswer[k]))
{
score = score + 10;
}
else
{
System.Console.WriteLine("Correct Answer is {0}", correctanswer[k]);
}
}
}
Console.WriteLine("Score is {0}%", score);
Console.ReadKey();
}
}
}
答案 0 :(得分:1)
using System;
using System.Linq;
using System.Text.RegularExpressions;
namespace Checkpoint_III
{
class Program
{
static void Main(string[] args)
{
string[] questions = //these are the questions for the quiz
{
"1) On which day was the United States Marine Corps born?",
"2) Where was the Marine Corps founded?",
"3) Robert Mullen was the first Marine Corps Recruiter.",
"4) Who was the Grand Old Man of the Marine Corps?",
"5) Marines are also referred to as “Devil Cats.”",
"6) Who was the first Commandant of the Marine Corps?",
"7) What is the Mascot of the Marine Corps?",
"8) The Blood Stripe worn by Marines is to signify the blood shed during the Battle of Chapultepec.",
"9) What is the Marine Corps motto?",
"10) Marines have often been referred to as “Leathernecks” due to the high leather collars worn to combat sword slashes."
};
string[] answers = //these are the selections for each of the questions
{
" a) 10 November 1776\n b) 4 July 1776\n c) 10 November 1775\n d) 8 December 1777\n",
" a) The White House, Washington D.C.\n b) Tuns Tavern, Philadelphia, PA\n c) Marine Corps Base Quantico, VA\n d) MCRD Parris Island, Beaufort, SC\n",
" True\n False\n",
" a) Archibald Henderson\n b) Chesty Puller\n c) Samuel Nichols\n d) Dan Daly\n",
" True\n False\n",
" a) Chesty Puller\n b) Samuel Nichols\n c) Ophae Mae Johnson\n d) None of the above\n",
" a) Pitbull\n b) Rottweiler\n c) English Bulldog\n d) German Shepard\n",
" True\n False\n",
" a) “Be Prepared”\n b) “Do a Good Turn Daily”\n c) “Semper Fidelis”\n d) None of the above\n",
" True\n False\n",
};
string[] correctanswer = //these are the correct answers for the questions
{
"c",
"b",
"t",
"a",
"f",
"b",
"c",
"t",
"c",
"t"
};
string[] validanswer = //These are the valid inputs accepted to not recieve error message
{
"a", "b", "c", "d", "t", "f", "A", "B", "C", "D", "T", "F"
};
int score = 0; //The initializing of the score board. Player starts at 0 while each question is worth 10 points. Final score will be out of 100.
int[] questionsIncorrect = new int[10];
//This is the beginning of my quiz program
Console.WriteLine("Jon Smith");
Console.WriteLine("Classwork");
Console.WriteLine("CP4\n");
Console.WriteLine("Marine Corps History Quiz\n");
Console.WriteLine("There are a series of multiple choice and true/false questions\n");
Console.WriteLine("Input the letter of the selection for multiple choice");
Console.WriteLine("For true/false, enter t or f\n");
Console.WriteLine("Shall we begin?");
Console.WriteLine();
int j = -1;
string check;
Console.WriteLine("Round One");
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Q{0}", questions[i]);
Console.WriteLine("{0}", answers[i]);
Console.WriteLine("Enter Answer :: ");
check = Console.ReadLine();
Console.WriteLine();
//Begin error message code
if (!validanswer.Contains(check))
{
Console.WriteLine("Invalid Entry");
Console.WriteLine("Enter Answer :: ");
while (validanswer.Contains(check) == false)
{
//End error message code
check = Console.ReadLine();
if (check.Equals(correctanswer[i]))
{
score = score + 10;
}
else
{
j++;
questionsIncorrect[j] = i;
}
}
}
else
{
if (check.Equals(correctanswer[i]))
{
score = score + 10;
}
else
{
j++;
questionsIncorrect[j] = i;
}
}
}
int k;
if (j > -1)
{
Console.WriteLine("Round II");
for (int i = 0; i <= j; i++)
{
k = questionsIncorrect[i];
Console.WriteLine("Q{0}", questions[k]);
Console.WriteLine("{0}", answers[k]);
Console.WriteLine("Enter Answer :: ");
check = Console.ReadLine();
Console.WriteLine();
if (check.Equals(correctanswer[k]))
{
score = score + 10;
}
else
{
Console.WriteLine("Correct Answer is {0}", correctanswer[k]);
}
}
}
Console.WriteLine("Score is {0}%", score);
Console.ReadKey();
}
}
}
让我们找出代码中的错误!
string[] validanswer = { "a b c d t f A B C D T F" };
validanswer
是具有一个元素的数组,因此
check.Equals(validanswer)
永远不会是正确的,因为您尝试过比较
要制作的是:"a" == "a b c t..."
。我改变了数组,我用了
Contains
基本上是在数组中寻找具有该值的元素
while(check.Equals(validanswer) == false);
index out of bounds
问题,因为在while循环中,您需要
读取新值。如果该值是“有效答案”,则算法应仅检查
答案是否正确System.Console..
,只需输入Console..
因为您已经包含了System
命名空间答案 1 :(得分:0)
我认为问题是您正在检查变量'check',它是一个包含内容'a'或'b'等的字符串,与可能的答案数组相对。 您不能针对数组检查单个字符串。为什么要使用数组(有效答案)然后在其中存储一个字符串?
您可以这样做:
string[] validanswers = {
"a",
"b",
...
}
bool isValid = false;
foreach (var validanswer in validanswers)
{
if(check == validanswer)
{
isValid = true;
break;
}
}
if(isValid)
Console.WriteLine("The input is valid");
else
Console.WriteLine("The input is invalid");
上面的代码与contains方法的作用相同。