小上下文错误

时间:2019-03-18 17:46:03

标签: c#

无法真正弄清楚这段代码有什么问题,如果有人可以快速看一下它会很高兴!我猜大概只是一些简单的错误,主要是上下文错误。 抱歉瑞典! 谢谢!

我遇到这些错误:

错误CS0103当前上下文中不存在名称'flaskor'

这恰好也会影响add_soda,print_crate,calc_total,antal_flaskor

错误CS1022类型或名称空间定义,或预期的文件结尾

错误CS0116名称空间不能直接包含诸如字段或方法之类的成员

using System;

namespace sodacrate
{
class Sodacrate
{
    private string[] flaskor = new string[24];
    private int antal_flaskor = 0;
    public int sum = 0;

    public void Run()
    {
        Console.WriteLine("|*|Välkommen till läskbacken!|*|");

        int temp = 0;
        do
        {
            Console.WriteLine("|*|Välj ett alternativ|*|");
            Console.WriteLine("/~*/~*/~*/~*/~*/~*/~*/~*/~*/~*/~*/~*/~*/~*/");
            Console.WriteLine("(1): Lägg till en läsk");
            Console.WriteLine("(2): Skriv ut innehållet i läskbacken");
            Console.WriteLine("(3): Beräkna totala värdet på läskbacken");
            Console.WriteLine("(4): Avsluta programmet");

            temp = int.Parse(Console.ReadLine());
            switch (temp)
            {
                case 1:
                    add_soda();
                    break;
                case 2:
                    print_crate();
                    break;
                case 3:
                    calc_total();
                    break;
                case 4:
                    Console.WriteLine("Programmet avslutas...");
                    break;
                default:
                    Console.WriteLine("Ogiltig inmatning");
                    break;
            }
        } while (temp != 0);
    }
}

public void add_soda()
{
    if (antal_flaskor == 24)
    {
        Console.WriteLine("Läskbacken är full, du kan inte lägga till fler flaskor!");
        return;
    }

    Console.WriteLine("    |*|*|~MENY~|*|*|");
    Console.WriteLine("    Välj valfri läsk");
    Console.WriteLine("/------------------/");
    Console.WriteLine("(1): Coca-Cola   5kr");
    Console.WriteLine("(2): Fanta       5kr");
    Console.WriteLine("(3): Sprite      5kr");
    Console.WriteLine("(4): Pepsi       5kr");
    Console.WriteLine("(5): Trocadero   5kr");
    Console.WriteLine("/------------------/");

    int temp = int.Parse(Console.ReadLine());
    bool meny = true;

    do
    {
        switch (temp)
        {
            case 1:
                Console.WriteLine("Coca-Cola");
                meny = false;
                break;
            case 2:
                Console.WriteLine("Fanta");
                meny = false;
                break;
            case 3:
                Console.WriteLine("Sprite");
                meny = false;
                break;
            case 4:
                Console.WriteLine("Pepsi");
                meny = false;
                break;
            case 5:
                Console.WriteLine("Trocadero");
                meny = false;
                break;
            default:
                Console.WriteLine("Ogiltig inmatning");
                meny = false;
                break;
        }
    } while (meny);
    Console.WriteLine("Tryck för att återgå till menyn...");
}
}

public void print_crate()
{
foreach (var dryck in flaskor)
{
    if (dryck != null)
        Console.WriteLine(dryck);
    else
        Console.WriteLine("Ledig");
}
}

public int calc_total()
{
int total = 0;

foreach (var dryck in flaskor)
{
    if (dryck != null)
        total += 5;
}
return total;
}
}

class Program
{
public static void Main(string[] args)
{
    var sodacrate = new Sodacrate();
    sodacrate.Run();
    Console.Write("Press any key to continue . . . ");
    Console.ReadKey(true);
}
}
}

2 个答案:

答案 0 :(得分:2)

您试图在类之外但在名称空间中编写方法。
这是不允许的!

