none
first time programming in c# RRS feed

  • Question

  • how to to compare to a variable numStudents not the hardcoded value
    of "10" in this?

    class Analysis
    {
       static void Main()
       {
          // initialize variables in declarations   
          int passes = 0; // number of passes       
          int failures = 0; // number of failures   
          int studentCounter = 1; // // student counter
         

            // process 10 students using counter-controlled iteration
            while (studentCounter <= 10)
          {
             // prompt user for input and obtain a value from the user
             Console.Write("Enter result (1 = pass, 2 = fail): ");
             int result = int.Parse(Console.ReadLine());
             
             
              

             // if...else is nested in the while statement           
             if (result == 1)
             {
                passes = passes + 1; // increment passes      
             }
             else
             {
                failures = failures + 1; // increment failures
             }

             // increment studentCounter so loop eventually terminates
             studentCounter = studentCounter + 1;
          } 

          // termination phase; prepare and display results
          Console.WriteLine($"Passed: {passes}\nFailed: {failures}");

          // determine whether more than 8 students passed
          if (passes > 8)
          {
             Console.WriteLine("Bonus to instructor!");
          }
            Console.WriteLine("press any key to continue: ");
            Console.ReadKey(); 
        } 

    • Moved by Danny van DamMVP Wednesday, March 25, 2020 2:36 PM not related to training and cert
    Wednesday, March 25, 2020 4:38 AM

All replies

  • Stripped down code sample.

    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                var studentCounter = 1; 
                while (studentCounter <= 10)
                {
                    Console.WriteLine(studentCounter);
    
                    studentCounter += 1;
                }
    
                Console.WriteLine("Done");
                Console.ReadLine();
            }
        }
    }

    In the future please post code in the code button.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, March 25, 2020 8:42 PM
    Moderator
  • how to to compare to a variable numStudents not the hardcoded value
    of "10" in this?

    class Analysis
    {
       static void Main()
       {
          // initialize variables in declarations   
          int passes = 0; // number of passes       
          int failures = 0; // number of failures   
          int studentCounter = 1; // // student counter
         

            // process 10 students using counter-controlled iteration
            while (studentCounter <= 10)
          {
             // prompt user for input and obtain a value from the user
             Console.Write("Enter result (1 = pass, 2 = fail): ");
             int result = int.Parse(Console.ReadLine());

    You can of course ask for the number of students as the first prompt from
    your program, before the while loop, in the same way that you are getting the 
    "result" value:

    int numStudents;
    
    // prompt user for number of students and obtain a value from the user
    Console.Write("Enter the number of students: ");
    int numStudents = int.Parse(Console.ReadLine());
    
    // process a number of students using counter-controlled iteration
          while (studentCounter <= numStudents)
          {
    ...
    

    - Wayne

    Wednesday, March 25, 2020 10:39 PM