![](/img/trans.png)
[英]I have been trying to make this ball bounce for a day. Why doesnt it work?
[英]Im trying to make a lottoticket for school, but when i have to draw the numbers it doesnt work(line 59)
除了第59行中的所有內容外,其他所有內容似乎都在起作用。 我對編碼非常陌生,但實際上並沒有得到……我找不到錯誤,我錯過了{}或(),但我不知道。 我認為唯一的錯誤是在“ TegnKupon”中,因為那是谷歌瀏覽器告訴我要更改的部分。
我試着和一些伙伴談論這件事,但是他們倆也幫不上忙。 他們做到了,但是顯然是另一種方式。
<!DOCTYPE html>
<html>
<head>
<canvas id="myCanvas" width="600" height="300" style="border:1px solid #ff0000;">
</canvas>
</head>
<script type="text/javascript">
function find_randomtal(limit)
{
var random_tal
var cont = true
while (cont)
{
random_tal=Math.round(Math.random()*100+1);
if (random_tal <=limit)
{
cont = false
}
}
return random_tal;
}
var lottotal = [];
function Findlottotal ()
{
var min=1;
var random_talny;
var lottotal = [];
for (var j=min; j<=7; j++)
{
random_talny = find_randomtal(36);
if (lottotal.indexOf(random_talny)<0)
{
console.log(j + "random_talny: " + random_talny + " Findes ikke ");
lottotal[j] = random_talny
}
else
{
console.log (j + "random_talny: " + random_talny + " Findes ");
j--
}
}
}
var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext("2d");
function TegnKupon()
{
var taeller = 0
for (var j=1;j<=4;j++)
{
for (var i=1;i<=9;i++)
{
taeller = taeller+1
xpos=i*100-50
ypos=100*j-50
if (lottotal.indexOf(taeller)<0)
{
ctx.beginPath();
ctx.fillStyle = "rgb(255,255,255)"
ctx.arc(xpos,ypos,50,0,2*Math.PI);
ctx.fillText("tallet",x,y+10)
}
else
{
ctx.beginPath();
ctx.fillStyle = "rgb(255,0,0)"
ctx.arc(xpos,ypos,50,0,2*Math.PI);
ctx.fillText("tallet",x,y+10)
}
}
}
}
lottotal = Findlottotal()
</script>
<body onload="Findlottotal();TegnKupon()">
</body>
</html>
這里發生了幾件事...
首先, x
和y
在TegnKupon
中未定義。 也許您想使用xPos
和yPos
?
接下來,我注意到為lottotal
分配了Findlottotal
的值,但實際上,此函數不返回任何內容。 我在Findlottotal
的底部添加了一行以返回lottotal
。
這是您對我進行調整的摘要。 它不再產生控制台錯誤。
<!DOCTYPE html>
<html>
<head>
<canvas id="myCanvas" width="600" height="300" style="border:1px solid #ff0000;">
</canvas>
</head>
<script type="text/javascript">
function find_randomtal(limit) {
var random_tal;
var cont = true;
while (cont) {
random_tal = Math.round(Math.random() * 100 + 1);
if (random_tal <= limit) {
cont = false;
}
}
return random_tal;
}
var lottotal = [];
function Findlottotal () {
var min = 1;
var random_talny;
var lottotal = [];
for (var j = min; j <= 7; j++) {
random_talny = find_randomtal(36);
if (lottotal.indexOf(random_talny) < 0) {
console.log(j + "random_talny: " + random_talny + " Findes ikke ");
lottotal[j] = random_talny;
}
else {
console.log (j + "random_talny: " + random_talny + " Findes ");
j--;
}
}
return lottotal;
}
var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext("2d");
function TegnKupon()
{
var taeller = 0
for (var j = 1; j <= 4; j++)
{
for (var i = 1; i <= 9; i++)
{
taeller = taeller + 1;
xpos = i * 100 - 50
ypos = 100 * j - 50;
if (lottotal.indexOf(taeller) < 0) {
ctx.beginPath();
ctx.fillStyle = "rgb(255,255,255)";
ctx.arc(xpos, ypos, 50, 0, 2 * Math.PI);
ctx.fillText("tallet", xpos, ypos + 10);
}
else {
ctx.beginPath();
ctx.fillStyle = "rgb(255,0,0)"
ctx.arc(xpos,ypos,50,0,2*Math.PI);
ctx.fillText("tallet",xpos,ypos+10);
}
}
}
}
lottotal = Findlottotal()
</script>
<body onload="Findlottotal();TegnKupon()">
</body>
</html>
如以上答案所示,我發現了一些錯誤:)這與您要查找的內容有關嗎?
function find_randomtal(limit) { var random_tal var cont = true while (cont) { random_tal=Math.round(Math.random()*100+1); if (random_tal <=limit) { cont = false } } return random_tal; } function Findlottotal () { var min=0; var random_talny; var lottotal = []; for (var j=min; j<=7; j++) { random_talny = find_randomtal(36); if (lottotal.indexOf(random_talny)<0) { console.log(j + "random_talny: " + random_talny + " Findes ikke "); lottotal[j] = random_talny } else { console.log (j + "random_talny: " + random_talny + " Findes "); j-- } console.log(lottotal); } return lottotal; } var canvas = document.getElementById('myCanvas'); var ctx = canvas.getContext("2d"); function TegnKupon(lottotal) { var taeller = 0 for (var j=1;j<=4;j++) { for (var i=1;i<=9;i++) { taeller = taeller+1 xpos=i*100-50 ypos=100*j-50 console.log("taller " + lottotal); if (lottotal.indexOf(taeller)<0) { ctx.beginPath(); ctx.fillStyle = "rgb(255,255,255)" ctx.arc(xpos,ypos,50,0,2*Math.PI); ctx.fillText("tallet",xpos,ypos+10) } else { ctx.beginPath(); ctx.fillStyle = "rgb(255,0,0)" ctx.arc(xpos,ypos,50,0,2*Math.PI); ctx.fillText("tallet",xpos,ypos+10) } } } } document.getElementsByTagName("body")[0].onload = function() { var lottotal = Findlottotal(); TegnKupon(lottotal); };
<!DOCTYPE html> <html> <head> </head> <canvas id="myCanvas" width="600" height="300" style="border:1px solid #ff0000;"> </canvas> <body> </body> </html>
您不應以這種方式調用函數。 而是將它們包裝到一個名為init的函數中,並將其稱為onload
function init(){
Findlottotal();
TegnKupon();
}
window.onload = init();
另外,您引用x
和y
未定義它們。 我想也許您是想說xpos
和ypos
嗎?
我不確定您要查找的輸出是什么,但發現了2個問題:1.在功能TengKupon中未定義lottotal 2.在TengKupon中,if / else中的ctx.filltext僅列出了“ x”和“ y”。 您是要在此處使用“ xpos”和“ ypos”嗎? 當我相應地替換它們時,我得到了一個帶有“ tallet”的網格(一次(來自“ if”))或多次(來自“ else”)
所以2號應該很容易,但是對於1號,我認為問題可能是您沒有返回“ lottotal”以將其提供給TengKupon。 您的錯誤是說“無法讀取未定義的indexof,這是指樂透。
我假設您是要從FindLottotal返回樂透,所以我在FindLottotal for循環中的if循環正下方添加了“ return lottotal”:
var lottotal = [];
for (var j=min; j<=7; j++)
{
random_talny = find_randomtal(36);
if (lottotal.indexOf(random_talny)<0)
{
console.log(j + "random_talny: " + random_talny + " Findes ikke ");
lottotal[j] = random_talny
}
else
{
console.log (j + "random_talny: " + random_talny + " Findes ");
j--
}
return lottotal;
}
在不了解您要查找的內容的情況下,我無法提供更多信息,但是希望這可以使您進入下一步。
祝好運!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.