2014-02-28 24 views
0
while(userInput!= -1) 
{ 
    var total=0.0; 
    var totalEarnings=0; 
    var item; 
    //var userInput; 
    var salesPerson=1; 
    var userInput= parseInt(prompt("outsidePlease enter noOfItemsSold of Item# 1 sold for SalesPerson #"+salesPerson+"\n"+" OR -1 to Exit ")); 
    salesPerson++; 
    //alert("in while "+salesPerson); 
    //userInput= parseInt(prompt("Please enter noOfItemsSold of Item# "+item+" sold for SalesPerson #"+salesPerson+"\n"+" OR -1 to Exit ")); 
    //if(userInput!=-1) 
    //{ 
    for(item=2;item<5;item++) 
    { 
     //userInput= parseInt(prompt("Please enter noOfItemsSold of Item# "+item+" sold for SalesPerson #"+salesPerson+"\n"+" OR -1 to Exit ")); 

     //var noOfItemsSold = parseInt(userInput); 

     if (item == 1) 
     { 
      total += userInput * 239.99; 
     } 
     else if (item == 2) 
     { 
      total += userInput * 129.75; 
     } 
     else if (item == 3) 
     { 
      total += userInput * 99.95; 
     } 
     else if (item == 4) 
     { 
      total += userInput * 350.89; 
     } 
     var input= parseInt(prompt("Please enter noOfItemsSold of Item# "+item+" sold for SalesPerson #"+salesPerson+"\n"+" OR -1 to Exit ")); 
    } 
    totalEarnings = .09 * total + 200; 
    document.writeln("<h1>SalesPerson#"+salesPerson+" Total Earnings this week is " +totalEarnings +"</h1>"); 
} 

我试图运行连续循环。脚本正在计算销售人员的总收入,我想运行脚本直到用户输入-1。 我不知道我在做什么错,但这是行不通的。 我该如何解决这个问题?如何在javascript中运行连续循环

+1

我想你可能需要在while循环之外实例化userInput,而不是在它之内。 – Bob

+2

当你说*这不工作*,你能更具体吗? –

+0

@Bob - 我认为可变提升实际上会让这个工作,但我同意它马虎。 –

回答

0
while (1) { 


    if (condition met) { 
     break; 
    } 
} 

在您的条件测试(插入您的条件为真)之前,您的循环将始终为真,然后它会中断并继续执行其余代码。

+0

这正是'while(condition)'*做的*。您刚刚将条件测试移至'while'块的底部,而不是在下一个循环的开始处评估它。这会起作用,但它不会带来任何好处,并会降低可读性。海报的根本问题是他并没有首先测试正确的条件。 – greenie2600

0

基本上,只有在循环迭代完成之后才会评估条件。因此,如果用户键入-1,那么for循环仍将被执行,这可能不是您想要的。

我建议使用break;关键字来退出循环。尝试是这样的:

while(true) // Loop forever, since we'll break manually 
{ 
    var total = 0.0; 
    var totalEarnings = 0; 
    var item; 
    var salesPerson = 1; 
    var userInput = parseInt(prompt("outsidePlease enter noOfItemsSold of Item# 1 sold for SalesPerson #"+salesPerson+"\n"+" OR -1 to Exit ")); 
    salesPerson++; 

    if(userInput == -1) // Exit immediately! 
    break; 

    for(item=2;item<5;item++) 
    { 
    // ... 
    } 
} 
0

我清理了一下代码:

var total; 
var totalEarnings; 
var item; 
var salesPerson; 
var userInput; 
var input; 

while (userInput != -1) { 

    total = 0.0; 
    totalEarnings = 0; 
    salesPerson = 1; 
    userInput = parseInt(prompt("outsidePlease enter noOfItemsSold of Item# 1 sold for SalesPerson #" + salesPerson + "\n OR -1 to Exit"), 10); 
    salesPerson++; 

    for (item = 2; item < 5; item++) { 

     if (item == 1) { 
      total += userInput * 239.99; 
     } else if (item == 2) { 
      total += userInput * 129.75; 
     } else if (item == 3) { 
      total += userInput * 99.95; 
     } else if (item == 4) { 
      total += userInput * 350.89; 
     } 
     input = parseInt(prompt("Please enter noOfItemsSold of Item# " + item + " sold for SalesPerson #" + salesPerson + "\n OR -1 to Exit")); 

    } 

    totalEarnings = .09 * total + 200; 
    document.writeln("<h1>SalesPerson#" + salesPerson + " Total Earnings this week is " + totalEarnings + "</h1>"); 

} 

你的循环将重复只要userInput比-1值。

现在让我们来看一下是什么每次循环的作用:

  1. 据一些初始值分配给几个变量。

  2. 它提示用户输入一个值;该值分配给userInput

  3. 然后,它(而奇怪)重复类似提示给用户,此时将它们分配给inputuserInput),并使用它们来执行计算为total

userInput是不断赋值唯一的一次是在第2步:

userInput = parseInt(prompt("outsidePlease enter noOfItemsSold of Item# 1 sold for SalesPerson #" + salesPerson + "\n OR -1 to Exit"), 10); 

如果你输入-1作为输入这里,循环will终止,正如预期。

+0

我的方法为一个人做了计算,但又没有再次运行程序我想运行我的第二个人的循环,所有变量初始化为初始值,所以我已经在while循环中声明了它们,但即使用户在此输入-1语句它不退出循环userInput = parseInt(prompt(“outsidePlease输入noOfItemsSold销售的SalesPerson#”+ salesPerson +“\ n OR -1退出”),10); – user3109794

+0

“尽管用户在此语句中输入-1,但它不退出循环” 这不是事实。如果你输入-1作为第一个数字*,那么它*会退出循环。 这是因为第一个提示是唯一一次为'userInput'赋值的唯一时间。随后提示的值将被分配给'input',这是一个不同的变量,并且不会被您的'while'条件检查。 虽然这个代码有很多其他的错误。例如,不需要第一次分别调用prompt(),然后使用循环在接下来的三次调用它。 – greenie2600