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

本文关键字:错误 明白 为什么 | 更新日期: 2023-09-27 18:35:10

我很确定我有这一切。我认为我的语法以某种方式是错误的,但我没有运气试图谷歌为什么它是错误的。这是我的代码:

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()++; 之后")

感谢您的帮助!

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

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