我正在尝试编写一个递归程序,它将接收用户输入的数字,然后让程序能够计算整数的所有数字的乘积。我已经计算出我希望程序运行的方式,但是我无法计算出如何运行我的循环来计算所有数字的乘积。我发现你可以用c语言来做这件事,用num%10来检索num的最后一个数字,num/10从整数的末尾一次一个地删去一个数字。我只是无法弄清楚如何在C#中实现这一点,以及if/else结构应该如何。整数C的所有数字的乘积#
以下是我为程序编写的代码,已经为写入if/else语句的代码编写了一部分。如果有人能够指出我如何实现这一目标的正确方向,那将不胜感激。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace _3
{
class Tester
{
public static void Main(string[] args)
{
int length;
Write("Enter a number: ");
string num = Console.ReadLine();
length = num.Length;
int productofnum = Int32.Parse(num);
productofnum = CalcProduct(productofnum);
WriteLine("The product of all digits of the number {0} is {1}.",num, productofnum);
}
public static int CalcProduct(int num)
{
int length = num.ToString().Length;
if (length == 0)
{
}
else
{
}
return num;
}
}
}
伟大的作品我怎么想! 关于递归,coud我只是问,有人告诉我,递归中需要循环,因为if(是“Base Case”)和else(递归情况)必须使基本情况成立,例如这是一个例子,我发现它的最简单的递归形式: https://paste.ofcode.org/k6LNEqJNBGVm6k9uZxxEVU 微软在递归,我认为是说,我上面说的是真的。 https://msdn.microsoft.com/en-us/library/z3dk2cc3(v=vs.100).aspx#使用递归 您的答案可以像文章描述的那样进行吗? –
首先,我认为这是写作难的部分,其他所有内容都是必要的,但它们不是您的主要问题。第二个通知,SO不是一个网站,所以人们会解决你的家庭作业(这就是为什么你有2票反对)。现在你能够并且应该继续自己。 – Yonlif