C# 如何使用另一个程序中的字符串
C# how to use an string from an program in another
我正在练习一些代码。
我有这个代码
class 计划
{
static void Main(string[] args)
{
Program a = new Program();
a.Alalao();
Program b = new Program();
b.Game();
}
public void Alalao()
{
Console.WriteLine("Hello my dear. How would you like to be called?");
string n1 = Console.ReadLine();
// more code
}
public void Game()
{
Console.WriteLine("This is a game where you have answer questions. For each correct answer 1 get an point and for each wrong answer you lose a point.");
Console.WriteLine("You will start with 5 points. Consider it as a courtesy");
Console.WriteLine("{0}, are you ready start?", n1);
}
}
如何将 Alalao() 中的字符串 n1 放入 Game() 中的最后一个命令 Console.writeline
提前致谢。
有多种可能的方法。一种解决方案是使用 arguments/return 值。
这就是结构化编程的全部内容,您将一个较大的问题分成较小的块(方法),但您仍然使用参数和 return 值来传达这些块。
static void Main(string[] args)
{
Program a = new Program();
var name = a.Alalao();
Program b = new Program();
b.Game(name);
}
public string Alalao()
{
Console.WriteLine("Hello my dear. How would you like to be called?");
string n1 = Console.ReadLine();
// more code
return n1;
}
public void Game(string n1)
{
Console.WriteLine("This is a game where you have answer questions. For each correct answer 1 get an point and for each wrong answer you lose a point.");
Console.WriteLine("You will start with 5 points. Consider it as a courtesy");
Console.WriteLine("{0}, are you ready start?", n1);
}
Return n1 来自Alalao 并将其作为参数添加到Game。像这样...
public string Alaloa () {
// ...
string n1 = Console.ReadLine();
// ...
return n1;
}
// ...
public void Game (string inString) {
// ..
Console.WriteLine("{0}, are you ready start?", inString);
}
然后您需要将 Alaloa 中的 return 值保存为变量并将其添加到主方法中的 Game 调用中,如下所示...
static void Main (string[] args) {
Program a = new Program();
string myString = a.Alaloa(); // Saving the string to a variable
Program b = new Program();
b.Game(myString);
}
我正在练习一些代码。
我有这个代码
class 计划
{
static void Main(string[] args)
{
Program a = new Program();
a.Alalao();
Program b = new Program();
b.Game();
}
public void Alalao()
{
Console.WriteLine("Hello my dear. How would you like to be called?");
string n1 = Console.ReadLine();
// more code
}
public void Game()
{
Console.WriteLine("This is a game where you have answer questions. For each correct answer 1 get an point and for each wrong answer you lose a point.");
Console.WriteLine("You will start with 5 points. Consider it as a courtesy");
Console.WriteLine("{0}, are you ready start?", n1);
}
}
如何将 Alalao() 中的字符串 n1 放入 Game() 中的最后一个命令 Console.writeline
提前致谢。
有多种可能的方法。一种解决方案是使用 arguments/return 值。
这就是结构化编程的全部内容,您将一个较大的问题分成较小的块(方法),但您仍然使用参数和 return 值来传达这些块。
static void Main(string[] args)
{
Program a = new Program();
var name = a.Alalao();
Program b = new Program();
b.Game(name);
}
public string Alalao()
{
Console.WriteLine("Hello my dear. How would you like to be called?");
string n1 = Console.ReadLine();
// more code
return n1;
}
public void Game(string n1)
{
Console.WriteLine("This is a game where you have answer questions. For each correct answer 1 get an point and for each wrong answer you lose a point.");
Console.WriteLine("You will start with 5 points. Consider it as a courtesy");
Console.WriteLine("{0}, are you ready start?", n1);
}
Return n1 来自Alalao 并将其作为参数添加到Game。像这样...
public string Alaloa () {
// ...
string n1 = Console.ReadLine();
// ...
return n1;
}
// ...
public void Game (string inString) {
// ..
Console.WriteLine("{0}, are you ready start?", inString);
}
然后您需要将 Alaloa 中的 return 值保存为变量并将其添加到主方法中的 Game 调用中,如下所示...
static void Main (string[] args) {
Program a = new Program();
string myString = a.Alaloa(); // Saving the string to a variable
Program b = new Program();
b.Game(myString);
}