使用另一个类中的类的整数值

时间:2015-08-19 16:19:21

标签: c# class console-application

请我尝试从这个类中获取值:

class MovieRating
{
    // This block of code is to get user ratings of the movie
    public void DetailsRate()
    {
        Console.WriteLine("\n Rate the Acting on a scale of 0 to 5");
         RateActing = int.Parse(Console.ReadLine());

        Console.WriteLine("\n Rate the music of the movie on a scale of 0 to 5");
        RateMusic = int.Parse(Console.ReadLine());

        Console.WriteLine("Rate the cinematography of the movie on a scale of 0 to 5");

        Console.WriteLine("Rate the plot of the movie on a scale of 0 to 5");
        Console.WriteLine("Rate the duration of the movie on a scale of 0 to 5");
        RateDuratn = int.Parse(Console.ReadLine());

    }
}

在本课程中使用它:

class Executerating
{
    public void  overallRate()
    {
        MovieRating movrate = new MovieRating();
        int rateact = movrate.RateActing;
        int ratemus = movrate.RateMusic;
        int ratecin = movrate.RateCinema;
        int rateplot = movrate.RatePlot;
        int ratedur = movrate.RateDuratn;

        int totrate = rateact + ratemus + ratecin + rateplot + ratedur;


        Console.WriteLine("total rate is- {0}", totrate);
    }

但我发现没有价值进入班级

  

' Executerating'

请问我错过了什么?提前谢谢。

3 个答案:

答案 0 :(得分:0)

DetailsRate构造函数中转换MovieRate方法:

public MovieRate() {
    // DetailsRate code
}

或者静态调用它而不是创建实例

MovieRate.DetailsRate();

答案 1 :(得分:0)

您似乎需要在DetailsRate()中拨打overallRate(),即

...
MovieRating movrate = new MovieRating();
movrate.DetailsRate();
...

答案 2 :(得分:-1)

为您的代码添加属性,如下所示:

Class MovieRating
{
  public int RateActing { get; set; }
  public int RateMusic { get; set; }
  public int RateDuratn { get; set; }

    public void DetailsRate()
    {
        Console.WriteLine("\n Rate the Acting on a scale of 0 to 5");
         RateActing = int.Parse(Console.ReadLine());

        Console.WriteLine("\n Rate the music of the movie on a scale of 0 to 5");
        RateMusic = int.Parse(Console.ReadLine());

        Console.WriteLine("Rate the cinematography of the movie on a scale of 0 to 5");

        Console.WriteLine("Rate the plot of the movie on a scale of 0 to 5");
        Console.WriteLine("Rate the duration of the movie on a scale of 0 to 5");
        RateDuratn = int.Parse(Console.ReadLine());

    }
}