上课时遇到麻烦

时间:2013-11-17 01:50:39

标签: c# visual-studio-2010

我对所有这些都相当新,所以我显然犯了愚蠢的错误,但有人可以告诉我这段代码有什么问题吗?我不知道我哪里出错了。

namespace FoodDrink
{
class DrinkItem
{
    string name;
    string description;
    double alcoholicVolume;
    double cost;

    //Getters (Retrieving information) and setters(Setting information)
    public string getName()
    {
        return name;
    }
    public void setName(string newName)
    {
        name = newName;
    }
    public string getDescription()
    {
        return description;
    }
    public void setDescription(string newDescription)
    {
        description = newDescription; 
    }
    public double getCost()
    {
        return cost;
    }
    public void setCost(double newCost)
    {
        cost = newCost;
    }
    public double getAlcoholicVolume()
    {
        return alcoholicVolume;
    }
    public void setalcoholicVolume(double newalcoholicVolume)
    {
        alcoholicVolume = newalcoholicVolume;
    }
    public DrinkItem()
    {
        //Default constructor, used when no info given
        name = "No name given";
        description = "No description given";
        cost = -1.00;
        alcoholicVolume = 0;
    }
}

    class FoodItem
{
    string name;
    string description;
    double cost;
    int numberServed;


    //Getters (Retrieving information) and setters(Setting information)
    public string getName()
    {
        return name;
    }
    public void setName(string newName)
    {
        name = newName;
    }
    public string getDescription()
    {
        return description;
    }
    public void setDescription(string newDescription)
    {
        description = newDescription; 
    }
    public double getCost()
    {
        return cost;
    }
    public void setCost(double newCost)
    {
        cost = newCost;
    }
    public int getNumberServed()
    {
        return numberServed;
    }
    public void setNumberServed(int newNumberServed)
    {
        numberServed = newNumberServed;
    }
    public FoodItem()
    {
        //Default constructor, used when no info given
        name = "No name given";
        description = "No description given";
        cost = -1.00;
        numberServed = 0;
    }
}

