无法将所选项目保存在数组的第一个位置

时间:2019-05-31 11:07:06

标签: c# arrays if-statement

我无法将输入保存在数组中,因此当我输入所有答案时,我想查看数组中的内容,所有位置都是空的。这是发生的事情,因为我将if语句放在了那里。如果我删除了if语句,则输入将保存在我的数组中,但它将错过第一个输入,因此我将在24个答案中有23个。

我也试图写一个else语句,但是缺少一个相同的问题,如果我删除整个if语句,也会出现同样的问题。

    {


        if (numberOfBottles < 24)
        {
            Console.WriteLine("");
            Console.WriteLine("Write what sodas u want in the crate:"); // Skriver ut text och ber användaren skriva drycken. 

            int soda_input = 0;

            string chosenSoda = "";
            do
            {

                Console.WriteLine("1: Coca-cola"); // skriver ut alternativen för flaska
                Console.WriteLine("2: Fanta");
                Console.WriteLine("3: Pepsi");
                Console.WriteLine("4: Beer");
                Console.WriteLine("5: Redbull");
                Console.WriteLine("6: Cider");
                Console.WriteLine("7: Water");


                try
                {
                    soda_input = int.Parse(Console.ReadLine());
                }
                catch (Exception e)
                {
                    Console.WriteLine("Just numbers, my friend");
                    continue;
                }

                if (chosenSoda == null)**
                {
                    crate[numberOfBottles] = chosenSoda; // lagrar svaren i min vektor
                } THIS IS THE IF-STATEMENT THAT IS MESSING EVERYTHING UP

                numberOfBottles++;  // för varje svar jag skriver så ökar det med 1 i vektorn hela vägen till 24

                switch (soda_input) // Skapar en meny, som skriver ut vilken dryck användaren väljer tills backen blir full.
                {

                    case 1:
                        chosenSoda = "Coca-Cola";
                        Console.WriteLine("-------------------");
                        Console.WriteLine("Du valde Coca-Cola");
                        Console.WriteLine("-------------------");
                        break;

                    case 2:
                        chosenSoda = "Fanta";
                        Console.WriteLine("-------------------");
                        Console.WriteLine("Du valde Fanta");
                        Console.WriteLine("-------------------");
                        break;

                    case 3:
                        chosenSoda = "Pepsi";
                        Console.WriteLine("-------------------");
                        Console.WriteLine("Du valde Pepsi");
                        Console.WriteLine("-------------------");
                        break;

                    case 4:
                        chosenSoda = "Öl";
                        Console.WriteLine("-------------------");
                        Console.WriteLine("Du valde Öl");
                        Console.WriteLine("-------------------");
                        break;

                    case 5:
                        chosenSoda = "Redbull";
                        Console.WriteLine("-------------------");
                        Console.WriteLine("Du valde Redbull");
                        Console.WriteLine("-------------------");
                        break;

                    case 6:
                        chosenSoda = "Cider";
                        Console.WriteLine("-------------------");
                        Console.WriteLine("Du valde Cider");
                        Console.WriteLine("-------------------");
                        break;

                    case 7:
                        chosenSoda = "Vatten";
                        Console.WriteLine("-------------------");
                        Console.WriteLine("Du valde Vatten");
                        Console.WriteLine("-------------------");
                        break;


                    default: // om man skriver siffror ovanför 1-7 så skrivs detta ut.
                        Console.WriteLine("-------------------");
                        Console.WriteLine("Siffor mellan 1-7 min vän");
                        Console.WriteLine("-------------------");
                        break;
                }

            } while (numberOfBottles != 24);

        }
        else
            Console.WriteLine("");
        Console.WriteLine("The crate is full, choose some of the alteratives to check the crate:");
        Console.WriteLine("");


    }

编辑:“杜瓦尔德(Du valde),意味着U选择了瑞典语。.

1 个答案:

答案 0 :(得分:1)

您要在选择任何内容之前分配数组值。

另外,您可能希望将它们保留在循环中,如果输入无效,则不要分配值。

要执行此操作,请将if移至开关之后但while语句之前。通过检查不等于(!=)为null(我认为永远不会这样)并且还检查不等于“”,您正在迫使他们停留在当前瓶子上并继续输入,直到他们做出有效的选择。

尝试一下:

do
        {

            Console.WriteLine("1: Coca-cola"); // skriver ut alternativen för flaska
            Console.WriteLine("2: Fanta");
            Console.WriteLine("3: Pepsi");
            Console.WriteLine("4: Beer");
            Console.WriteLine("5: Redbull");
            Console.WriteLine("6: Cider");
            Console.WriteLine("7: Water");


            try
            {
                soda_input = int.Parse(Console.ReadLine());
            }
            catch (Exception e)
            {
                Console.WriteLine("Just numbers, my friend");
                continue;
            }

            switch (soda_input) // Skapar en meny, som skriver ut vilken dryck användaren väljer tills backen blir full.
            {

                case 1:
                    chosenSoda = "Coca-Cola";
                    Console.WriteLine("-------------------");
                    Console.WriteLine("Du valde Coca-Cola");
                    Console.WriteLine("-------------------");
                    break;

                case 2:
                    chosenSoda = "Fanta";
                    Console.WriteLine("-------------------");
                    Console.WriteLine("Du valde Fanta");
                    Console.WriteLine("-------------------");
                    break;

                case 3:
                    chosenSoda = "Pepsi";
                    Console.WriteLine("-------------------");
                    Console.WriteLine("Du valde Pepsi");
                    Console.WriteLine("-------------------");
                    break;

                case 4:
                    chosenSoda = "Öl";
                    Console.WriteLine("-------------------");
                    Console.WriteLine("Du valde Öl");
                    Console.WriteLine("-------------------");
                    break;

                case 5:
                    chosenSoda = "Redbull";
                    Console.WriteLine("-------------------");
                    Console.WriteLine("Du valde Redbull");
                    Console.WriteLine("-------------------");
                    break;

                case 6:
                    chosenSoda = "Cider";
                    Console.WriteLine("-------------------");
                    Console.WriteLine("Du valde Cider");
                    Console.WriteLine("-------------------");
                    break;

                case 7:
                    chosenSoda = "Vatten";
                    Console.WriteLine("-------------------");
                    Console.WriteLine("Du valde Vatten");
                    Console.WriteLine("-------------------");
                    break;


                default: // om man skriver siffror ovanför 1-7 så skrivs detta ut.
                    Console.WriteLine("-------------------");
                    Console.WriteLine("Siffor mellan 1-7 min vän");
                    Console.WriteLine("-------------------");
                    break;
            }

 if (chosenSoda != null && chosenSoda != "")
            {
                crate[numberOfBottles] = chosenSoda; // lagrar svaren i min vektor
            numberOfBottles++;
            }

        } while (numberOfBottles != 24);