在另一个 class 中使用 class 中的整数值

Using an integer value from a class in another class

拜托,我正在尝试从中获取值 class:

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中使用它:

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);
    }

但是我发现没有值进入 class

'Executerating'

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

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

public MovieRate() {
    // DetailsRate code
}

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

MovieRate.DetailsRate();

按如下方式将属性添加到您的代码中:

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());

    }
}

看来您需要在 overallRate() 中调用 DetailsRate(),即

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