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。我不知道我做错了什么,但这不起作用。我怎样才能解决这个问题?

4

3 回答 3

0

基本上,直到循环的迭代完成后才会评估条件。所以,如果用户输入-1for循环仍然会被执行,这可能不是你想要的。

我建议使用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++)
  {
    // ...
  }
}
于 2014-02-28T23:28:40.170 回答
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. 然后它(相当奇怪地)向用户重复一个类似的提示,这次将它们分配给input( not userInput ) 并使用它们来执行计算total.

唯一一次userInput被赋值是在第 2 步:

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

如果您在此处输入 -1 作为输入,则循环will终止,正如预期的那样。

于 2014-02-28T23:35:10.660 回答
0
while (1) {


   if (condition met) {
       break;
   } 
}

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

于 2014-02-28T23:23:26.387 回答