功能调用问题 [英] Fuction calling problem

查看:69
本文介绍了功能调用问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个程序中,我必须确保 MAX_CAPACITY 值设置为某个适当的值,该值被用作用户的输入。这应该仅在第一次调用 Cache.GetMaxCapacity()时完成。我正在使用自定义控制台来获取用户的输入。



缓存类程序



In this program , I have to make sure that the MAX_CAPACITY value is set to some proper value which is taken as an input from the user. This should be done only the first time when the Cache.GetMaxCapacity() is called. I am using custom console to get inputs from the user.

Program in Cache Class

 private static int MAX_CAPACITY = 0;
                     
public static int GetMaxCapacity()
{
    if (MAX_CAPACITY == 0)
    {
        Console.WriteLine("Enter Max Capacity:");
        MAX_CAPACITY = CustomConsole.ReadInt();
    }

    Console.WriteLine("Returning MAX_CAPACITY");
    return MAX_CAPACITY;
}







主要功能计划






Program in Main function

Console.WriteLine("MAX_CAPACITY is " + Cache.GetMaxCapacity());

Console.WriteLine("MAX_CAPACITY is " + Cache.GetMaxCapacity());

Console.WriteLine("MAX_CAPACITY is " + Cache.GetMaxCapacity());

Console.ReadLine();







我用问题解决了问题上面的代码,但我认为这不合逻辑,解决方案应该更加棘手和优化。请帮助我。




I solved the problem using the above code but I think it's not logical , the solution should be trickier and optimized. Help me on this.

推荐答案

优化这一点真的没有意义,但如果你的优化想法是更少的代码行......那你就去吧。 />


There really is no point in optimizing this but if your idea of optimization is less lines of code...here you go.

//Usage
Console.WriteLie(MAX_CAPACITY == 0 ? "Enter Max Capacity:" : "Returning MAX_CAPACITY");
MAX_CAPACITY = GetMaxCapacity();

//"Refactored" code	
public static int GetMaxCapacity()
{
    return MAX_CAPACITY == 0 ? CustomConsole.ReadInt() : MAX_CAPACITY;
}


这篇关于功能调用问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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