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

コードを少し整理しました。

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. 次に、(かなり奇妙に)ユーザーに同様のプロンプトを繰り返します。今回はそれらをinputnot 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 に答える
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++)
  {
    // ...
  }
}
于 2014-02-28T23:28:40.170 に答える