预期额外的括号?

时间:2017-04-27 12:54:32

标签: c# brackets

以下:

if (parseok == false)
{
    Console.WriteLine("Error : Please enter valid numeric Value ");
    Console.ReadLine();
    Environment.Exit(0);
}
//Is currently working fine, however, I am wanting to change the if statement to a while loop - for error checking purposes. When I change the code to this :

while (parseok == false)
{
    Console.WriteLine("Error : Please enter valid numeric Value ");
    Console.ReadLine();
    Console.Write("Please enter minutes for your first run:    ");
    parseok = int.TryParse(Console.ReadLine(), out run1m);
}
//I am recieveing an error message saying that I'm missing a close bracket, when I add the close bracket the whole program becomes riddled with errors. What am I forgetting to do?

//I will add the entire project's code : 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp1
    {
        class Program
        {
            //Ajdin Karahodzic 
            //ID : KARAD1301

        static void Main(string[] args)
        {
            String membertype = "";

            //Declaration of Variables 
            string name = "";
            string member = "";
            string gender = "";
            int run1m = 0;
            int run1s = 0;
            int run2m = 0;
            int run2s = 0;
            int run3m = 0;
            int run3s = 0;
            int run1total = 0;
            int run2total = 0;
            int run3total = 0;
            int totalsecs = 0;
            int avgsecs = 0;
            int hours = 0;
            int minutes = 0;
            int seconds = 0;
            bool parseok = false;
            string menu = "";
            int run1tempS = 0;
            int run1tempM = 0;
            int run1tempH = 0;
            int run2tempS = 0;
            int run2tempM = 0;
            int run2tempH = 0;
            int run3tempS = 0;
            int run3tempM = 0;
            int run3tempH = 0;

            while (menu != "x")
            {
                Console.WriteLine("Please choose from the following : ");
                Console.WriteLine("Enter Runner Details (R) : ");
                Console.WriteLine("Enter run times (T) : ");
                Console.WriteLine("Display runner results (D) : ");
                Console.WriteLine("Exit the program (X) ");
                menu = Console.ReadLine();

                switch (menu)
                {
                    //CLOSE PROGRAM SWITCH
                    case "x":
                    case "X":
                        Environment.Exit(0);
                        break;

                    //ENTER RUNNER DATA SWITCH
                    case "r":
                    case "R":
                        Console.Write("Please enter your name:               ");
                        name = Console.ReadLine();

                    while (string.IsNullOrEmpty(name))
                    {
                        Console.WriteLine("Error : Please ensure you have entered your name, press enter to continue ");
                        Console.ReadLine();
                        Console.Write("Please enter your name:               ");
                        name = Console.ReadLine();
                    }

                    Console.Write("Please enter your  membership number: ");
                    member = Console.ReadLine();
                    while (member.Length != 5)
                    {
                        Console.WriteLine("Error : Membership number must be 5 characters long, press enter to continue ");
                        Console.ReadLine();
                        Console.Write("Please enter your  membership number: ");
                        member = Console.ReadLine();
                    }
                    char first = member[0];
                    while (first != 'o' && first != 'O' && first != 's' && first != 'S' && first != 'j' && first != 'J' && first != 'C' && first != 'c')
                    {
                        Console.WriteLine("Error : Membership number must begin with O, S, J or C, press enter to continue ");
                        Console.ReadLine();
                        Console.Write("Please enter your  membership number: ");
                        member = Console.ReadLine();
                    }
                    if (first == 'o' || first == 'O')
                    {
                        membertype = "Ordinary Member";
                    }
                    else if (first == 's' || first == 'S')
                    {
                        membertype = "Student Member";
                    }
                    else if (first == 'j' || first == 'J')
                    {
                        membertype = "Junior Member";
                    }
                    else if (first == 'c' || first == 'C')
                    {
                        membertype = "Child Member";
                    }
                    string response = "";
                    Console.Write("Please enter your gender (m) or (f) : ");
                    response = Console.ReadLine();
                    gender = response;
                    while (gender != "m" && gender != "M" && gender != "f" && gender != "F")
                    {
                        Console.WriteLine("Error : Gender must be either : M / m (For Male) or F / f (For Female), press enter to continues ");
                        Console.ReadLine();
                        Console.Write("Please enter your gender (m) or (f) : ");
                        response = Console.ReadLine();
                        gender = response;
                    }
                    break;

                    //ENTER RUN TIMES - SWITCH
                    case "T":
                    case "t":
                    //Prompt for user input; collect and store data.
                    /*---------RUN 1 INPUT---------
                      -----------------------------*/
                    //MINUTES
                    Console.Write("Please enter minutes for your first run:    ");
                    parseok = int.TryParse(Console.ReadLine(), out run1m);
                    while (parseok == false)
                    {
                        Console.WriteLine("Error : Please enter valid numeric Value ");
                        Console.ReadLine();
                        Console.Write("Please enter minutes for your first run:    ");
                        parseok = int.TryParse(Console.ReadLine(), out run1m);
                    }
                    else if (run1m < 15 || run1m > 180)
                    {
                        Console.WriteLine("Error : Minutes cannot be less than 15 or greater than 180");
                        Console.ReadLine();
                        Environment.Exit(0);
                    }
                    //SECONDS
                    Console.Write("Please enter seconds for your first run:    ");
                    parseok = int.TryParse(Console.ReadLine(), out run1s);
                    if (parseok == false)
                    {
                        Console.WriteLine("Error : Please enter valid numeric Value ");
                        Console.ReadLine();
                        Environment.Exit(0);
                    }
                    else if (run1s < 0 || run1s > 59)
                    {
                        Console.WriteLine("Error : Seconds must be between 0 and 59 ");
                        Console.ReadLine();
                        Environment.Exit(0);
                    }
                    Console.WriteLine();
                    /*---------RUN 2 INPUT---------
                      ------------------------------*/
                    Console.Write("Please enter minutes for your second run:   ");
                    parseok = int.TryParse(Console.ReadLine(), out run2m);
                    if (parseok == false)
                    {
                        Console.WriteLine("Error : Please enter valid numeric Value ");
                        Console.ReadLine();
                        Environment.Exit(0);
                    }
                    else if (run2m < 15 || run2m > 180)
                    {
                        Console.WriteLine("Error : Minutes cannot be less than 15 or greater than 180");
                        Console.ReadLine();
                        Environment.Exit(0);
                    }
                    Console.Write("Please enter seconds for your second run:   ");
                    run2s = int.Parse(Console.ReadLine());
                    if (parseok == false)
                    {
                        Console.WriteLine("Error : Please enter valid numeric Value ");
                        Console.ReadLine();
                        Environment.Exit(0);
                    }
                    else if (run2s < 0 || run2s > 59)
                    {
                        Console.WriteLine("Error : Seconds must be between 0 and 59 ");
                        Console.ReadLine();
                        Environment.Exit(0);
                    }
                    Console.WriteLine();
                    /*---------RUN 3 INPUT---------
                     ------------------------------*/
                    Console.Write("Please enter minutes for your third run:    ");
                    parseok = int.TryParse(Console.ReadLine(), out run3m);
                    if (parseok == false)
                    {
                        Console.WriteLine("Error : Please enter valid numeric Value ");
                        Console.ReadLine();
                        Environment.Exit(0);
                    }
                    else if (run3m < 15 || run3m > 180)
                    {
                        Console.WriteLine("Error : Minutes cannot be less than 15 or greater than 180");
                        Console.ReadLine();
                        Environment.Exit(0);
                    }
                    Console.Write("Please enter seconds for your third run:    ");
                    run3s = int.Parse(Console.ReadLine());

                    if (parseok == false)
                    {
                        Console.WriteLine("Error : Please enter valid numeric Value ");
                        Console.ReadLine();
                        Environment.Exit(0);
                    }
                    else if (run3s < 0 || run3s > 59)
                    {
                        Console.WriteLine("Error : Seconds must be between 0 and 59 ");
                        Console.ReadLine();
                        Environment.Exit(0);
                    }
                    Console.WriteLine();
                    break;
                    case "d":
                    case "D":
                    // CALCULATIONS
                    //Converting individual run times to seconds
                    run1total = (run1m * 60) + run1s;
                    run2total = (run2m * 60) + run2s;
                    run3total = (run3m * 60) + run3s;
                    //Convert individual times to hours, mins, secs.
                    // RUN1
                    run1tempS = (run1total % 60);
                    run1tempM = ((run1total / 60) % 60);
                    run1tempH = ((run1total / 3600) % 60);
                    // RUN2
                    run2tempS = (run2total % 60);
                    run2tempM = ((run2total / 60) % 60);
                    run2tempH = ((run2total / 3600) % 60);
                    // RUN3
                    run3tempS = (run3total % 60);
                    run3tempM = ((run3total / 60) % 60);
                    run3tempH = ((run3total / 3600) % 60);
                    //Calculate average time
                    totalsecs = (run1total + run2total + run3total);
                    avgsecs = (totalsecs / 3);
                    seconds = (avgsecs % 60);
                    minutes = ((avgsecs / 60) % 60);
                    hours = ((avgsecs / 3600) % 60);
                    //Display results
                    Console.WriteLine();
                    Console.WriteLine("==========================================================================");
                    Console.WriteLine("10 Km results for: ");
                    Console.WriteLine("{0} [{1}] - {2}, {3}  ", name, member, membertype, gender.ToUpper());
                    Console.WriteLine("Run 1 - {0} hr(s) {1} min(s) {2} sec(s). ", run1tempH, run1tempM, run1tempS);
                    Console.WriteLine("Run 2 - {0} hr(s) {1} min(s) {2} sec(s). ", run2tempH, run2tempM, run2tempS);
                    Console.WriteLine("Run 3 - {0} hr(s) {1} min(s) {2} sec(s). ", run3tempH, run3tempM, run3tempS);
                    Console.WriteLine();
                    Console.WriteLine("Average 10km run time is : {0} hours {1} minutes {2} seconds. ", hours, minutes, seconds);
                    break;
                    default:
                      Console.WriteLine("Incorrect input, please try again ");
                      break;
                }
                Console.ReadLine();
            }
        }
    }
}

2 个答案:

答案 0 :(得分:4)

在第161行,你正在做else if但是你没有指定任何if。

enter image description here

我不知道你在代码中做了什么。但要么删除else语句中的else if,要么在else if之前创建if语句。

else if语句在if语句或else if语句之后发出,并且不能作为比较中的第一个语句。

解决方案示例 enter image description here 因此,在if之前添加else if将解决问题。 (注意我现在不需要你的程序需要做什么,因此你需要检查你必须做什么条件检查。)

答案 1 :(得分:2)

你的问题是遗留下来的

else

在第163行。删除它允许代码编译,但我不会验证行为。