我试图了解方法的工作原理,我以为我已经失望了,但似乎我不喜欢我的RunSelect();。方法没有达到我的期望。在第19行,我要求用户选择值(1或2)并将其作为int返回(运行)。然后在第25行上,根据所选的int执行if / if else / else语句。无论选择了什么,都不会识别int并要求用户再次尝试-不管我输入什么内容,都无法识别它,因此我的控制台方法失败了,而且我不确定自己在做什么。
我尝试跟踪我的代码,将其打印出来进行分析,并花了一夜时间在上面睡觉。等。我迷失了为什么它不起作用。我是新手,非常感谢您的帮助。我不认为问题出在我的循环或方法上,我认为这是我将int传递给if语句的方式。但是我迷失了为什么它没有像我想的那样起作用。
谢谢大家的帮助
这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace a032_Factorial_Fibonacci_Sequencer
{
class Program
{
String command = "";
//Need to get the runSelect to work as method
public void Play()
{
Announcer("+ + + Welcome To MegaCorps Factorial Fibioci Sequencer + + +\n\n");
int run = 0;
do
{
RunSelect(run);
if (run == 1) { RunFact(); }
else if (run == 2) { RunFib(); }
else
{
Announcer("Non Valid Selection");
RunSelect(run);
}
Announcer("Enter 'Y' to Run another sequence? ");
command = Console.ReadLine().ToLower().Trim();
}
while (command == "y" || command == "yes");
}
//HelperMethods
public String Announcer(String strTxt)
{
Console.WriteLine(strTxt);
return strTxt;
}
public int RunSelect(int run)
{
Announcer("Enter '1' to run Factor | Enter '2' to run Fibioci");
run = int.Parse(Console.ReadLine());
return run;
}
public void Closer()
{ Console.Read(); }
public void RunFact()
{
//craft program to factor a number entered and return factor
//use double not int to handle factored numbers larger then 57+
Console.WriteLine("Factorial Sequencer Entered/n/n");
Announcer("Enter A Number to FACTOR: ");
double num = double.Parse(Console.ReadLine());
double numNew = 1;
for (int i = 1; i <= num; i++)
{ numNew = numNew * i; }
Announcer("\nFACTORED result: " + numNew);
}
public void RunFib()
{
//craft program to fib a number entered and return fib
//use double not int to handle factored numbers larger then 57+
Console.WriteLine("Fibioci Sequencer Entered\n");
Announcer("Enter A Number to FIBIOC: ");
double iSequence = double.Parse(Console.ReadLine());
Announcer("\nFIBIOC result: ");
double iPrevious = -1;
double iNext = 1;
for (int i = 1; i <= iSequence; i++)
{
double iSum = iNext + iPrevious;
iPrevious = iNext;
iNext = iSum;
Console.Write(iNext + ", ");
}
}
static void Main(string[] args)
{
Program myProgram = new Program();
myProgram.Play();
}
}
}
问题:您没有存储方法的返回值RunSelect()
。
解决方案:您需要存储RunSelect()
Method的返回值,否则变量run
值将不会被修改(即使在调用RunSelect()
method之后仍然为零)。
替换为:
RunSelect(run);
有了这个:
run=RunSelect(run);
编辑:如果您正在调用返回某项内容的方法,则您需要读取/存储该return
方法的值,因为它包含修改后的值。
步骤1:在代码中,您已run
使用以下语句初始化了变量:
int run = 0;
第2步:在内部do-while
循环中,您调用RunSelect()
了以下方法:
do
{
RunSelect(run);
------
步骤3:在该方法中,RunSelect()
您将使用以下语句为run
变量分配实际的用户输入console
:
注意:这里RunSelect()
方法中的运行变量是该方法的局部变量,因此即使您为其赋值,run
也不会反映到函数中run
声明的变量中Play()
。
public int RunSelect(int run)
{
Announcer("Enter '1' to run Factor | Enter '2' to run Fibioci");
run = int.Parse(Console.ReadLine());/here you are assigning userinput to run variable.
return run;
}
步骤4:您将修改后的变量值发送回run
此方法的调用方(RunSelect()
):
步骤5:您不会再将方法return
发送的值RunSelect()
再次存储到run
变量中,如下所示:
RunSelect(run);
因此仍然run
变量将具有初始化值zero
。
要使其正常工作,您只需存储方法的返回值,RunSelect()
如下所示:
run=RunSelect(run);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句