我正在自己学习C# - 这不是家庭作业。我只是在第7章,所以我希望得到一个简单/基本的答案。
我遇到麻烦从Main调用TalentListing方法,从另一个方法获取其参数。我是否必须在Main方法中复制它们?
我意识到方法是无效的 - 我不需要它们返回任何东西,我只需要它们就可以运行。
对不起,代码很长,我认为最好全部展示。
谢谢!
class Program
{
static void Main()
{
int pastContestants;
int currentContestants;
// Call Methods:
Console.Write("LAST YEAR'S TALENT: ");
pastContestants = Contestants();
Console.Write("THIS YEAR'S TALENT: ");
currentContestants = Contestants();
Overview(pastContestants, currentContestants);
CompetitorTalents(currentContestants);
// need to call the TalentListing Method...
TalentListing(CompetitorTalents(contestantNames), CompetitorTalents(validTalentCodes), CompetitorTalents(contestantTalentCodes);
Console.ReadLine();
}
public static int Contestants()
{ // Get & returns valid # of contestants. Called twice - last year & this year
int contestants = 0; // holds return value
const int MIN = 0; // will allow 0 as an answer
const int MAX = 30; // will allow 30 as an answer
Console.Write("Please enter the number of contestants: ");
contestants = Int32.Parse(Console.ReadLine());
while (contestants < MIN || contestants > MAX)
{
Console.Write("Invalid number. Please enter a number between 0 and 30, inclusive: ");
contestants = Int32.Parse(Console.ReadLine());
}
return contestants;
}
public static void Overview(int past, int current)
{ // Accepts contestant - past & current. Displays 1 of 3 messages.
double entranceFee = 25.00;
Console.WriteLine("\nWe had {0} contestants last year and have {1} contestants this year.", past,
current);
//if more than double
if (current > (past * 2))
Console.WriteLine("The competition is more than twice as big this year!");
//if bigger but no more than double
if (current > past && current < (past * 2))
Console.WriteLine("The competition is bigger than ever!");
//if smaller than last year
if (current < past)
Console.WriteLine("A tighter race this year. Come out and cast your vote!");
Console.WriteLine("\nThe revenue expected for this year is {0:C}.", current * entranceFee);
}
public static void CompetitorTalents(int current)
{ // Fill array of competitors and their talent codes.
string[] contestantNames;
string contestantNameEntered;// user entry
char[] contestantTalentCodes;
char talentCodeEntered; // user entry
string[] talents = { "Dancing", "Musical", "Other" };
char[] validTalentCodes = { 'D', 'M', 'O' };
int[] total = new int[talents.Length];
bool validCode = false;
int counter = 0;
contestantNames = new string[current]; // set array size
contestantTalentCodes = new char[current]; // set array size
// put contestants in array
while (counter < current)// loop for all contestants
{
// get contenstants name and put in array
Console.Write("Please enter the name of contestant: ");
contestantNameEntered = Console.ReadLine();
counter += 1; // contestant number
validCode = false;
//place in correct array element
contestantNames[counter - 1] = contestantNameEntered;
// get contestants talent code, verify and place in array
while (validCode == false) // reset per contestant
{
Console.Write("Please enter the contestant's talent code (D=Dancing, M=Musical, O=Other): ");
talentCodeEntered = Char.Parse(Console.ReadLine().ToUpper()); // convert to uppercase
for (int x = 0; x < validTalentCodes.Length && !validCode; ++x)
if (talentCodeEntered == validTalentCodes[x]) // talent code valid?
{
validCode = true; // true if match found
contestantTalentCodes[counter - 1] = talentCodeEntered; //put talent code in array
x = validTalentCodes.Length; // breaks out of loop
}
if (validCode == false) // false if no match found = invalid code.
{
Console.WriteLine("Invalid code talent Code.");
}
}
}
// search all elements of validTalentCodes array (D, M, O), count instances in contestantTalentCodes array
for (int z = 0; z < validTalentCodes.Length; z++) // validTalentCodes
for (int x = 0; x < contestantTalentCodes.Length; x++) // contestantTalentCodes
if (contestantTalentCodes[x] == validTalentCodes[z])
{
total[z]++;
}
for (int x = 0; x < talents.Length; ++x)
Console.WriteLine("Total contenstants for {0} is {1}.", talents[x], total[x]);
}
public static void TalentListing(string contestantNames, char [] validTalentCodes, char [] contestantTalentCodes)
{ // Continuously prompt for talent codes and display contestants with the corresponding talent until quit.
// What talent code would you like to see (or QUIT)?
const char QUIT = 'Q';// must be upper as char are converted!!
char userOption = ' ';
bool validCode = false;
while (userOption != QUIT)
{
Console.Write("\nWhat talent code would you like to view? (D, M, O or Q to quit): ");
validCode = false; // reset from previous section
userOption = Char.Parse(Console.ReadLine().ToUpper());
for (int x = 0; x < contestantNames.Length && !validCode; ++x)
{
if (userOption == validTalentCodes[x]) // valid talent code?
{
validCode = true;
Console.Write("The contestants in {0} talent are: ", validTalentCodes[x]);
// display list of contestants with that code
for (int z = 0; z < contestantTalentCodes.Length; z++) // validTalentCodes
if (contestantTalentCodes[z] == userOption)
{
Console.Write("\n{0}", contestantNames[z]);
}
}
}
if (validCode == false)
Console.WriteLine("Invalid talent code!");
}
Console.WriteLine(); // when QUIT is selected
}
}
答案 0 :(得分:2)
CompetitorTalents(int current)
方法没有返回值,您正在使用void
。 TalentListing
收到两个string
个。所以你需要从某个地方获取这两个。如果您希望它们来自CompetitorTalens
方法,则必须更改它以使其返回string
并实际返回其中的内容。
答案 1 :(得分:1)
对于这个简单的应用程序,使用静态全局变量。只需复制此代码并查看即可。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
public static string[] talents = { "Dancing", "Musical", "Other" };
public static char[] validTalentCodes = { 'D', 'M', 'O' };
public static string[] contestantNames;
public static char[] contestantTalentCodes;
static void Main()
{
int pastContestants;
int currentContestants;
// Call Methods:
Console.Write("LAST YEAR'S TALENT: ");
pastContestants = Contestants();
Console.Write("THIS YEAR'S TALENT: ");
currentContestants = Contestants();
Overview(pastContestants, currentContestants);
CompetitorTalents(currentContestants);
// need to call the TalentListing Method...
TalentListing();
Console.ReadLine();
}
public static int Contestants()
{ // Get & returns valid # of contestants. Called twice - last year & this year
int contestants = 0; // holds return value
const int MIN = 0; // will allow 0 as an answer
const int MAX = 30; // will allow 30 as an answer
Console.Write("Please enter the number of contestants: ");
contestants = Int32.Parse(Console.ReadLine());
while (contestants < MIN || contestants > MAX)
{
Console.Write("Invalid number. Please enter a number between 0 and 30, inclusive: ");
contestants = Int32.Parse(Console.ReadLine());
}
return contestants;
}
public static void Overview(int past, int current)
{ // Accepts contestant - past & current. Displays 1 of 3 messages.
double entranceFee = 25.00;
Console.WriteLine("\nWe had {0} contestants last year and have {1} contestants this year.", past,
current);
//if more than double
if (current > (past * 2))
Console.WriteLine("The competition is more than twice as big this year!");
//if bigger but no more than double
if (current > past && current < (past * 2))
Console.WriteLine("The competition is bigger than ever!");
//if smaller than last year
if (current < past)
Console.WriteLine("A tighter race this year. Come out and cast your vote!");
Console.WriteLine("\nThe revenue expected for this year is {0:C}.", current * entranceFee);
}
public static void CompetitorTalents(int current)
{ // Fill array of competitors and their talent codes.
string contestantNameEntered;// user entry
char talentCodeEntered; // user entry
int[] total = new int[talents.Length];
bool validCode = false;
int counter = 0;
contestantNames = new string[current]; // set array size
contestantTalentCodes = new char[current]; // set array size
// put contestants in array
while (counter < current)// loop for all contestants
{
// get contenstants name and put in array
Console.Write("Please enter the name of contestant: ");
contestantNameEntered = Console.ReadLine();
counter += 1; // contestant number
validCode = false;
//place in correct array element
contestantNames[counter - 1] = contestantNameEntered;
// get contestants talent code, verify and place in array
while (validCode == false) // reset per contestant
{
Console.Write("Please enter the contestant's talent code (D=Dancing, M=Musical, O=Other): ");
talentCodeEntered = Char.Parse(Console.ReadLine().ToUpper()); // convert to uppercase
for (int x = 0; x < validTalentCodes.Length && !validCode; ++x)
if (talentCodeEntered == validTalentCodes[x]) // talent code valid?
{
validCode = true; // true if match found
contestantTalentCodes[counter - 1] = talentCodeEntered; //put talent code in array
x = validTalentCodes.Length; // breaks out of loop
}
if (validCode == false) // false if no match found = invalid code.
{
Console.WriteLine("Invalid code talent Code.");
}
}
}
// search all elements of validTalentCodes array (D, M, O), count instances in contestantTalentCodes array
for (int z = 0; z < validTalentCodes.Length; z++) // validTalentCodes
for (int x = 0; x < contestantTalentCodes.Length; x++) // contestantTalentCodes
if (contestantTalentCodes[x] == validTalentCodes[z])
{
total[z]++;
}
for (int x = 0; x < talents.Length; ++x)
Console.WriteLine("Total contenstants for {0} is {1}.", talents[x], total[x]);
return ;
}
/// Continuously prompt for talent codes and display contestants with the corresponding talent until quit.
/// What talent code would you like to see (or QUIT)?
public static void TalentListing()
{
const char QUIT = 'Q';// must be upper as char are converted!!
char userOption = ' ';
bool validCode = false;
while (userOption != QUIT)
{
Console.Write("\nWhat talent code would you like to view? (D, M, O or Q to quit): ");
validCode = false; // reset from previous section
userOption = Char.Parse(Console.ReadLine().ToUpper());
for (int x = 0; x < contestantNames.Length && !validCode; ++x)
{
if (userOption == validTalentCodes[x]) // valid talent code?
{
validCode = true;
Console.Write("The contestants in {0} talent are: ", validTalentCodes[x]);
// display list of contestants with that code
for (int z = 0; z < contestantTalentCodes.Length; z++) // validTalentCodes
if (contestantTalentCodes[z] == userOption)
{
Console.Write("\n{0}", contestantNames[z]);
}
}
}
if (validCode == false)
Console.WriteLine("Invalid talent code!");
}
Console.WriteLine(); // when QUIT is selected
}
}
}
答案 2 :(得分:0)
你需要在最后关闭方法调用(所以你错过了一个):
TalentListing(CompetitorTalents(contestantNames), CompetitorTalents(validTalentCodes), CompetitorTalents(contestantTalentCodes));
您调用的方法无效,意味着它们不会返回任何内容:
public static string CompetitorTalents(int current)
然后在竞争对手的方法调用中,您应该传递方法需要的内容。我建议在不同的方面这样做:
string contestantNames = CompetitorTalents(currentContestants);
依此类推,然后在TalentListing中你可以引用各个变量:
TalentListing(contestantNames, validTalentCodes, contestantTalentCodes);
答案 3 :(得分:0)
好吧,看看您的方法签名,我们可以看到以下内容:
void TalentListing(string,char [],char [])
void CompetitorTalents(int)
您的CompetitorTalents
方法返回void,但您尝试将该方法的结果作为TalentListing
方法的参数传递。因此,您实际上只是想要TalentListing(void, void, void)
,当它真正需要string
,char[]
,然后是另一个char[]