对于同一个赋值,我发现了一个类似的问题,但该人员正在使用不同类型的循环。我一直在努力完成这项任务,甚至在老师给我伪代码以进一步解释它的时候,我仍然很难写出它到底应该做什么。具有循环分配的Javascript函数
我们应该创建一个包含剧场门票价格的数组,然后制作一个具有与数组中数字对应的不同门票和价格水平的html表。在此之后,我们提示用户输入他们的名字并确认他们输入了一些东西。然后,我们应该创建一个名为numSeats的函数,用于提示用户想要购买的座位数 ,并确认他们输入了一个数字,并且他们在一次交易中可以购买的座位的最大数量为6.
我们应该使用一个循环 提示用户,直到他们输入一个有效的数字,然后创建一个名为seatingChoice的函数,通过从表中指示正确的编号,提示用户他们想要座位的位置。
seartingChoice还需要验证他们是否输入了数字1-4并使用循环提示用户,直到他们输入有效数字。如果用户随时在提示中点击取消按钮,我们应该发出“抱歉改变了主意”的提醒。这是从我的代码中缺少,因为我还没有想出如何做到这一点。
当程序计算出所有内容并最终写入屏幕时,它应该写为“UsersName命令#tickets总数为dollaramt”,但是它会写入“Null命令空票据总数为$ null。 “以下是我的代码的JavaScript部分:
var Prices = [60, 50, 40, 30];
var Usersname = prompt("Please enter your name");
while(Usersname = null)
{
Usersname = prompt("Please enter your name");
}
function numSeats() {
var seats = prompt("Please enter the number of seats you want to buy");
parseInt(seats);
while((seats = null)||(seats > 6))
{
seats = prompt("Please enter a number between 1 and 6");
parseInt(seats);
}
return seats;
}
var seatswanted = numSeats();
function seatingChoice() {
var choice = prompt("Please enter where you would like your seats to be located by indicating the correct number from the table");
parseInt(choice)
while((choice = null)||(choice > 4))
{
choice = prompt("Please enter a number between 1 and 4, corresponding to your section choice");
parseInt(choice);
}
return choice;
}
var seating = seatingChoice();
var dollaramt = (seatswanted * Prices[seating-1]);
document.write(Usersname + " ordered " + seatswanted + "tickets for a total of " + "$" + dollaramt + ".");
'而(选择== NULL)||(选择> 6) { ..... } ' 认为它是一个错字, '而选择= null'将选项赋值为null – Tejesh
除了其他人说的之外,parseInt(选择)不会将值分配回选项。你打算说** choice = parseInt(选择)**最有可能。与**席位**解析一样。 –