比方说,比例是200:1
如何将该值存储在单个变量中,而不是将两个值(A上的选民数和B上的选民数)存储在两个变量中.
你会怎么做?
解决方法 从问题措辞的方式来看,这可能不是您正在寻找的答案,但最简单的方法是使用结构:struct Ratio{ public Ratio(int a,int b) { this.a = a; this.b = b; } public int a = 1; public int b = 1;}
你几乎肯定会想要使用属性而不是字段,你可能也想要重载==和!=,类似于:
public static bool operator ==(Ratio x,Ratio y){ if (x.b == 0 || y.b == 0) return x.a == y.a; // There is some debate on the most efficIEnt / accurate way of doing the following // (see the comments),however you get the IDea! :-) return (x.a * y.b) == (x.b / y.a);}public static bool operator !=(Ratio x,Ratio y){ return !(x == y);}public overrIDe string ToString(){ return string.Format("{0}:{1}",this.a,this.b);}总结
以上是内存溢出为你收集整理的如何在单个变量中存储比率并在C#中读回全部内容,希望文章能够帮你解决如何在单个变量中存储比率并在C#中读回所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)