我正在尝试使用拆分方法将学生与分数分开,然后如果分数好坏加上你的负100而不是输出消息。拆分方法无法将char转换为字符串
class Program
{ //Here we declare some Const variables
const int MAX = 1;
const int ZERO = 0;
const int ONE = 1;
static void Main(string[] args)
{ //here we declare the variables and the 2 arrays for the main method
int perfecto = 100;
string input;
string[] student = new string[MAX];
int[] score = new int[MAX];
//this will be the introduction for the program nice and friendly.
Console.WriteLine("Welcome to the Test score calculator!!");
Console.Write("\nPlease Input your name and your score, seperated by a space: ");
input = Console.ReadLine();
Console.WriteLine("Welcome to the Test score calculator!!");
Console.Write("\nPlease Input your name and your score, seperated by a space and Press Enter: ");
input = Console.ReadLine();
//SPLIT METHOD ACTIVATED.. here we call the split method
SplitMethod(input, ref student, ref score);
//Here we call the output
Output(student, score, perfecto);
Console.ReadLine();
}
//Here is the split method. this will take the two kinds of data and split them into 2 arrays
//the string and the int seperate so that its easyer to make calculations.
//also we referenced these arrays
static void SplitMethod(string input, ref string[] student, ref int[] score)
{
char rules = { ' ', '\r' };
string splitArray = input.Split();
//here is the actual split
student = splitArray[ZERO];
score = int.Parse(splitArray[]);
return;
}
static void Output(string[] student, int[] score, int perfecto)
{
//here is the added if statement for the perfect score scenario
if (score[i] > perfecto)
{
//here is the output incase someone scores a perfect game
Console.WriteLine("\n{0}'s score was {1}*...Congrats {2} you qualify for the TEAM!!!", student[], score[], student[]);
}
else
{
//and if they dont it displayes here.
Console.WriteLine("\nSorry {0}, {1} is not good enough. \nIm afraid you dont qualify, but keep it up!", student[], score[]);
}
}
}
}
答案 0 :(得分:1)
首先,你应该将你的角色数组声明为数组
char[] rules = { ' ', '\r' };
其次你应该将规则传递给你的分裂
string[] splitArray = input.Split(rules);
我相信你也有其他编译器错误。你应该尝试自己做一些
您还应该告诉我们您提出问题时的编译错误。
答案 1 :(得分:1)
答案 2 :(得分:0)
你的代码有很多缺陷,我纠正了它们。第一个也是最明显的是你根本不应该使用数组。即使您想重复此名称和分数输入,您也不需要数组,因为用户一次只输入一个名称和分数。这是您的代码已更改为:
class Program
{
static void Main(string[] args)
{ //here we declare the variables and the 2 arrays for the main method
int perfecto = 100;
string input;
string student = string.Empty;
int score = 0;
//this will be the introduction for the program nice and friendly.
Console.WriteLine("Welcome to the Test score calculator!!");
Console.Write("\nPlease Input your name and your score, seperated by a space and Press Enter: ");
input = Console.ReadLine();
//SPLIT METHOD ACTIVATED.. here we call the split method
SplitMethod(input, ref student, ref score);
//Here we call the output
Output(student, score, perfecto);
Console.ReadLine();
}
//Here is the split method. this will take the two kinds of data and split them into 2 arrays
//the string and the int seperate so that its easyer to make calculations.
//also we referenced these arrays
static void SplitMethod(string input, ref string student, ref int score)
{
char [] rules = { ' ', '\r' };
string [] splitArray = input.Split(rules);
//here is the actual split
if(splitArray.Length>1)
{
student = splitArray[0];
int.TryParse(splitArray[1], out score);
}
}
static void Output(string student, int score, int perfecto)
{
//here is the added if statement for the perfect score scenario
if (score > perfecto)
{
//here is the output incase someone scores a perfect game
Console.WriteLine("\n{0}'s score was {1}*...Congrats {2} you qualify for the TEAM!!!", student, score, student);
}
else
{
//and if they dont it displayes here.
Console.WriteLine("\nSorry {0}, {1} is not good enough. \nIm afraid you dont qualify, but keep it up!", student, score);
}
}
}
现在我想你要为许多输入执行此操作,直到用户输入结束,然后你应该改变你的Main方法:
static void Main(string[] args)
{ //here we declare the variables and the 2 arrays for the main method
int perfecto = 100;
string input;
string student = string.Empty;
int score = 0;
//this will be the introduction for the program nice and friendly.
Console.WriteLine("Welcome to the Test score calculator!!");
Console.Write("\nPlease Input your name and your score, seperated by a space and Press Enter, insert END if you want to finish: ");
while (!(input = Console.ReadLine()).Equals("end", StringComparison.CurrentCultureIgnoreCase))
{
SplitMethod(input, ref student, ref score);
Output(student, score, perfecto);
Console.Write("\nPlease Input your name and your score, seperated by a space and Press Enter, insert END if you want to finish: ");
}
Console.ReadLine();
}