    class Program
{
    static void Main(string[] args)
    {
        //Creating the objects
        FoodItem pizza = new FoodItem();
        FoodItem chips = new FoodItem();
        FoodItem burger = new FoodItem();
        FoodItem cheeseBurger = new FoodItem();
        FoodItem scampi = new FoodItem();
        FoodItem lasagne = new FoodItem();
        FoodItem chicken = new FoodItem();
        FoodItem mixedGrill = new FoodItem();
        FoodItem steak = new FoodItem();
        FoodItem allDayBreakfast = new FoodItem();
        FoodItem vegetarianCurry = new FoodItem();
        FoodItem soup = new FoodItem();
        FoodItem prawnCocktail = new FoodItem();
        FoodItem melonBalls = new FoodItem();
        FoodItem patteAndMelbeToast = new FoodItem();
        FoodItem garlicMushroom = new FoodItem();
        FoodItem herbSaladWithGoatCheese = new FoodItem();
        FoodItem hotFudgeCake = new FoodItem();
        FoodItem iceCream = new FoodItem();
        FoodItem chocolateSundae = new FoodItem();
        FoodItem applePie = new FoodItem();
        FoodItem familyDish = new FoodItem();

        DrinkItem lemonade = new DrinkItem();
        DrinkItem cola = new DrinkItem();
        DrinkItem orange = new DrinkItem();
        DrinkItem whiteWine = new DrinkItem();
        DrinkItem shandy = new DrinkItem();
        DrinkItem redWine = new DrinkItem();
        DrinkItem water = new DrinkItem();
        DrinkItem whisky = new DrinkItem();

        string inputString;

        Console.WriteLine("Welcome ");
        Console.Write("Please press either S for starters, C for childs menu, M for main, D for drinks, or DS for desserts ");
        Console.ReadKey(inputString);

        if (inputString = "s")
        {
            soup.setName("Soup ");
            soup.setDescription("The soup of the day ");
            soup.setCost(3.45 );
            soup.setNumberServed(1);
            Console.WriteLine("Name: " + soup.getName());
            Console.WriteLine("Description: ") + soup.getDescription());
            Console.WriteLine("Cost £" + soup.getCost());
            Console.WriteLine("Number served: " + soup.getNumberServed());

            prawnCocktail.setName("Prawn Cocktail ");
            prawnCocktail.setCost(4.50);
            prawnCocktail.setDescription("Prawns mixed togetherwith mayonnaise,tomato chutney, Worcestershire sauce, horseradish and tabasco ");
            prawnCocktail.setNumberServed(1);
            Console.WriteLine("Name: " +prawnCocktail.getName());
            Console.WriteLine("Description " + prawnCocktail.getDescription());
            Console.WriteLine("Cost: £" + prawnCocktail.getCost());
            Console.WriteLine("Number served " + prawnCocktail.getNumberServed());

            melonBalls.setName("Melon Balls ");
            melonBalls.setDescription("Pieces of watermelon cut into balls ");
            melonBalls.setCost(2.00);
            melonBalls.setNumberServed(1);
            Console.WriteLine("Name: " + melonBalls.getName();
            Console.WriteLine("Description: " + melonBalls.getDescription());
            Console.WriteLine("Cost: £" + melonBalls.getCost());
            Console.WriteLine("Number served " + melonBalls.getNumberServed());

            patteAndMelbeToast.setName("Patte and Melbe Toast ");
            patteAndMelbeToast.setDescription("Malbe toast with your choice of patte ");
            patteAndMelbeToast.setCost(1.60);
            patteAndMelbeToast.setNumberServed(1);
            Console.WriteLine("Name: " patteAndMelbeToast.getName());
            Console.WriteLine("Description: " patteAndMelbeToast.getDescription());
            Console.WriteLine("Cost: £" patteAndMelbeToast.getCost());
            Console.WriteLine("Number served " + patteAndMelbeToast.getNumberServed());

            garlicMushroom.setName("Garlic Mushroom ");
            garlicMushroom.setDescription("Mushrooms coated in a garlic sauce ");
            garlicMushroom.setCost(3.40);
            garlicMushroom.setNumberServed(1);
            Console.WriteLine("Name: " + garlicMushroom.getName());
            Console.WriteLine("Description " + garlicMushroom.getDescription());
            Console.WriteLine("Cost: £" + garlicMushroom.getCost());
            Console.WriteLine("Number served " garlicMushroom.getNumberServed());

            herbSaladWithGoatCheese.setName("Herb Salad With Goat Cheese ");
            herbSaladWithGoatCheese.setDescription("A herb salad coated with grated goats cheese ");
            herbSaladWithGoatCheese.setCost(4.70);
            herbSaladWithGoatCheese.setNumberServed(1);
            Console.WriteLine("Name: " + herbSaladWithGoatCheese.getName());
            Console.WriteLine("Description: " + herbSaladWithGoatCheese.getDescription());
            Console.WriteLine("Cost: £" + herbSaladWithGoatCheese.getCost());
            Console.WriteLine("Number served: " herbSaladWithGoatCheese.getNumberServed());


        }
        else if (inputString = "m")
        {
            scampi.setName("Scampi ");
            scampi.setDescription("Breaded pieces of scampi served with chips ");
            scampi.setCost(5.05);
            scampi.setNumberServed(1);
            Console.WriteLine("Name: " + scampi.getName());
            Console.WriteLine("Description: " + scampi.getDescription());
            Console.WriteLine("Cost: £" + scampi.getCost());
            Console.WriteLine("Number served " +scampi.getNumberServed());

            lasagne.setName("Lasagne ");
            lasagne.setDescription("A small boll of lasagne served with either chips or vegetables ");
            lasagne.setCost(6.50);
            lasagne.setNumberServed(1);
            Console.WriteLine("Name: " + lasagne.getName());
            Console.WriteLine("Description: " + lasagne.getDescription());
            Console.WriteLine("Cost: £" + lasagne.getCost());
            Console.WriteLine("Number served: " + lasagne.getNumberServed());

            chicken.setName("Chicken dinner ");
            chicken.setDescription("A full chicken dinner served with chips, peas and gravy ");
            chicken.setCost(9.99);
            chicken.getNumberServed(1);
            Console.WriteLine("Name: " + chicken.getName());
            Console.WriteLine("Description: " + chicken.getDescription());
            Console.WriteLine("Cost: £" + chicken.getCost());
            Console.WriteLine("Number served: " + chicken.getNumberServed());

            mixedGrill.setName("Mixed Grill ");
            mixedGrill.setDescription("A mixture of grilled meats, including steak, gammon, beef, pork ");
            mixedGrill.setCost(13.69);
            mixedGrill.setNumberServed(1);
            Console.WriteLine("Name: " + mixedGrill.getName());
            Console.WriteLine("Description: " + mixedGrill.getDescription());
            Console.WriteLine("Cost: £" + mixedGrill.getCost());
            Console.WriteLine("Number served: " +mixedGrill.getNumberServed());

            steak.setName("Steak ");
            steak.setDescription("Grilled steak with your choice of vegetables and chips ");
            steak.setCost(8.20);
            steak.setNumberServed(1);
            Console.WriteLine("Name: " + steak.setName());
            Console.WriteLine("Description: " + steak.getDescription());
            Console.WriteLine("Cost: £" + steak.getCost());
            Console.WriteLine("Number served: " +steak.getNumberServed());

            allDayBreakfast.setName("All Day Breakfast ");
            allDayBreakfast.setDescription("A full on breakfast, including sausage, eggs and bacon ");
            allDayBreakfast.setCost(14.55);
            allDayBreakfast.getNumberServed(1);
            Console.WriteLine("Name: " + allDayBreakfast.getName());
            Console.WriteLine("Description: " + allDayBreakfast.getDescription());
            Console.WriteLine("Cost: £" + allDayBreakfast.getCost());
            Console.WriteLine("Number served: " + allDayBreakfast.getNumberServed());

            vegetarianCurry.setName("Vegetarian Curry ");
            vegetarianCurry.setDescription("A mild curry filled with an assortment of vegetables and rice ");
            vegetarianCurry.setCost(8.90);
            vegetarianCurry.setNumberServed(1);
            Console.WriteLine("Name: " + vegetarianCurry.getName());
            Console.WriteLine("Description: " + vegetarianCurry.getDescription());
            Console.WriteLine("Cost: £" + vegetarianCurry.getCost());
            Console.WriteLine("Number served: " + vegetarianCurry.getNumberServed());

            familyDish.setName("Family Dish ");
            familyDish.setDescription("A family sized meal containing a multitude of meats, sauces, side dishes, vegetables, all for mixing together ");
            familyDish.setCost(17.90);
            familyDish.setNumberServed(4);
            Console.WriteLine("Name: " + familyDish.getName());
            Console.WriteLine("Description: " + familyDish.getDescription());
            Console.WriteLine("Cost: £" + familyDish.getCost());
            Console.WriteLine("Number served: " + familyDish.getNumberServed());

        }

        else if (inputString = "c")
        {
            pizza.setName("Pizza ");
            pizza.setDescription("A 6 inch, thick crust, cheese pizza ");
            pizza.setCost(3.95);
            pizza.setNumberServed(1);
            Console.WriteLine("Name: " + pizza.getName());
            Console.WriteLine("Description " + pizza.getDescription());
            Console.WriteLine("Cost: £" + pizza.getCost());
            Console.WriteLine("Number Served " + pizza.getNumberServed());

            burger.setName("Beef buger ");
            burger.setDescription("A single beef burger with optional salad ");
            burger.setCost(4.50);
            burger.setNumberServed(1);
            Console.WriteLine("Name: " + burger.getName());
            Console.WriteLine("Description " + burger.getDescription());
            Console.WriteLine("Cost: £" + burger.getCost());
            Console.WriteLine("Number Served " +burger.getNumberServed());

            cheeseBurger.setName("Cheese burger ");
            cheeseBurger.setDescription("A single cheese burger with optional salad ");
            cheeseBurger.setCost(4.60);
            cheeseBurger.setNumberServed(1);
            Console.WriteLine("Name: " + cheeseBurger.getName());
            Console.WriteLine("Description " +cheeseBurger.getDescription());
            Console.WriteLine("Cost: £" + cheeseBurger.getCost());
            Console.WriteLine("Number Served " + cheeseBurger.getNumberServed());

        }
        else if (inputString = "DS")
        {
            hotFudgeCake.setName("Hot fudge cake ");
            hotFudgeCake.setDescription("A bowl of hot fudge cake which comes with either cream or ice cream ");
            hotFudgeCake.setCost(7.50);
            hotFudgeCake.setNumberServed(1);
            Console.WriteLine("Name: " + hotFudgeCake.getName());
            Console.WriteLine("Description: " + hotFudgeCake.getDescription());
            Console.WriteLine("Cost: £" + hotFudgeCake.getCost());
            Console.WriteLine("Number served: " + hotFudgeCake.getNumberServed());

            iceCream.setName("Ice Cream ");
            iceCream.setDescription("Fresh ice cream which comes in either vanilla, strawberry or chocolate flavour");
            iceCream.setCost(4.60);
            iceCream.setNumberServed(1);
            Console.WriteLine("Name: " + iceCream.getName());
            Console.WriteLine("Description: " + iceCream.getDescription());
            Console.WriteLine("Cost: £" + iceCream.getCost());
            Console.WriteLine("Number served: " + iceCream.getNumberServed());

            chocolateSundae.setName("Chocolate Sundae ");
            chocolateSundae.setDescription("A large bowl of ice cream coated with chocolate sauces, pieces of flakes, chocolates and fudge cake ");
            chocolateSundae.setCost(9.90);
            chocolateSundae.setNumberServed(2);
            Console.WriteLine("Name: " + chocolateSundae.getName());
            Console.WriteLine("Description: " + chocolateSundae.getDescription());
            Console.WriteLine("Cost: £" + chocolateSundae.getCost());
            Console.WriteLine("Number served: " + chocolateSundae.getNumberServed());
        }
        else if (inputString = "D")
        {
            cola.setName("Cola ");
            cola.setDescription("A glass of cold Cola with or without ice ");
            cola.setCost(3.30);
            cola.setalcoholicVolume(0);
            Console.WriteLine("Name: " + cola.getName());
            Console.WriteLine("Description: " + cola.getDescription());
            Console.WriteLine("Cost: £" + cola.getCost());
            Console.WriteLine("Alcoholic volume: " + cola.getAlcoholicVolume());

            lemonade.setName("Lemonade ");
            lemonade.setDescription("A cold glass of lemonade with or without ice ");
            lemonade.setCost(3.30);
            lemonade.setalcoholicVolume(0);
            Console.WriteLine("Name: " + lemonade.getName());
            Console.WriteLine("Description: " + lemonade.getDescription());
            Console.WriteLine("Cost: £" + lemonade.getCost());
            Console.WriteLine("Alcoholic volume: " + lemonade.getAlcoholicVolume());

            orange.setName("Fizzy orange ");
            orange.setDescription("A cold glass of fizzy orange with or without ice ");
            orange.setCost(3.30);
            orange.setalcoholicVolume(0);
            Console.WriteLine("Name: " + orange.getName());
            Console.WriteLine("Description: " + orange.getDescription());
            Console.WriteLine("Cost: £" + orange.getCost());
            Console.WriteLine("Alcoholic volume: " + orange.getAlcoholicVolume());

            water.setName("Water ");
            water.setDescription("A glass of cold water with ice ");
            water.setCost(1.00);
            water.setalcoholicVolume(0);
            Console.WriteLine("Name: " + water.getName());
            Console.WriteLine("Description: " + water.getDescription());
            Console.WriteLine("Cost: £" + water.getCost());
            Console.WriteLine("Alcoholic volume: " + water.getAlcoholicVolume());

            shandy.setName("Shandy ");
            shandy.setDescription("A cool glass of shandy ");
            shandy.setCost(3.50);
            shandy.setalcoholicVolume(0.5);
            Console.WriteLine("Name: " + shandy.getName());
            Console.WriteLine("Description: " + shandy.getDescription());
            Console.WriteLine("Cost: £" + shandy.getCost());
            Console.WriteLine("Alcoholic volume: " + shandy.getAlcoholicVolume());

            whiteWine.setName("White Wine ");
            whiteWine.setDescription("A glass of white wine ");
            whiteWine.setCost(5.30);
            whiteWine.setalcoholicVolume(10.7);
            Console.WriteLine("Name: " + whiteWine.getName());
            Console.WriteLine("Description: " + whiteWine.getDescription());
            Console.WriteLine("Cost: £" + whiteWine.getCost());
            Console.WriteLine("Alcoholic volume: " + whiteWine.getAlcoholicVolume());

            redWine.setName("Red Wine ");
            redWine.setDescription("A glass of red wine ");
            redWine.setCost(5.20);
            redWine.setCost(11.5);
            Console.WriteLine("Name: " + redWine.getName());
            Console.WriteLine("Description: " + redWine.getDescription());
            Console.WriteLine("Cost: £" + redWine.getCost());
            Console.WriteLine("Alcoholic volume: " + redWine.getAlcoholicVolume());

            whisky.setName("Whisky ");
            whisky.setDescription("A glass of whisky ");
            whisky.setCost(10.30);
            whisky.setalcoholicVolume(40);
            Console.WriteLine("Name: " + whisky.getName());
            Console.WriteLine("Description: " + whisky.getDescription());
            Console.WriteLine("Cost: £" + whisky.getCost());
            Console.WriteLine("Alcoholic volume: " + whisky.getAlcoholicVolume());
        }

    }
}

我很抱歉发布了很多内容,但我认为你需要看到这一切。

2 个答案:

答案 0 :(得分:2)

首先,您需要提供正确的使用声明。

using System;
using System.Collections.Generic;
using System.Linq;
etc.

还有连接错误,在几行上缺少括号等等,会产生编译错误。

Console.WriteLine("Name: " patteAndMelbeToast.getName());

而且,if语句需要一个等于运算符==,而不是=

您应该能够通过编译器的反馈来解决其他一些问题。

答案 1 :(得分:0)

首发

Console.ReadKey(inputString);

应该是

inputString = Console.ReadLine();

您有一些示例,例如chicken.getNumberServed(1);应为chicken.setNumberServed(1);

另外,我建议您在Main中包含Console.ReadLine();的最后一行,这样您就可以通过按Enter键来控制控制台窗口关闭的时间。然后你可以实际读出实际输出的内容。