我整夜都在搜索存储系统的修复程序,以查找用户输入的内容(从循环到最高3循环)。我相信我已经找到了它,但是正如标题所说的那样,我无法在console.readline上为用户输入错误地将Type'int'隐式转换为'int []'?关于如何解决这个问题有什么建议吗?
谢谢。
//Array For Ticket prices, sales and user input
int[] TicketChoices = new int[3];
//Ticket Types
//ChildT = £1.50 = Child;
//AdultT = £2.35 = Adult;
//StudentT = £1.99 = Student;
//Film Certificate Seats Screen
//Jaws 12A 15 1
//The Exorcist 18 33 2
cw("Hello Current tickets are:");
for (int I = 0; I < 3; I++)
{
cw("ID (1) Child, £1.50");
cw("ID:(2) Adult, £2,35");
cw("ID:(3) Student £1.99");
cw("");
cw("Please Select Which ticket you would like to input By Entering it's id Number");
cw("input Must be between 1-3 for it to be vaild.");
TicketChoices = int.Parse(Console.ReadLine());
}
答案 0 :(得分:1)
int[] TicketChoices = new int[3];
TicketChoices
不是int
,而是int
TicketChoices = int.Parse(Console.ReadLine());
也许像这样
var choice = int.Parse(Console.ReadLine());
如果您不接受用户的输入,请信任他们以正确输入信息
改为使用TryParse
将数字的字符串表示形式转换为其32位带符号 等价的整数。返回值指示操作是否 成功。
答案 1 :(得分:1)
这是我想您要尝试做的:
static void Main()
{
//Array For Ticket prices, sales and user input
var ticketChoices = new int[3];
//Ticket Types
//ChildT = £1.50 = Child;
//AdultT = £2.35 = Adult;
//StudentT = £1.99 = Student;
//Film Certificate Seats Screen
//Jaws 12A 15 1
//The Exorcist 18 33 2
Console.WriteLine("Hello Current tickets are:");
for (var i = 0; i < 3; i++)
{
Console.WriteLine("ID (1) Child, £1.50");
Console.WriteLine("ID:(2) Adult, £2,35");
Console.WriteLine("ID:(3) Student £1.99");
Console.WriteLine("");
Console.WriteLine("Please Select Which ticket you would like to input By Entering it's id Number");
Console.WriteLine("input Must be between 1-3 for it to be vaild.");
var valid = false;
while (!valid)
{
var input = Console.ReadLine();
if (int.TryParse(input, out var ticketNumber))
{
if (ticketNumber >= 0 && ticketNumber <= 3)
{
valid = true;
}
}
if (valid)
{
ticketChoices[i] = ticketNumber;
}
else
{
Console.WriteLine("Please enter a value between 1 and 3");
}
}
}
// Print the results
Console.WriteLine("You entered:");
foreach (var ticketChoice in ticketChoices)
{
Console.WriteLine(ticketChoice);
}
Console.ReadLine();
}
答案 2 :(得分:0)
int.Parse
返回一个整数。因此,您尝试将TicketChoices
(数组)设置为单个整数。那行不通。
如果需要的话,可以将数组中的 first 整数设置为int.Parse
的输出:
TicketChoices[0] = int.Parse(Console.ReadLine());