【发布时间】:2014-11-14 07:15:53
【问题描述】:
我在同一个作业中发现了一个类似的问题,但那个人使用了不同类型的循环。我一直在努力完成这项作业,即使老师给了我伪代码试图进一步解释它,我仍然很难写出它最终应该做什么。
我们应该创建一个包含剧院门票价格的数组,然后制作一个 html 表格,其中包含与数组中的数字对应的不同级别的门票和价格。在此之后,我们提示用户输入他们的姓名并验证他们确实输入了一些内容。然后我们应该创建一个名为 numSeats 的函数,它会提示用户输入 他们想要购买的座位并验证他们输入了一个数字,并且他们在一次交易中可以购买的最大座位数是 6。
我们应该使用循环来 提示用户,直到他们输入一个有效的数字,然后创建一个名为 seatChoice 的函数,通过指示表格中的正确数字来提示用户他们希望座位的位置。
seartingChoice 还需要验证他们是否输入了数字 1-4,并使用循环提示用户,直到他们输入有效数字。如果用户在任何时候点击提示中的取消按钮,我们应该发出“抱歉你改变主意”的警报。这是我的代码中缺少的,因为我还没有弄清楚如何做到这一点。
当程序计算所有内容并最终写入屏幕时,它应该写成“UsersName 订购了 #tickets 总共 $null”,但它却写成了“Null 订购了 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 + ".");
【问题讨论】:
-
while(choice == null)||(choice>6) { ..... }认为这是一个错字,while choice=null将选择分配给 null -
除了其他人所说的之外, parseInt(choice) 不会将值分配回选择。你的意思是说 choice = parseInt(choice) 最有可能。与 seats 解析相同。
标签: javascript arrays function validation loops