【发布时间】:2015-02-22 23:39:55
【问题描述】:
抱歉,如果这是一个基本问题,我对这种东西很陌生 我有以下代码,在方法 Kep 中,我需要以递归方式计算 50 次内部的操作,然后在 50 次迭代后返回值并打印它。 当我尝试运行它时,它说该变量在上下文中不存在。 任何建议都非常感谢
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Kepler
{
class Kepler
{
static void Main(string[] args)
{
//Variables
double M = 0; //Anomalia
double e = 0; //Excentricidad
double e0 = 0; //Excentricidad Corregida
//double E1=0;
double E0 = 0; //
Console.WriteLine("Ingresa M:");
string m = Console.ReadLine();
M = Convert.ToDouble(m);
Console.WriteLine("Ingresa e:");
string ee = Console.ReadLine();
e = Convert.ToDouble(ee);
//Calculo de e0
e0 = e * 180 / Math.PI;
Console.WriteLine("Ingresa E0:");
string EE0 = Console.ReadLine();
E0 = Convert.ToDouble(EE0);
//calculo de las funciones trigonometricas
double sin = Math.Sin((E0 * Math.PI / 180));
double cos = Math.Cos((E0 * Math.PI / 180));
int cuenta = 0;
Console.Clear();
double total = Kep(M, e, sin, cos, e0, E0, ref cuenta);
Console.WriteLine("Total=" + total);
Console.ReadLine();
}
static double Kep(double M, double e, double sin, double cos, double e0, double E0, ref int cuenta)
{
double E1 = 0;
for (cuenta = 0; cuenta <= 50; cuenta++)
{
E1 = E0 + ((M + e0 * sin - E0) / (1 - e * cos));
Console.WriteLine("E1 hasta ahora" + E1);
}
return Kep(M, e, sin, cos, e0, E1, ref cuenta);
}
}
}
【问题讨论】:
-
这可能会在您运行时崩溃,因为您递归调用相同的函数而没有任何停止它
-
谢谢大家,成功了:)
标签: c# variables recursion return