Friday 4 July 2014

C# Loops: while loop exercises Write C# program to prompt the user to choose the correct answer from a list of answer choices of a question

The user can choose to continue answering the question or stop answering it. See the example below:
What is the command keyword to exit a loop in C#?
a. int
b. continue
c. break
d. exit
Enter your choice: b
Incorrect!
Again? press y to continue:
Solution:
uusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Csharp_exercises
{
class Program
{
static void Main(string[] args)
{
string choice;
string con="y";
Console.WriteLine("What is the command keyword to exit a loop in C#?");
Console.WriteLine("a.quit");
Console.WriteLine("b.continue");
Console.WriteLine("c.break");
Console.WriteLine("d.exit");


while (con=="y")
{
  Console.Write("Enter your choice:");
  choice =Console.ReadLine();

  if (choice == "c")
  {
    Console.WriteLine("Congratulation!");
  }
  else if (choice == "q" || choice == "e") { Console.WriteLine("Exiting...!");       break; }
  else Console.WriteLine("Incorrect!");

        Console.Write("Again? press y to continue:");
        con = Console.ReadLine().ToString();

          }


     }
   }
}

1 comment: