您好,我在获取系统生成变量的值时遇到问题。这是从用户获取值的代码;
public void DetailsRate()
{
begin1:
Console.WriteLine("\n \t Rate the Acting on a scale of 0 to 5");
RateActing = int.Parse(Console.ReadLine());
switch (RateActing)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
Console.WriteLine("\n you have rated the action of the movie {0}", RateActing);
break;
default:
Console.WriteLine("you have selected the wrong choice {0}", RateActing);
goto begin1;
}
begin2:
Console.WriteLine("\n \t Rate the music of the movie on a scale of 0 to 5");
RateMusic = int.Parse(Console.ReadLine());
switch (RateMusic)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
Console.WriteLine("you have rated the music of the movie {0}", RateMusic);
break;
default:
Console.WriteLine("you have selected the wrong choice {0}", RateMusic);
goto begin2;
}
}
我把输入的值称为这段代码
public double getoverallRate(double rateact, double ratemus)
{
double totrate = 0;
rateact = RateActing * 0.25;
ratemus = RateMusic * 0.15;
totrate = (rateact + ratemus);
return totrate;
}
这是主要方法
static void Main(string[] args)
{
MovieRating MR = new MovieRating();
MR.DetailsRate();
MovieRating MT = new MovieRating();
double totrate = MT.getoverallRate(1, 2);
Console.WriteLine("total rate is {0}", totrate);
Console.ReadKey();
}
请问我错过了totrate的价值就是给我0.请帮助我。
答案 0 :(得分:0)
首先摆脱goto语句。快速浏览一下,你可以写下:
static void Main(string[] args)
{
double RateActing = -1;
double RateMusic = -1;
RateActing = GetMovieRating(RateActing);
RateMusic = GetMovieMusicRating(RateMusic);
double totrate = getoverallRate(RateActing, RateMusic);
Console.WriteLine("total rate is {0}", totrate);
Console.ReadKey();
}
private static double GetMovieRating(double RateActing)
{
do
{
Console.WriteLine("\n \t Rate the Acting on a scale of 0 to 5");
double.TryParse(Console.ReadLine(), out RateActing);
}
while (RateActing < 0 || RateActing > 5);
Console.WriteLine("\n you have rated the action of the movie {0}", RateActing);
return RateActing;
}
private static double GetMovieMusicRating(double RateMusic)
{
do
{
Console.WriteLine("\n \t Rate the music of the movie on a scale of 0 to 5");
double.TryParse(Console.ReadLine(), out RateMusic);
}
while (RateMusic < 0 || RateMusic > 5);
Console.WriteLine("\n you have rated the music of the movie {0}", RateMusic);
return RateMusic;
}
public static double getoverallRate(double rateact, double ratemus)
{
rateact *= 0.25;
ratemus *= 0.15;
return rateact + ratemus;
}
答案 1 :(得分:0)
这里有很多问题 - 几乎可以重新开始!
首先:永远不要使用goto
- 有更好的方法来构建程序流程。
第二:你的方法getoverallRate
占用的参数(2)少于你传入的参数(5),所以这不应该构建。
第三:你在getoverallRate
中引用了三个额外的变量,看起来它们应该是局部变量,但它们没有在任何地方定义。是否应该像Main
中的用法所暗示的那样传递。
第四:您在变量rateact
和ratemus
中传递值,但是您将立即用计算覆盖它们。
第五:如果你将用户的输入值与你需要的任何其他方法一起传递给这个方法,那对我来说会更有意义。您不应该在任何计算值并返回结果的方法中使用来自用户输入的全局变量。您应该始终传递计算所需的任何内容。
第六:MR
声明的重点是什么以及DetailsRate
做了什么?