[英]How can I write a conditional without any conditional statements or operators?
My computer teacher wants me to write code to concatenate two strings( in a weird way). 我的计算机老师要我编写代码来连接两个字符串(以一种奇怪的方式)。 The code should be such that if length of both string are equal then output should be (string1 + string2). 代码应该是这样的,如果两个字符串的长度相等,那么输出应该是(string1 + string2)。 Otherwise the output should be string which is larger in length. 否则输出应该是长度更大的字符串。 Challenge is that I should not use if else
statements or condition?exp1:exp2
whatsoever. 挑战是我不应该使用if else
语句或condition?exp1:exp2
。 This is what I am able to come up with (a and b are names of input string): 这是我能够提出的(a和b是输入字符串的名称):
int aLen = a.Length;
int bLen = b.Length;
//+1 is added to lengths to prevent divide by zero
int bGreatFlag = ((aLen+1) % (bLen + 1)) / (aLen + 1); //1 if aLen < bLen; 0 Otherwise
int aGreatFlag = ((bLen+1) % (aLen+1)) / (bLen+1); //1 if bLen < aLen; 0 Otherwise
string result = (a + b).Substring((bGreatFlag) * aLen,(aLen + bLen)-(bGreatFlag*aLen)-(aGreatFlag*bLen));
I believe that there is another way to approach this question which I am missing altogether(an inbuilt function or some LINQ maybe?). 我相信还有另一种方法可以解决这个问题,我完全没有这个问题(一个内置函数或一些LINQ可能?)。 Any other approach or any pointers in the right direction to join strings conditionally will be really helpful. 任何其他方法或正确方向的任何指针有条件地连接字符串将是非常有帮助的。 Thanks :) . 谢谢 :) 。 Please be kind if the answer to this is very trivial. 如果对此的答案非常简单,请善待。
Since you're allowed to use LINQ, here's a possible solution: 既然你被允许使用LINQ,这是一个可能的解决方案:
But your strings into a collection, group it by the length of its string, order the result by the length of the strings, then take the group with the longest strings. 但是你的字符串进入一个集合,按字符串的长度对它进行分组,按字符串的长度对结果进行排序,然后使用最长字符串的组。 Since now you have a collection of either both strings (if they are of equal length) or the longer one, create a string of this collection by using String.Join
. 从现在开始,您拥有两个字符串(如果长度相等)或较长字符串的集合,请使用String.Join
创建此集合的字符串。
Spoiler (don't miss the fun of implementing this yourself): 剧透 (不要错过自己实现这个的乐趣):
var result = String.Join("", new[]{a, b}.GroupBy(x => x.Length).OrderByDescending(x => x.Key).First().ToArray()); var result = String.Join(“”,new [] {a,b} .GroupBy(x => x.Length).OrderByDescending(x => x.Key).First()。ToArray());
you can use Math.Sign to determine which length is greater. 您可以使用Math.Sign来确定哪个长度更大。 Hope this helps... 希望这可以帮助...
string a, b, result;
switch (Math.Sign(a.Length - b.Length))
{
case 1: result = a; break;
case -1: result = b; break;
default: result = a + b; break;
}
try using while like this 尝试使用这样的
string r = "bla", rr = "blabla";
while (rr.Length > r.Length)
{
MessageBox.Show("it works");
break;
}
where the r and rr is a example of the two strings and when it enters the break serves to make it run only once so it would work like a if... 其中r和rr是两个字符串的一个例子,当它进入break时,它只能运行一次,所以它会像if ...
Since I liked your way of doing it I improved it a bit for you: 既然我喜欢你的做法,我会为你改进一下:
string a = "strin";
string b = "string";
string combined = (a + b);
int aLength = a.Length;
int bLength = b.Length;
int aCheck = (aLength % bLength) / aLength;
int bCheck = (bLength % aLength) / bLength;
// If aCheck and bCheck are both 0 (same length) it isn't going to add any characters from the substring as
// aLength * 0 = 0. If either aCheck or bCheck is 1 then it will add the length from aLength again to the string.
string result = combined += combined.Substring(0, (aLength * (aCheck + bCheck)));
result += combined.Substring(0, (bLength * bCheck)); ;
As per other's comments, your teacher doesn't exclude all the branches possible, eg you could use a while
and break after one loop. 根据其他人的评论,你的老师并没有排除所有可能的分支,例如你可以使用一段while
并在一个循环后中断。 switch
would be my favourite, with the use of CompareTo()
to reduce the range of output to -1, 0 and 1
: switch
将是我的最爱,使用CompareTo()
将输出范围缩小到-1, 0 and 1
:
switch ((s1.Length - s2.Length).CompareTo(0))
{
case -1: // s1 less than s2
return s2;
case 1: // s1 greater than s2
return s1;
case 0: // equal length
return s1 + s2;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.