学习C#,编写错误的程序,需要帮助了解为什么它';这不是我想要的功能
我不熟悉编码,编写了这个小控制台读取程序,试图理解数组、方法等。学习C#,编写错误的程序,需要帮助了解为什么它';这不是我想要的功能,c#,console,console.writeline,console.readline,C#,Console,Console.writeline,Console.readline,我不熟悉编码,编写了这个小控制台读取程序,试图理解数组、方法等。 我知道我的代码有很多错误,可能没有正确地(按书)编写。 我想得到一些关于如何修复我的程序的帮助,但它有一个问题,控制台允许我输入一个数字,但它不读取我的选择方法。 任何帮助都是非常宝贵的 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namesp
我知道我的代码有很多错误,可能没有正确地(按书)编写。
我想得到一些关于如何修复我的程序的帮助,但它有一个问题,控制台允许我输入一个数字,但它不读取我的选择方法。 任何帮助都是非常宝贵的
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两次。你的代码应该是
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();
}
}
}
你说不读我的选择方法是什么意思?对我来说,选择似乎很有效。
writeLines
的作用是什么?writeLines被设置为从Console.ReadLine返回字符串,因为如果没有它,我会遇到一个错误,即不是所有代码路径都返回值。请不要污损您的帖子,您可以要求SE将您的用户从这个问题中删除,其他用户已经花时间回答了。非常感谢!你对我的代码有什么意见吗?将来有什么需要提防的吗?请查看代码复查堆栈交换站点。