【问题标题】:What do i return in this method c#我在这个方法c#中返回什么
【发布时间】:2019-06-26 06:11:38
【问题描述】:

我正在尝试返回歌曲名称、艺术家姓名和销量。唯一似乎在返回中编译的是方法名称,它不可能是正确的,因为它只会导致它成为一个无限循环。

同样在循环中,如何更改 tryparse 使其也不接受负数?

代码如下

namespace Songs
{
    class Program
    {
        static void Main(string[] args)
        {
            object [] ArrayOfSongs;
            ArrayOfSongs = new object[4];

            for (int i = 4; i < ArrayOfSongs.Length; i++)
            {
                ArrayOfSongs[i] = InputSongDetails();
                var store = InputSongDetails();
            }

            Console.WriteLine("Enter an artists name, or press return for all artists");
        }

        static Song InputSongDetails()
        {
            Console.WriteLine("What is the name of your song");
            string name = Console.ReadLine();

            Console.WriteLine("What is the artists name");
            string artist = Console.ReadLine();

            int records;
            Console.WriteLine("How many records did it sell");
            while (!int.TryParse(Console.ReadLine(), out records))
            {
                Console.WriteLine("That is not valid please enter a number");
            }
            string returns = $"Your song is{name}, the artists name is {artist} and it sold {records} records";
            return InputSongDetails();
        }
    }
}

这是我的歌曲课

namespace Songs

{ 班歌 { 字符串名称; 弦乐艺术家; int 副本已售出;

    public Song(string name, string artist, int copiesSold)
    {
        this.name = name;
        this.artist = artist;
        this.copiesSold = copiesSold;
    }

    public Song()
    {
    }

    public string GetArtist()
    {
        return artist;
    }

    public string GetDetails()
    {
        return $"Name: {name} Artist: {artist} Copies Sold: {copiesSold},";
    }

    public string GetCertification()
    {
        if (copiesSold<200000)
        {
            return null;
        }
        if (copiesSold<400000)
        {
            return "Silver";
        }
        if (copiesSold<600000)
        {
            return "gold";
        }
        return "Platinum";  
    }
}

}

【问题讨论】:

  • 有一个TryParse 需要一个NumberStylesNumberStyles 包括启用/禁用负片的能力...所以...NumberStyles.Integer &amp; ~NumberStyles.AllowLeadingSign 应该可以工作...
  • 歌曲课在哪里?
  • 或更简单的while (!int.TryParse(Console.ReadLine(), out records) || records &lt; 0)
  • for循环永远无法运行,因为数组的长度是4..
  • 哇!不错的收获@NineBerry

标签: c#


【解决方案1】:

根据您的方法签名,您将返回类似于 Song 的内容:

static Song InputSongDetails()

理想情况下,您将在某处定义一个名为 Song 的类,如下所示:

class Song
{
    public string Name { get; set; }
    public string Artist { get; set; }
    public int Records { get; set; }
}

所以,你的回报应该是这样的:

return new Song
{
    Name = name,
    Artist = artist,
    Records = records
};

对于您的循环,只需在 while 子句中添加一个附加条件:

while (!int.TryParse(Console.ReadLine(), out records) || records < 0)

更新:

根据您新发现的Song 类,只需使用第一个构造函数返回一个新实例:

return new Song(name, artist, records);

【讨论】:

  • 嗯,从技术上讲,签名不需要一个名为 Song(或一个实现名为 Song 的接口的类对于这个问题)。 Song 也可以实现为 struct。但就实际建议而言,我同意将 Song 设为一个类...
  • @preciousbetine,为什么不呢?
  • @preciousbetine 如果我记得正确声明records 变量...现在修复!
  • 这个条件records &lt; 0应该在循环体中检查。
  • @preciousbetine,不,没有必要在体内这样做。无论您喜欢这种风格还是那种风格都不是重点。你声称它不起作用。但确实如此。
【解决方案2】:

编译器不会阻止您创建无限循环。我认为你想要返回的是一个新的Song 对象(猜测属性名称):

  return new Song() {
      Name = name,
      Artist = artist,
      Records = records
  };

同样在循环中,如何更改 tryparse 使其也不接受负数?

tryparse 放入while 循环中,如果根据您想要的任何条件解析的值“有效”,则该循环将退出。

【讨论】:

    【解决方案3】:

    我想你有一个像这样的 Song 类

    public class Song {
        public string Name {get; set; }
        public string Artist {get;set; }
        public int Records {get;set; }
    }
    

    然后你必须在你的 InputSongDetails 方法中返回一个新的 Song 对象

    static Song InputSongDetails()
    {
        var song = new Song();
        Console.WriteLine("What is the name of your song");
        song.Name = Console.ReadLine();
    
        Console.WriteLine("What is the artists name");
        song.Artist = Console.ReadLine();
    
        int records;
        Console.WriteLine("How many records did it sell");
        while (!int.TryParse(Console.ReadLine(), out records))
        {
            Console.WriteLine("That is not valid please enter a number");
        }
        song.Records = records;
        string returns = String.Format("Your song is{0}, the artists name is {1} and it sold {2} records", song.Name, song.Artist, song.Records);
    
        return song;
    }   
    

    【讨论】:

      【解决方案4】:

      更新为使用 op 的类

      using System;
      
      
      namespace Songs
      {
      class Program
      {
          static void Main(string[] args)
          {
              Song[] ArrayOfSongs = new Song[4];
      
              for (var i = 0; i < ArrayOfSongs.Length; i++)
              {
      
                  ArrayOfSongs[i] = InputSongDetails();
      
              }
      
              Console.ReadLine();
          }
      
          static Song InputSongDetails()
          {
              //Song returnObj = new ExpandoObject();
              Console.WriteLine("Enter an artists name, or press return for all artists");
              Console.WriteLine("What is the name of your song");
              string name = Console.ReadLine();
             // returnObj.name = name;
              Console.WriteLine("What is the artists name");
              string artist = Console.ReadLine();
              //returnObj.artist = artist;
              Console.WriteLine("How many records did it sell");
              string recordsStr = Console.ReadLine();
              int records;
              while (!Int32.TryParse(recordsStr, out records) || records < 0)
              {
                  Console.WriteLine("try again");
                  recordsStr = Console.ReadLine();
              }
              //returnObj.records = records;
              Console.WriteLine($"Your song is{name}, the artists name is {artist} and it sold {records.ToString()} records");
              return new Song(name,artist,records);
      
          }
      }
      
       class Song
      {
          string name; string artist; int copiesSold;
      
          public Song(string name, string artist, int copiesSold)
          {
              this.name = name;
              this.artist = artist;
              this.copiesSold = copiesSold;
          }
      
          public Song()
          {
          }
      
          public string GetArtist()
          {
              return artist;
          }
      
          public string GetDetails()
          {
              return $"Name: {name} Artist: {artist} Copies Sold: {copiesSold},";
          }
      
          public string GetCertification()
          {
              if (copiesSold < 200000)
              {
                  return null;
              }
              if (copiesSold < 400000)
              {
                  return "Silver";
              }
              if (copiesSold < 600000)
              {
                  return "gold";
              }
              return "Platinum";
          }
      }
      
      }
      

      【讨论】:

        猜你喜欢
        • 2021-05-12
        • 2013-10-14
        • 2020-10-22
        • 2020-03-06
        • 2019-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-17
        相关资源
        最近更新 更多