[英]How can I divide two user inputs using only loops and using any operations is not allowed?
Console.Write("Input Value for A: ");
int a = int.Parse(Console.ReadLine());
Console.Write("Input Value for B: ");
int b = int.Parse(Console.ReadLine());
int acc = 0;
for (int i = a; a >= b; a--)
{
acc += b;
}
Console.Write("The quotient is {0}", acc);
Console.ReadKey(true);
好吧,这是一个棘手的问题,因此我立即不喜欢它。 解决方案如下:
var quotient = 0;
for (var acc = b; acc <= a; acc += b, quotient += 1);
Console.Write($"The quotient of {a} divided by {b} is {quotient}");
诀窍是您仍在使用操作,但作为循环本身的一个步骤,而不是作为循环体内的显式操作,这可能是练习的重点。
更新:此代码仅在a
和b
具有相同符号时才给出准确的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.