C# 在这个方法c中我返回什么#

C# 在这个方法c中我返回什么#,c#,C#,我试图返回歌曲的名字,艺术家的名字和售出的数量。在返回中似乎要编译的唯一东西是方法名,它不可能是正确的,因为它只会导致它成为一个无限循环 同样在循环中,如何更改tryparse,使其也不接受负数 代码如下 名称空间歌曲 { 班级计划 { 静态void Main(字符串[]参数) { 对象[]数组歌曲; ArrayOfSongs=新对象[4]; for(int i=4;i

我试图返回歌曲的名字,艺术家的名字和售出的数量。在返回中似乎要编译的唯一东西是方法名,它不可能是正确的,因为它只会导致它成为一个无限循环

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

代码如下

名称空间歌曲
{
班级计划
{
静态void Main(字符串[]参数)
{
对象[]数组歌曲;
ArrayOfSongs=新对象[4];
for(int i=4;i
这是我的歌曲课

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";  
    }
}
公共歌曲(字符串名称、字符串艺术家、int copiesSold)
{
this.name=名称;
这个艺术家=艺术家;
this.copiesSold=copiesSold;
}
公歌()
{
}
公共字符串GetArtist()
{
回归艺术家;
}
公共字符串GetDetails()
{
return$“Name:{Name}艺术家:{Artist}已售出的拷贝:{copiesSold},”;
}
公共字符串GetCertification()
{

如果(copiesSold根据您的方法签名,您返回的是类似于
歌曲的内容

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
};
因此,您的报税表应该如下所示:

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
对象(猜测属性名称):

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


tryparse
放入
while
循环中,如果解析的值基于您想要的任何标准是“有效的”,则该循环将退出。

我想您有这样一个歌曲类

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

更新为使用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";
    }
}

}
使用系统;
名称空间歌曲
{
班级计划
{
静态void Main(字符串[]参数)
{
歌曲[]ArrayOfSongs=新歌[4];
对于(变量i=0;i
有一个
TryParse
采用
NumberStyles
NumberStyles
包括启用/禁用负片的功能…所以…
NumberStyles.Integer&~NumberStyles.AllowLeadingSign
应该可以工作…歌曲类在哪里?或者更简单的
,而(!int.TryParse(Console.ReadLine(),out records)| | records<0)
for循环永远无法运行,因为4是数组的长度。.哇!nice catch@NineBerryWell,从技术上讲,签名不需要名为Song的类(或者实现名为Song的接口的类).Song也可以实现为一个结构。但在实际建议方面,我同意将Song作为一个类…@preciousbetine,为什么不?@preciousbetine如果我记得正确声明
记录
变量,我会记得的…现在修复了!这个条件
记录<0
shou