In: Computer Science
So pretty much I need my code without the arrays, or lists. Please and thank you!
Important: You may not use arrays, lists, or similar for your questions. This will be covered in the next module. The objective is to use conditionals in order to achieve the overall task.
Checkpoint 3 is a continuation of the “Quiz” Programming Project.
This module week, you will implement repetitive tasks in your program while using conditional and iteration statements in C#.
Important: You may not use arrays, lists, or similar for your questions. This will be covered in the next module. The objective is to use conditionals in order to achieve the overall task.
My CODE:
using System.IO;
using System;
namespace Checkpoint_3
{
class Program
{
static void Main(string[] args)
{
// display the current date
DateTime d = DateTime.Now;
Console.WriteLine("{0:MM/dd/yyyy HH:mm:ss}", d);
Console.WriteLine("Quiz");
Console.WriteLine("John Doe");
Console.WriteLine("ENGR 115");
//Creating object
Program p = new Program();
Console.WriteLine("Score is {0}", p.playGame());
Console.ReadKey();
}
//Method that returns questions
string getQuestions(int i)
{
string[] questions =
{
"Question 1 - ",
"Question 2 - ",
"Question 3 - ",
"Question 4 - ",
"Question 5 - ",
"Question 6 - ",
"Question 7 - ",
"Question 8 - ",
"Question 9 - ",
"Question 10 -"
}; //Questions for test
return questions[i];
}
//Method that returns answers
string getAnswers(int i)
{
string[] answers =
{
"A. \nB. \nC. \nD. ",
"A. \nB. \nC. \nD. ",
"A. \nB. \nC. \nD. ",
"A. \nB. \nC. \nD. ",
"A. \nB. 5 \nC. \nD. ",
"A. \nB. \nC. \nD. ",
"A. \nB. \nC. \nD. ",
"A. \nB. \nC. \nD. ",
"A. \nB. \nC. \nD. ",
"A. \nB. \nC. \nD. ",
}; // Answer Bank
return answers[i];
}
//Method that returns correct answers
string getCorrectAnswers(int i)
{
string[] correctanswer =
{
"D", "D", "B", "B", "D", "D", "C", "B", "C", "A"
}; //These are the correct answers for the quiz. If one of these is
incorrect it will re-display it a second time.
return correctanswer[i];
}
//Validates the input
Boolean validateInput(string answer, string correctAnswer)
{
{
if (answer.Equals("A") || answer.Equals("B") || answer.Equals("C")
|| answer.Equals("D"))
{
return true;
}
else
{
Console.WriteLine("\n Invalid Answer!!! Answer as either A, B, C,
or D. \n");
return false;
}
}
}
//Game
int playGame()
{
//Variables
int scoreCard = 0; // This is what I used to display the number of
correct/incorrect answers during the second attempt.
int[] questionsIncorrect = new int[10];
Console.WriteLine();
int j = -1;
string check;
Console.WriteLine("Attempt No 1");
for (int i = 0; i < 10; i++)
{
System.Console.WriteLine("{0}", getQuestions(i));
System.Console.WriteLine("{0}", getAnswers(i));
System.Console.WriteLine("Enter the answer :: ");
check = Console.ReadLine();
//Validating answer
while (validateInput(check, getCorrectAnswers(i)) == false)
{
//Re-reading
check = Console.ReadLine();
}
Console.WriteLine();
if (check.Equals(getCorrectAnswers(i)))
{
scoreCard = scoreCard + 1;
}
else
{
j++;
questionsIncorrect[j] = i;
}
}
int k;
if (j > -1)
{
Console.WriteLine("Attempt No 2");
for (int i = 0; i <= j; i++)
{
k = questionsIncorrect[i];
System.Console.WriteLine("{0}", getQuestions(k));
System.Console.WriteLine("{0}", getAnswers(k));
System.Console.WriteLine("Enter the answer :: ");
check = Console.ReadLine();
//Validating answer
while (validateInput(check, getCorrectAnswers(k)) == false)
{
//Re-reading
check = Console.ReadLine();
}
Console.WriteLine();
if (check.Equals(getCorrectAnswers(k)))
{
scoreCard = scoreCard + 1;
}
else
{
System.Console.WriteLine("Correct Answer is {0}",
getCorrectAnswers(k));
}
}
}
return scoreCard;
}
}
}
Screenshot
Program
using System;
namespace Checkpoint_3
{
class Program
{
static void
Main(string[] args)
{
// display the current date
DateTime d = DateTime.Now;
Console.WriteLine("{0:MM/dd/yyyy HH:mm:ss}", d);
Console.WriteLine("Quiz");
Console.WriteLine("John Doe");
Console.WriteLine("ENGR 115");
//Creating object
Program p = new Program();
Console.WriteLine("Score is {0}", p.playGame());
Console.ReadKey();
}
//Method that returns
questions
string getQuestions(int
i)
{
String question1 = "Question 1 - ";
String question2 = "Question 2 - ";
String question3 = "Question 3 - ";
String question4 = "Question 4 - ";
String question5 = "Question 5 - ";
String question6 = "Question 6 - ";
String question7 = "Question 7 - ";
String question8 = "Question 8 - ";
String question9 = "Question 9 - ";
String question10 = "Question 10 - ";
if (i == 1)
{
return question1;
}
else if (i == 2)
{
return question2;
}
else if (i == 3)
{
return question3;
}
else if (i == 4)
{
return question4;
}
else if (i == 5)
{
return question5;
}
else if (i == 6)
{
return question6;
}
else if (i == 7)
{
return question7;
}
else if (i == 8)
{
return question8;
}
else if (i == 9)
{
return question9;
}
else
{
return question10;
}
}
//Method that returns
answers
string getAnswers(int
i)
{
string answers1 = "A. \nB. \nC. \nD. ";
string answers2= "A. \nB. \nC. \nD. ";
string answers3 = "A. \nB. \nC. \nD. ";
string answers4 = "A. \nB. \nC. \nD. ";
string answers5 = "A. \nB. \nC. \nD. ";
string answers6 = "A. \nB. \nC. \nD. ";
string answers7 = "A. \nB. \nC. \nD. ";
string answers8 = "A. \nB. \nC. \nD. ";
string answers9 = "A. \nB. \nC. \nD. ";
string answers10 = "A. \nB. \nC. \nD. ";
if (i == 1)
{
return answers1;
}
else if (i == 2)
{
return answers2;
}
else if (i == 3)
{
return answers3;
}
else if (i == 4)
{
return answers4;
}
else if (i == 5)
{
return answers5;
}
else if (i == 6)
{
return answers6;
}
else if (i == 7)
{
return answers7;
}
else if (i == 8)
{
return answers8;
}
else if (i == 9)
{
return answers9;
}
else
{
return answers10;
}
}
//Method that returns
correct answers
string
getCorrectAnswers(int i)
{
string correctanswer1 = "D";
string correctanswer2= "D";
string correctanswer3 = "B";
string correctanswer4 = "B";
string correctanswer5 = "D";
string correctanswer6 = "D";
string correctanswer7 = "C";
string correctanswer8 = "B";
string correctanswer9 = "C";
string correctanswer10 = "A";
if (i == 1)
{
return correctanswer1;
}
else if (i == 2)
{
return correctanswer2;
}
else if (i == 3)
{
return correctanswer3;
}
else if (i == 4)
{
return correctanswer4;
}
else if (i == 5)
{
return correctanswer5;
}
else if (i == 6)
{
return correctanswer6;
}
else if (i == 7)
{
return correctanswer7;
}
else if (i == 8)
{
return correctanswer8;
}
else if (i == 9)
{
return correctanswer9;
}
else
{
return correctanswer10;
}
}
//Validates the
input
Boolean
validateInput(string answer, string correctAnswer)
{
if (answer.Equals("A") || answer.Equals("B") || answer.Equals("C")
|| answer.Equals("D"))
{
return true;
}
else
{
Console.WriteLine("\n Invalid Answer!!! Answer as either A, B, C,
or D. \n");
return false;
}
}
//Game
int playGame()
{
//Variables
int scoreCard = 0; // This is what I used to display the number of
correct/incorrect answers during the second attempt.
bool questionsIncorrect1 = false;
bool questionsIncorrect2 = false;
bool questionsIncorrect3 = false;
bool questionsIncorrect4 = false;
bool questionsIncorrect5 = false;
bool questionsIncorrect6 = false;
bool questionsIncorrect7 = false;
bool questionsIncorrect8 = false;
bool questionsIncorrect9 = false;
bool questionsIncorrect10 = false;
Console.WriteLine();
int j = -1;
string check;
Console.WriteLine("Attempt No 1");
for (int i = 1; i <= 10; i++)
{
System.Console.WriteLine("{0}", getQuestions(i));
System.Console.WriteLine("{0}", getAnswers(i));
System.Console.WriteLine("Enter the answer :: ");
check = Console.ReadLine();
//Validating answer
while (validateInput(check, getCorrectAnswers(i)) == false)
{
//Re-reading
check = Console.ReadLine();
}
Console.WriteLine();
if (check.Equals(getCorrectAnswers(i)))
{
scoreCard = scoreCard + 1;
}
else
{
j++;
if (i == 1)
{
questionsIncorrect1=true;
}
else if (i == 2)
{
questionsIncorrect2 = true;
}
else if (i == 3)
{
questionsIncorrect3 = true;
}
else if (i == 4)
{
questionsIncorrect4 = true;
}
else if (i == 5)
{
questionsIncorrect5 = true;
}
else if (i == 6)
{
questionsIncorrect6 = true;
}
else if (i == 7)
{
questionsIncorrect7 = true;
}
else if (i == 8)
{
questionsIncorrect8 = true;
}
else if (i == 9)
{
questionsIncorrect9 = true;
}
else
{
questionsIncorrect10 = true;
}
}
}
int k=0;
while(j > -1)
{
Console.WriteLine("Attempt No 2");
// for (int i = 1; i <= j+1; i++)
//{
if (questionsIncorrect1)
{
k = 1;
questionsIncorrect1 = false;
}
else if (questionsIncorrect2)
{
k = 2;
questionsIncorrect2= false;
}
else if (questionsIncorrect3)
{
k = 3;
questionsIncorrect3 = false;
}
else if (questionsIncorrect4)
{
k = 4;
questionsIncorrect4 = false;
}
else if (questionsIncorrect5)
{
k = 5;
questionsIncorrect5 = false;
}
else if (questionsIncorrect6)
{
k = 6;
questionsIncorrect6 = false;
}
else if (questionsIncorrect7)
{
k = 7;
questionsIncorrect7 = false;
}
else if (questionsIncorrect8)
{
k = 8;
questionsIncorrect8 = false;
}
else if (questionsIncorrect9)
{
k = 9;
questionsIncorrect9 = false;
}
else if(questionsIncorrect10)
{
k = 10;
questionsIncorrect10 = false;
}
System.Console.WriteLine("{0}", getQuestions(k));
System.Console.WriteLine("{0}", getAnswers(k));
System.Console.WriteLine("Enter the answer :: ");
check = Console.ReadLine();
//Validating answer
while (validateInput(check, getCorrectAnswers(k)) == false)
{
//Re-reading
check = Console.ReadLine();
}
Console.WriteLine();
if (check.Equals(getCorrectAnswers(k)))
{
scoreCard = scoreCard + 1;
}
else
{
System.Console.WriteLine("Correct Answer is {0}",
getCorrectAnswers(k));
}
j--;
//}
}
return scoreCard;
}
--------------------------------------------------------------------------------
Output
09-08-2019 19:16:12
Quiz
John Doe
ENGR 115
Attempt No 1
Question 1 -
A.
B.
C.
D.
Enter the answer ::
D
Question 2 -
A.
B.
C.
D.
Enter the answer ::
D
Question 3 -
A.
B.
C.
D.
Enter the answer ::
C
Question 4 -
A.
B.
C.
D.
Enter the answer ::
A
Question 5 -
A.
B.
C.
D.
Enter the answer ::
B
Question 6 -
A.
B.
C.
D.
Enter the answer ::
C
Question 7 -
A.
B.
C.
D.
Enter the answer ::
D
Question 8 -
A.
B.
C.
D.
Enter the answer ::
D
Question 9 -
A.
B.
C.
D.
Enter the answer ::
C
Question 10 -
A.
B.
C.
D.
Enter the answer ::
D
Attempt No 2
Question 3 -
A.
B.
C.
D.
Enter the answer ::
C
Correct Answer is B
Attempt No 2
Question 4 -
A.
B.
C.
D.
Enter the answer ::
A
Correct Answer is B
Attempt No 2
Question 5 -
A.
B.
C.
D.
Enter the answer ::
A
Correct Answer is D
Attempt No 2
Question 6 -
A.
B.
C.
D.
Enter the answer ::
D
Attempt No 2
Question 7 -
A.
B.
C.
D.
Enter the answer ::
C
Attempt No 2
Question 8 -
A.
B.
C.
D.
Enter the answer ::
D
Correct Answer is B
Attempt No 2
Question 10 -
A.
B.
C.
D.
Enter the answer ::
C
Correct Answer is A
Score is 5
---------------------------------------------------------
Note:-
I assume you want to avoid all arrays and change into conditional statements.Any queries let me know