这里是一些问题,请加注释,请更加严格地为每个打开支架定义一个关闭支架:

// [...]

        } while (temp != 0);
    }
} // <== this one closes the class, you should move it after all methods!

public void add_soda()
{

// [...]

    Console.WriteLine("Tryck för att återgå till menyn...");
}

} // <== this one closes the namespace, you should move it after the whole code!

public void print_crate()
{

// [...]


还要注意,您需要using sodacrate;才能在Program类中创建一个新的Sodacrate对象!

答案 1 :(得分:0)

我重新排列了代码:

using System;

namespace sodacrate
{
    public class Sodacrate
    {
        private string[] flaskor = new string[24];
        private int antal_flaskor = 0;
        public int sum = 0;

        public void Run()
        {
            Console.WriteLine("|*|Välkommen till läskbacken!|*|");

            int temp = 0;
            do
            {
                Console.WriteLine("|*|Välj ett alternativ|*|");
                Console.WriteLine("/~*/~*/~*/~*/~*/~*/~*/~*/~*/~*/~*/~*/~*/~*/");
                Console.WriteLine("(1): Lägg till en läsk");
                Console.WriteLine("(2): Skriv ut innehållet i läskbacken");
                Console.WriteLine("(3): Beräkna totala värdet på läskbacken");
                Console.WriteLine("(4): Avsluta programmet");

                temp = int.Parse(Console.ReadLine());
                switch (temp)
                {
                    case 1:
                        add_soda();
                        break;
                    case 2:
                        print_crate();
                        break;
                    case 3:
                        calc_total();
                        break;
                    case 4:
                        Console.WriteLine("Programmet avslutas...");
                        break;
                    default:
                        Console.WriteLine("Ogiltig inmatning");
                        break;
                }
            } while (temp != 0);
        }

        public void add_soda()
        {
            if (antal_flaskor == 24)
            {
                Console.WriteLine("Läskbacken är full, du kan inte lägga till fler flaskor!");
                return;
            }

            Console.WriteLine("    |*|*|~MENY~|*|*|");
            Console.WriteLine("    Välj valfri läsk");
            Console.WriteLine("/------------------/");
            Console.WriteLine("(1): Coca-Cola   5kr");
            Console.WriteLine("(2): Fanta       5kr");
            Console.WriteLine("(3): Sprite      5kr");
            Console.WriteLine("(4): Pepsi       5kr");
            Console.WriteLine("(5): Trocadero   5kr");
            Console.WriteLine("/------------------/");

            int temp = int.Parse(Console.ReadLine());
            bool meny = true;

            do
            {
                switch (temp)
                {
                    case 1:
                        Console.WriteLine("Coca-Cola");
                        meny = false;
                        break;
                    case 2:
                        Console.WriteLine("Fanta");
                        meny = false;
                        break;
                    case 3:
                        Console.WriteLine("Sprite");
                        meny = false;
                        break;
                    case 4:
                        Console.WriteLine("Pepsi");
                        meny = false;
                        break;
                    case 5:
                        Console.WriteLine("Trocadero");
                        meny = false;
                        break;
                    default:
                        Console.WriteLine("Ogiltig inmatning");
                        meny = false;
                        break;
                }
            } while (meny);
            Console.WriteLine("Tryck för att återgå till menyn...");
        }

        public void print_crate()
        {
            foreach (var dryck in flaskor)
            {
                if (dryck != null)
                    Console.WriteLine(dryck);
                else
                    Console.WriteLine("Ledig");
            }
        }

        public int calc_total()
        {
            int total = 0;

            foreach (var dryck in flaskor)
            {
                if (dryck != null)
                    total += 5;
            }
            return total;
        }

    }
}



class Program
{
    public static void Main(string[] args)
    {
        var sodacrate = new sodacrate.Sodacrate();
        sodacrate.Run();
        Console.Write("Press any key to continue . . . ");
        Console.ReadKey(true);
    }
}