本文介绍了最常见的除数法的麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在创建一个计算器程序,需要创建GreatestCommononDivisor和ReduceFraction方法;但是我的GreatestCommonDivisor方法并没有给我正确的答案,我不知道为什么。
任何人都能提供任何正确的指导或帮助吗? />
public static void ProcessReduceFraction()
{
Int32 numerator,分母,减少分子,减少分子;
numerator = GetPostiveNonZeroInteger( 请输入非零Postive Numerator) ;
denominator = GetPostiveNonZeroInteger( 请输入非零积分母);
reducedNumerator = numerator / GreatestCommonDivisor(分母,分子);
reducedDenominator = denominator / GreatestCommonDivisor(分子,分母);
Console.WriteLine( {0} / {1}可以简化为:{ 2} / {3},分子,分母,reducedNumerator,reducedDenominator);
Console.ReadLine();
}
public static Int32 GreatestCommonDivisor( Int32 r, Int32 n)
{
Int32 余数,被除数,除数,pervremainder;
dividend = Math.Max(Math.Abs(n),Math.Abs(r));
divisor = Math.Min(Math.Abs(n),Math.Abs(r));
余额= dividend%divisor;
pervremainder = divisor;
while (余数== 0 )
{
if (余数!= 0 )
{
dividend = divisor;
divisor = remaining;
pervremainder = remainder;
余额= dividend%divisor;
}
}
return pervremainder;
}
public static void ProcessGreatestCommonDivisor()
{
Int32 firstnumber,secondnumber,greatcommondivisor;
firstnumber = GetPostiveNonZeroInteger( Plesae输入非零积极第一个数字 );
// firstnumber = Int32.Parse(Console.ReadLine());
secondnumber = GetPostiveNonZeroInteger( 请输入非零Postive Secound Number);
// secoundnumber = Int32.Parse(Console.ReadLine());
greatcommondivisor = GreatestCommonDivisor(secondnumber,firstnumber);
// greatcommondivisor = Int32.Parse(Console.ReadLine());
Console.WriteLine( {0}和{1}的最大公约数为:{2},
firstnumber,secondnumber,greatcommondivisor);
Console.ReadLine();
}
public static Int32 GetPostiveNonZeroInteger( String 提示符)
{
Int32 n;
Console.WriteLine(提示);
n = Int32 .Parse(Console.ReadLine());
while (n < = 0 )
{
Console.WriteLine( 错误:输入非 - 零正面价值);
Console.WriteLine(提示);
n = Int32 .Parse(Console.ReadLine());
}
return n;
}
}
解决方案
这篇关于最常见的除数法的麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!