简体   繁体   中英

Why is there an undefined error in my C# code?

This code is an examination consisting of different specific types of questions. The problem in this code is the error that is undefined. Hi. This code is an examination consisting of different specific types of questions. The problem in this code is the error that is undefined.

using System;

class HelloWorld 
{
    static void Main() 
    {
        string id, fullName, yourAnswer; 
        int number;
        bool exam = true;
    
        while (exam)
        {
        //Codes for Inputting ID number and Full Name
        Console.Write("Please enter your ID Number: ");
        id = Console.ReadLine(); 
        Console.Write("Please enter your Full Name: ");
        fullName = Console.ReadLine(); 
        Console.Clear();
        bool repeat = true;
    
        //Codes for Displaying ID number and Full Name
        Console.WriteLine("STUDENT DETAILS");
        Console.WriteLine("ID Number: {0}", id);
        Console.WriteLine("Full Name: {0}", fullName);
        Console.WriteLine();
    
            while (repeat)
            {
            //Codes for Confirming personal details
            Console.Write("Would you like to edit your personal details again? ");
            yourAnswer = Console.ReadLine ();
            
                //Codes for Checking the answer    
               if (yourAnswer.ToUpper() == "YES")
                {
                exam = true;
                repeat = false;
                Console.Clear();
                }
                else if (yourAnswer.ToUpper() == "NO")
                {
                exam = false;
                repeat = false;
                Console.Clear();
                }
                else
                {
                Console.WriteLine("Answer not found. Please try again!");
                exam = true;
                repeat = false;
                }
            }
        }
            
        //Codes for choosing the type of exam  
        Console.WriteLine("TYPE OF EXAM");
        Console.WriteLine("1 = True or False");
        Console.WriteLine("2 = Multiple Choice");
        Console.WriteLine("3 = Identification");
        Console.WriteLine();
        Console.Write("What would you like to take first? ");
    
        number = Int32.Parse(Console.ReadLine());
        Console.Clear();
    
        //Codes for executing scores
        string correctAns;
        int score;
        int questions = 30;
    
        if(correctAns == yourAnswer){
           score++;
        }
    
        percentage = (int)Math.Round((double)(score * 100) / questions);
    
          //Codes for the final score
         //Console.Write("Congratulations! You got " + score " /30");
          //Console.Write("Your final score is " + percentage + "%.");
    
            //Codes for the exam content
            switch (number)
             {
                case 1:
                Console.WriteLine("Type of Exam: True or False");
                Console.WriteLine("THE HCI IS HSCI");
                yourAnswer = Console.ReadLine();
                correctAns = "TRUE";
                break;
        
                case 2:
                Console.WriteLine("Type of Exam: Multiple Choice");
                break;
        
                case 3:
                Console.WriteLine("Type of Exam: Identification");
                break;
        
                default:
                Console.WriteLine("Type of Exam: Enter number 1, 2 or 3 only.");
                break;
            }    
    }
}

Okay so I found a few issues with your code. Please see below with some comments of how I got it to run.

string id, fullName, yourAnswer = ""; //I initiated the value of yourAnswer
int number;
bool exam = true;
double percentage; //You are missing this


while (exam)
{
    //Codes for Inputting ID number and Full Name
    Console.Write("Please enter your ID Number: ");
    id = Console.ReadLine();
    Console.Write("Please enter your Full Name: ");
    fullName = Console.ReadLine();
    Console.Clear();
    bool repeat = true;

    //Codes for Displaying ID number and Full Name
    Console.WriteLine("STUDENT DETAILS");
    Console.WriteLine("ID Number: {0}", id);
    Console.WriteLine("Full Name: {0}", fullName);
    Console.WriteLine();

    while (repeat)
    {
        //Codes for Confirming personal details
        Console.Write("Would you like to edit your personal details again? ");
        yourAnswer = Console.ReadLine();

        //Codes for Checking the answer    
        if (yourAnswer.ToUpper() == "YES")
        {
            exam = true;
            repeat = false;
            Console.Clear();
        }
        else if (yourAnswer.ToUpper() == "NO")
        {
            exam = false;
            repeat = false;
            Console.Clear();
        }
        else
        {
            Console.WriteLine("Answer not found. Please try again!");
            exam = true;
            repeat = false;
        }
    }
}

//Codes for choosing the type of exam  
Console.WriteLine("TYPE OF EXAM");
Console.WriteLine("1 = True or False");
Console.WriteLine("2 = Multiple Choice");
Console.WriteLine("3 = Identification");
Console.WriteLine();
Console.Write("What would you like to take first? ");

number = Int32.Parse(Console.ReadLine());
Console.Clear();

//Codes for executing scores
string correctAns = null;
int score = 0;
int questions = 30;

if (correctAns == yourAnswer)
{
    score++;
}

percentage = (int)Math.Round((double)(score * 100) / questions);

//Codes for the final score
//Console.Write("Congratulations! You got " + score " /30");
//Console.Write("Your final score is " + percentage + "%.");

//Codes for the exam content
switch (number)
{
    case 1:
        Console.WriteLine("Type of Exam: True or False");
        Console.WriteLine("THE HCI IS HSCI");
        yourAnswer = Console.ReadLine();
        correctAns = "TRUE";
        break;

    case 2:
        Console.WriteLine("Type of Exam: Multiple Choice");
        break;

    case 3:
        Console.WriteLine("Type of Exam: Identification");
        break;

    default:
        Console.WriteLine("Type of Exam: Enter number 1, 2 or 3 only.");
        break;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM