学习 C#,编写了错误的程序,需要帮助了解为什么它不能按我想要的方式运行
Learning C#, wrote faulty program, need help as to why it's not function how I want it to
我是编码新手,我编写了这个控制台阅读小程序来尝试理解数组、方法等。
我知道我的代码有很多错误并且可能没有正确编写(按书本)。
不过,我想要一些关于如何修复我的程序的帮助,它有一个问题,控制台让我输入一个数字,但它没有从我的选择方法中读取。
我们将不胜感激任何帮助。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PalacioGameImproved
{
class Program
{
static void Main(string[] args)
{
WelcomeMessage();
choices();
}
public static string WelcomeMessage()
{
string writeLines;
Console.WriteLine("Pick a DooWop");
Console.WriteLine("{0,15}", "0 = Johnny");
Console.WriteLine("{0,13}", "1 = Nick");
Console.WriteLine("{0,15}", "2 = Conrad");
Console.WriteLine("{0,14}", "3 = Diane");
Console.WriteLine("{0,13}", "4 = Rick");
writeLines = Console.ReadLine();
return writeLines;
}
public static void choices()
{
string[] names = new string[5];
names[0] = "Johnny";
names[1] = "Nick";
names[2] = "Conrad";
names[3] = "Diane";
names[4] = "Rick";
string UserInput = Console.ReadLine();
if (UserInput == "0")
{
Console.WriteLine("is it the array");
}
else if (UserInput == "1")
{
Console.WriteLine(names[1]);
}
else if (UserInput == "2")
{
Console.WriteLine(names[2]);
}
else if (UserInput == "3")
{
Console.WriteLine(names[3]);
}
else if (UserInput == "4")
{
Console.WriteLine(names[4]);
}
else
{
Console.WriteLine("That was not one of the choices, please try again.");
WelcomeMessage();
}
}
}
}
您使用了Console.ReadLine 2 次。 .net Fiddle example 你的代码应该是
using System;
public class Program
{
public static void Main()
{
WelcomeMessage();
choices();
}
public static void WelcomeMessage()
{
Console.WriteLine("Pick a DooWop");
Console.WriteLine("{0,15}", "0 = Johnny");
Console.WriteLine("{0,13}", "1 = Nick");
Console.WriteLine("{0,15}", "2 = Conrad");
Console.WriteLine("{0,14}", "3 = Diane");
Console.WriteLine("{0,13}", "4 = Rick");
}
public static void choices()
{
string[] names = new string[5];
names[0] = "Johnny";
names[1] = "Nick";
names[2] = "Conrad";
names[3] = "Diane";
names[4] = "Rick";
string UserInput = Console.ReadLine();
if (UserInput == "0")
{
Console.WriteLine("is it the array");
}
else if (UserInput == "1")
{
Console.WriteLine(names[1]);
}
else if (UserInput == "2")
{
Console.WriteLine(names[2]);
}
else if (UserInput == "3")
{
Console.WriteLine(names[3]);
}
else if (UserInput == "4")
{
Console.WriteLine(names[4]);
}
else
{
Console.WriteLine("That was not one of the choices, please try again.");
WelcomeMessage();
}
}
}
我是编码新手,我编写了这个控制台阅读小程序来尝试理解数组、方法等。
我知道我的代码有很多错误并且可能没有正确编写(按书本)。
不过,我想要一些关于如何修复我的程序的帮助,它有一个问题,控制台让我输入一个数字,但它没有从我的选择方法中读取。
我们将不胜感激任何帮助。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PalacioGameImproved
{
class Program
{
static void Main(string[] args)
{
WelcomeMessage();
choices();
}
public static string WelcomeMessage()
{
string writeLines;
Console.WriteLine("Pick a DooWop");
Console.WriteLine("{0,15}", "0 = Johnny");
Console.WriteLine("{0,13}", "1 = Nick");
Console.WriteLine("{0,15}", "2 = Conrad");
Console.WriteLine("{0,14}", "3 = Diane");
Console.WriteLine("{0,13}", "4 = Rick");
writeLines = Console.ReadLine();
return writeLines;
}
public static void choices()
{
string[] names = new string[5];
names[0] = "Johnny";
names[1] = "Nick";
names[2] = "Conrad";
names[3] = "Diane";
names[4] = "Rick";
string UserInput = Console.ReadLine();
if (UserInput == "0")
{
Console.WriteLine("is it the array");
}
else if (UserInput == "1")
{
Console.WriteLine(names[1]);
}
else if (UserInput == "2")
{
Console.WriteLine(names[2]);
}
else if (UserInput == "3")
{
Console.WriteLine(names[3]);
}
else if (UserInput == "4")
{
Console.WriteLine(names[4]);
}
else
{
Console.WriteLine("That was not one of the choices, please try again.");
WelcomeMessage();
}
}
}
}
您使用了Console.ReadLine 2 次。 .net Fiddle example 你的代码应该是
using System;
public class Program
{
public static void Main()
{
WelcomeMessage();
choices();
}
public static void WelcomeMessage()
{
Console.WriteLine("Pick a DooWop");
Console.WriteLine("{0,15}", "0 = Johnny");
Console.WriteLine("{0,13}", "1 = Nick");
Console.WriteLine("{0,15}", "2 = Conrad");
Console.WriteLine("{0,14}", "3 = Diane");
Console.WriteLine("{0,13}", "4 = Rick");
}
public static void choices()
{
string[] names = new string[5];
names[0] = "Johnny";
names[1] = "Nick";
names[2] = "Conrad";
names[3] = "Diane";
names[4] = "Rick";
string UserInput = Console.ReadLine();
if (UserInput == "0")
{
Console.WriteLine("is it the array");
}
else if (UserInput == "1")
{
Console.WriteLine(names[1]);
}
else if (UserInput == "2")
{
Console.WriteLine(names[2]);
}
else if (UserInput == "3")
{
Console.WriteLine(names[3]);
}
else if (UserInput == "4")
{
Console.WriteLine(names[4]);
}
else
{
Console.WriteLine("That was not one of the choices, please try again.");
WelcomeMessage();
}
}
}