简体   繁体   中英

I don't understand why I am receiving the following errors

I am pretty sure that I have all this right. I am thinking that somehow my syntax is wrong, but I am having no luck trying to Google as to why it is wrong. Here is my code:

namespace Exercise1
{
    class Program
    {
        static void Main(string[] args)
        {
            Values aValue = new Values();

            //set array
            int[] number = new int[12];

            //ask user for input
            Console.WriteLine("Please enter a value between 0 and 10: ");
            aValue.InValue = Console.ReadLine(); //Read input
            aValue.IntValue = int.Parse(aValue.InValue); //convert string to int

            while (aValue.IntValue != -99) //user has not stopped program

                if (aValue.IntValue > 10 || aValue.IntValue < 0) //valid value
                    Console.WriteLine("Thank you! Please enter '-99' when you are ready to finsih.");
            aValue.InValue = Console.ReadLine(); //read input
            aValue.IntValue = int.Parse(aValue.InValue); //convert string to int
            number[aValue.IntValue]++; //add input to corresponding array box

????        else 
                Console.WriteLine("You have entered an invalid value.");
            aValue.InvalidValueCount()++;

            namespace Exercise1
            {
                class Values
                {
                    private string inValue;
                    public string InValue { get; set; }

                    private int intValue;
                    public int IntValue { get; set; }

                    private int validValueCount;
                    public int ValidValueCount { get; set; }

                    private int invalidValueCount;
                    public int InvalidValueCount() { 
                        invalidValueCount = 0;
                        return invalidValueCount;
                    }
                }
           }

Error messages are as follows:

Invalid expression term 'else'
; expected (after "else")
Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement (after " number[aValue.IntValue]++; ")
The operand of an increment or decrement operator must be a variable, property or indexer (after " aValue.InvalidValueCount()++; ")

Thanks for your help!

You need curly braces around multiline blocks of code after while/if/else

        while (aValue.IntValue != -99) //user has not stopped program
        { 
            if (aValue.IntValue > 10 || aValue.IntValue < 0) //valid value
            {
                Console.WriteLine("Thank you! Please enter '-99' when you are ready to finsih.");
                aValue.InValue = Console.ReadLine(); //read input
                aValue.IntValue = int.Parse(aValue.InValue); //convert string to int
                number[aValue.IntValue]++; //add input to corresponding array box
            }
            else 
            {
                Console.WriteLine("You have entered an invalid value.");
                aValue.InvalidValueCount()++; // <-- this is your error
            }
        }

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