The program below is written in C# and it neither compiles or runs. Please fix t
ID: 3890494 • Letter: T
Question
The program below is written in C# and it neither compiles or runs. Please fix the errors and document the errors fixed.
using System;
namespace DebugFixIFStmt
{
class Program
{
static void Main(string[] args)
{
(new Program()).run();
}
void run()
{
int firstChoice = 0, secondChoice = 0, thirdChoice = 0;
//System.Console.WriteLine("Corrected Copy");
firstChoice = 0; secondChoice = 0; thirdChoice = 0;
WriteCurrentChoices(firstChoice, secondChoice, thirdChoice);
firstChoice = 2; secondChoice = 0; thirdChoice = 0;
WriteCurrentChoices(firstChoice, secondChoice, thirdChoice);
firstChoice = 2; secondChoice = 5; thirdChoice = 0;
WriteCurrentChoices(firstChoice, secondChoice, thirdChoice);
firstChoice = 2; secondChoice = 5; thirdChoice = 7;
WriteCurrentChoices(firstChoice, secondChoice, thirdChoice);
}
void WriteCurrentChoices(int firstChoice, int secondChoice, int thirdChoice)
{
if (secondChoice == 0)
Console.WriteLine("Choices are: {0}, {1}, {2} => There are no choices yet", firstChoice, secondChoice, thirdChoice);
else if (secondChoice = 0)
Console.WriteLine("Choices are: {0}, {1}, {2} => Currently choices are {0}", firstChoice, secondChoice, thirdChoice, firstChoice);
else if (thirdChoice === 0)
Console.WriteLine("Choices are: {0}, {1}, {2} => Currently choices are {0}, {1}", firstChoice, secondChoice, thirdChoice, firstChoice, secondChoice);
else if(thirdChoice == 0 )
Console.WriteLine("Choices are: {0}, {1}, {2} => Currently choices are {0}, {1}, {2}",
firstChoice, secondChoice, thirdChoice, firstChoice, secondChoice, thirdChoice);
}
}
}
Explanation / Answer
Given below is the fixed code. The output is shown. If you need someother output, please update the needed output so that any logical errors can be fixed. The syntactic errors are fixed.
using System;
namespace DebugFixIFStmt
{
class Program
{
static void Main(string[] args)
{
(new Program()).run();
}
void run()
{
int firstChoice = 0, secondChoice = 0, thirdChoice = 0;
System.Console.WriteLine("Corrected Copy");
firstChoice = 0; secondChoice = 0; thirdChoice = 0;
WriteCurrentChoices(firstChoice, secondChoice, thirdChoice);
firstChoice = 2; secondChoice = 0; thirdChoice = 0;
WriteCurrentChoices(firstChoice, secondChoice, thirdChoice);
firstChoice = 2; secondChoice = 5; thirdChoice = 0;
WriteCurrentChoices(firstChoice, secondChoice, thirdChoice);
firstChoice = 2; secondChoice = 5; thirdChoice = 7;
WriteCurrentChoices(firstChoice, secondChoice, thirdChoice);
}
void WriteCurrentChoices(int firstChoice, int secondChoice, int thirdChoice)
{
if (secondChoice == 0)
Console.WriteLine("Choices are: {0}, {1}, {2} => There are no choices yet", firstChoice, secondChoice, thirdChoice);
else if (secondChoice == 0) //FIX: added 2nd = sign
Console.WriteLine("Choices are: {0}, {1}, {2} => Currently choices are {0}", firstChoice, secondChoice, thirdChoice, firstChoice);
else if (thirdChoice == 0) //FIX: removed third = sign
Console.WriteLine("Choices are: {0}, {1}, {2} => Currently choices are {0}, {1}", firstChoice, secondChoice, thirdChoice, firstChoice, secondChoice);
else if(thirdChoice == 0 )
Console.WriteLine("Choices are: {0}, {1}, {2} => Currently choices are {0}, {1}, {2}",
firstChoice, secondChoice, thirdChoice, firstChoice, secondChoice, thirdChoice);
}
}
}
output
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.