让我摆脱困境,我尝试了很多,但感到困惑 [英] get me out of this i m tried a lot but confused

查看:68
本文介绍了让我摆脱困境,我尝试了很多,但感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Program
    {
        string MobileName;
        bool Nokia5233;
        bool Nokia2690;
        bool SonyP910i;
        int Price;
        public void ListOfMobiles()
        {
           
            
            Console.WriteLine("The first Model Is Nokia5233");
            
            Console.WriteLine("The second Model Is SonyP910i");
            
            Console.WriteLine("The third Model is Nokia2690");

            if (Nokia2690)
            {
                Console.WriteLine("The price is 4900");
                //Price = Convert.ToInt32(Console.ReadLine());
            }
            else
            {

            }
            if (Nokia5233)
            {
                Console.WriteLine("The Price is 7000");
                //Price = Convert.ToInt32(Console.ReadLine());
            }
            else
            {


            }
            
        }
        
        public void ChooseMobile()
        {
            Console.WriteLine("Enter Mobile Name");
            //Console.WriteLine("/n The Model Number is: ");
            MobileName = Console.ReadLine();
        }
        public void DisplayMobileDetails()
        {
            Console.WriteLine("The Mobile name is {0}", MobileName);
            Console.WriteLine("The price is {0}", Price );
        }
        static void Main(string[] args)
        {
            Program prm = new Program();
            prm.ListOfMobiles();
            prm.ChooseMobile();
            
            prm.DisplayMobileDetails();
            Console.ReadLine();
        }
    }
}


我编写了此代码,并希望得到结果,但没有得到期望的结果.当我输入手机名称时,它想要显示手机名称及其价格.


i wrote this code nd want the result but the desire result didn''t came out i want when i enter mobile name it show mobile name as well as its price

推荐答案

if (Nokia2690)
           {
               Console.WriteLine("The price is 4900");
               //Price = Convert.ToInt32(Console.ReadLine());
           }
           else
           {

           }
           if (Nokia5233)
           {
               Console.WriteLine("The Price is 7000");
               //Price = Convert.ToInt32(Console.ReadLine());
           }
           else
           {


           }



谁实际上是在设置您的 Nokia2690,Nokia5233 bool值.而且您在其他部分没有任何东西.因此没有输出.

问候



Who is actually setting your Nokia2690,Nokia5233 bool values. And you do not have anything in else part.So no output.

Regards


您需要对照已知项目列表检查输入的详细信息.以您的情况来说,将其硬连线可能是最容易的,但是在现实世界中,我们不会这样做-尽管对于初学者而言,它会给您一个想法.
You need to check the entered details against you list of known items. In your case, it is probably easiest to hardwire it, but in the real world we wouldn''t - for beginners though, it will give you an idea.
public void ChooseMobile()
    {
    bool selected = false;
    do
        {
        Console.WriteLine("Enter Mobile Name");
        //Console.WriteLine("/n The Model Number is: ");
        MobileName = Console.ReadLine();
        switch (MobileName)
            {
            case "Nokia5233":
                Nokia5233 = true;
                selected = true;
                break;
            case Nokia2690:
                Nokia2690 = true;
                selected = true;
                break;
            case SonyP910i:
                SonyP910i = true;
                selected = true;
                break;
            }
        } while (!selected);
    }


如果您提供的代码是完整代码,则不会.绝不.您知道,您已经声明了成员
If the code you provided is the complete code, it won''t. Never. You see, you''ve declared members
bool Nokia5233;
bool Nokia2690;
bool SonyP910i;


但是你不''设置他们的价值观.因此,它们保留默认值false.因此,在ListOfMobiles()中不会触发任何if.而且Price的值也永远不会设置-那么您期望什么?
这里您需要根据用户输入在ChooseMobile()方法中设置相应的布尔值(例如Nokia2690).为此使用switch.另一种-可能更好的选择-是将您的MobileName变成属性,并在其设置部分进行切换,以调整您的对象(设置相应的bool值和价格).

这将帮助您解决方案的工作,仅此而已.还可以做其他一些改进,例如,完全删除bool变量或由枚举成员替换它们


but you dont'' set their values. So, they retain thair default values, which are false. So no if is gonna trigger in ListOfMobiles(). And Price value is never set as well - so what do you expect?
What you need here is to set corresponding boolean value (e.g. Nokia2690) in ChooseMobile() method depending on user input. Use switch for that. One more - and probably better - option is to turn your MobileName into property with switch in its set part that would tune your object (set corresponding bool value and price).

That''ll help your solution work, no more. There are certanly other improvements that can be done, for example removing bool variables completely or replacing them by enum member


这篇关于让我摆脱困境,我尝试了很多,但感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