2016-12-17 103 views
-2

我是编程新手。我学习了一点JavaScript,并制作了一段代码。当所有事情都是真实的时候,这一切都很好,但是当它是错误的时候,我需要添加一些事情来发生。所以我添加了一个while,它并没有为我工作。有人可以帮助我吗?谢谢你的答案!不知道这个代码有什么问题

<!DOCTYPE html> 
 
<html> 
 
    <head> 
 
    <meta charset="UTF-8"/> 
 
    <link rel="stylesheet" href="styles.css"/> 
 
    <script src="JavaScript.js"></script> 
 
    <title> 
 
     Hello 
 
    </title> 
 
    </head> 
 
    <body> 
 
    <p id="Hello"></p> 
 
    <script> 
 
     function onload() { 
 
     alert("choose a number between 1 and 9"); 
 
     var Multiply1 = prompt("Multiply this number by 9"); 
 
     
 
     while (Multiply1 % 9 != 0) { 
 
     alert("This can't be true"); 
 
     var Multiply = prompt("Multiply this number by 9"); 
 
     } 
 
     
 
     while (multiply1 % 9 = 0) { 
 
     var add = prompt("Add the 2 digits you have"); 
 
     } 
 
     
 
     while (add != 9) { 
 
     alert("This can't be true"); 
 
     var add1 = prompt("Add the 2 digits you have"); 
 
     } 
 
     
 
     while (add = 9) { 
 
     var decrease = prompt("deacrease the number you have chosen from the number you got:"); 
 
     alert("the number you have chosen is " + (9-decrease)); 
 
     
 
     } 
 
     } 
 
     
 
    </script> 
 
    <noscript>Please enable JS</noscript> 
 
    <button onclick="onload()">Cilck on me to start"</button> 
 
    </body> 
 
</html>

+3

欢迎堆栈溢出。请阅读http://stackoverflow.com/help/how-to-ask,它将帮助您如何提问。就目前而言,这个问题属于“为什么不用这个代码?”这个类别,它将被删除。我们希望您能够努力解决问题,而不只是向我们展示代码并询问为什么它不起作用。 – Mikkel

回答

0

我看到的几个问题 - 首先,JavaScript是大小写敏感的,所以你需要使用变量的具体情况 - Multiply1 != multiply1。接下来,只要条件成立,while循环将继续,因此您会先陷入任何一个循环评估为true的环境中。相反,您应该有一个内部的if条件的外部循环。

function myFunction() { 

    var myValue = 0; 

    // this will run until "myValue" equals "something" 
    while(myValue !== something) { 
    if (myValue < 9) { 
     // update the value if it is less than 9 
    } 

    if (myValue === 10) { 
     // update the value if it is equal to 10 
    } 
    } 

    return myValue; 
} 
0

尝试把假案内如果statement.No需要独立而真实,false.Two循环而循环就足够了。

0

你应该避免调用在HTML中的JavaScript函数是这样的:

<button onclick="onload()"> 

Why is using onClick() in HTML a bad practice?

+0

欢迎来到SO作为新用户。尼斯:新用户回答新用户,但问题不符合[如何问](https://stackoverflow.com/help/on-topic)您可能会回答。 – LotPings