我不明白为什么我收到以下错误

时间:2013-10-12 20:57:43

标签: c# syntax-error

我很确定我拥有这一切。我想我的语法不对,但我没有运气试图谷歌为什么它是错的。这是我的代码:

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;
                    }
                }
           }

错误消息如下:

  

无效的表达术语'else'
  ;预期(在“其他”之后)
  只有赋值,调用,递增,递减,等待和新对象表达式才能用作语句(在“number[aValue.IntValue]++;”之后)   增量或减量运算符的操作数必须是变量,属性或索引器(在“aValue.InvalidValueCount()++;”之后)

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

在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
            }
        }