如何在不填充整个数组的情况下向数组添加用户输入

时间:2019-05-22 20:16:55

标签: c# arrays switch-statement

我正在做作业,代码有问题。
尝试让用户在切换情况下为数组添加值。问题->当他们在此处添加选择时,它会填满整个数组,请帮忙!

    public void add_soda()
    {
        int temp = 0;
        string input;


            Console.WriteLine("Which kind of soda do you want to add?");
            Console.WriteLine("1. Monster");
            Console.WriteLine("2. Päronsoda");
            Console.WriteLine("3. Fanta");
            Console.WriteLine("4. Fanta Exotic");
            Console.WriteLine("5. Coca Cola");
            Console.WriteLine("6. Loka");
            Console.WriteLine("0. Återgå till huvudmenyn");


            Console.Write("Enter Soda number here: ");
            temp = int.Parse(Console.ReadLine());
            for (int i = 0; i < flaskor.Length; i++)
            {


                switch (temp)
                {
                    case 1:
                        input = "Monster";
                        Console.WriteLine("You picked Monster");
                        flaskor[antal_flaskor] = input;
                        antal_flaskor++;
                        break;
                    case 2:
                        input = "Päronsodan";
                        Console.WriteLine("You picked Päronsoda");
                        flaskor[antal_flaskor] = input;
                        antal_flaskor++;
                        break;
                    case 3:
                        input = "Fanta";
                        Console.WriteLine("You picked Fanta");
                        flaskor[antal_flaskor] = input;
                        antal_flaskor++;
                        break;
                    case 4:
                        input = "Fanta Exotic";
                        Console.WriteLine("You picked Fanta Exotic");
                        flaskor[antal_flaskor] = input;
                        antal_flaskor++;
                        break;
                    case 5:
                        input = "Coca Cola";
                        Console.WriteLine("You picked Coca Cola");
                        flaskor[antal_flaskor] = input;
                        antal_flaskor++;
                        break;
                    case 6:
                        input = "Loka";
                        Console.WriteLine("You picked Loka");
                        flaskor[antal_flaskor] = input;
                        antal_flaskor++;
                        break;
                    case 0:
                        Console.WriteLine("To main menu. . .");
                        break;

                    default:

                        Console.WriteLine("Only numbers 0-6");
                        Console.ReadKey();
                    break;
                }
            }

如前所述,我希望用户能够输入“ 5”,然后它只会添加一个可口可乐,但是现在它填充了整个箱子:(

0 个答案:

没有答案