循环函数在javascript中工作20次,但随后返回NaN

时间:2017-02-24 00:14:30

标签: javascript jquery google-chrome

 /**
   * This program will prompt the user to guess a secret number
   * This number will is between 1 and N, where N is a postive number
  *
 * @author: Perry C
*/

 import java.util.Scanner;

 public class SecretNumber
 {
 public static void main( String [] args )
 {
    int N;    
    int guess;
    int playAgain;
    int tries;
    int maxTries;

    playAgain = 1;
    tries = 0;

    Scanner input = new Scanner( System.in );

    while(playAgain == 1)
      { 
      boolean win = false;
      System.out.println( "This is a guessing game." );
      System.out.println( "How many guesses would you like?" );
      maxTries = input.nextInt();
      System.out.println( "Enter a value between 1 and 1000: " );
      N = input.nextInt();

      int randomNumber = (int)(N * Math.random()) + 1;

    while (win == false) 
      {
        System.out.println( "Enter your guess: " );                      
        guess = input.nextInt();
        tries++;

        if(guess == randomNumber) 
        {  
          System.out.println( "Congratulations! The number of guesses it took you was \t" + tries );
          win = true;
          System.out.println( "Would you like to play again(1 or 2):" );
          playAgain = input.nextInt();
          tries = 0;
        }

        else if(guess < randomNumber)
          System.out.println( "Too low, guess again." );

        else if(guess > randomNumber)                        
          System.out.println( "Too high, guess again." );

        if(tries == maxTries)
        {
          System.out.println( "You have exceeded the max number of tries. \n You lose." );
          System.out.println( "Would you like to play again(1 or 2):" );
          playAgain = input.nextInt();
          tries = 0;
          win = true;
        }
      }
    }
}
}

网页我在https://m.roblox.com/items/362081769

上使用此代码

当我运行此代码时,它会成功运行20次并返回3997,然后我会收到&#34; NaN&#34; Image

我试图让这个程序做的是检查项目的价格,只要我想要,并在Chrome控制台中报告。为什么函数工作20次然后只返回NaN?

我是javascript的初学者,所以请解释一下你的解决方案,以便像我这样的初学者能够理解。

谢谢:)

0 个答案:

没有答案