我正在尝试返回歌曲的名称,艺术家的名称以及售出的副本数量。返回中似乎唯一可以编译的是方法名称,该名称可能不正确,因为它只会导致它成为无限循环。
还在循环中如何更改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 copySold;
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";
}
}
}
答案 0 :(得分: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
类,只需使用fist构造函数返回一个新实例:
return new Song(name, artist, records);
答案 1 :(得分:0)
编译器不会阻止创建无限循环。我认为您要返回的是一个新的Song
对象(猜测属性名称):
return new Song() {
Name = name,
Artist = artist,
Records = records
};
还在循环中如何更改tryparse,使其也不接受负数?
将tryparse
放入while
循环中,如果根据您想要的条件,分析的值“有效”,则退出循环。
答案 2 :(得分:0)
我想你有一个像这样的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;
}
答案 3 :(得分:0)
已更新为使用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";
}
}
}