【问题标题】:Match generated balls with the same number匹配生成的具有相同编号的球
【发布时间】:2016-11-15 16:37:09
【问题描述】:

我创建了一个程序:

  • 在屏幕上生成 4 个球,其中包含一个数字
  • 然后用户键入他想为要生成的新圈子提供的值。

点击按钮后,在已生成的球下会生成新的 4 个带有数字的圆圈。

如果第二行中的球号与第一行中的球号匹配,则该球应显示在该特定球的下方。

这是我的代码:

<html>
  <head>
    <style>
      body {
        margin: 0px;
        padding: 0px;
      }
    </style>
  </head>
  <body>
  <input id="one0" type="text" style="width:100px;height:30px;">
  <input id="one1" type="text" style="width:100px;height:30px;">
  <input id="one2" type="text" style="width:100px;height:30px;">
  <input id="one3" type="text" style="width:100px;height:30px;">
  <input id="result" value="click here" type="button" style="width:70px;" onclick="go()">
    <canvas id="myCanvas" width="800" height="500"></canvas>
    <script>
      var arr=[];
      var x=[];
      var y;
      for(var i=0;i<4;i++){
      arr[i]=5*(i+1);
      }
      var canvas = document.getElementById('myCanvas');
      var context = canvas.getContext('2d');
      var centerX = 0;
      var centerY = 90;
      var radius = 70;
      var temp;
      var xar=[];
        for(var i=0;i<4;i++){
        centerX=centerX+100;
      context.beginPath();
      context.arc(centerX, centerY, 50, 0, 2 * Math.PI, false);
      context.fillStyle = 'green';
      context.fill();
      context.lineWidth = 5;
      context.strokeStyle = '#003300';
      context.stroke();
      context.fillStyle="black";
      context.fillText(arr[i],centerX,centerY);
      }

        function go(){
        for(i=0;i<4;i++){
          temp=parseInt(document.getElementById("one"+i).value);
          xar.push(temp);
        }
      console.log(xar);
      var xnext=0;
      for(var i=0;i<4;i++){
      xnext=xnext+100;
      context.beginPath();
      context.arc(xnext, 190, 50, 0, 2 * Math.PI, false);
      context.fillStyle = 'green';
      context.fill();
      context.lineWidth = 5;
      context.strokeStyle = '#003300';
      context.stroke();
      context.fillStyle="black";
      context.fillText(xar[i],xnext,190);
      if(xar[i]==arr[i]){
       context.arc(xnext, 190, 50, 0, 2 * Math.PI, false);
      }


      }

      }
    </script>
  </body>
</html>    

按钮会生成球,但不会进行数字匹配。

注意 jsfiddle 没有正确显示结果。单击按钮时未显示任何结果。 https://jsfiddle.net/59p5k15q/

【问题讨论】:

    标签: javascript jquery arrays html


    【解决方案1】:

    我将 if() 移到函数 go() 的开头,就在您开始在画布上绘图之前。 (我保留 xnext+=100 以确保 x 值正确。)

    它现在似乎工作正常。我的第二组球是红色的。

    <html>
    <head>
    <style>
      body {
        margin: 0px;
        padding: 0px;
      }
    </style>
    </head>
    <body>
    <input id="one0" type="text" style="width:100px;height:30px;">
    <input id="one1" type="text" style="width:100px;height:30px;">
    <input id="one2" type="text" style="width:100px;height:30px;">
    <input id="one3" type="text" style="width:100px;height:30px;">
    <input id="result" value="click here" type="button" style="width:70px;" onclick="go()">
    <canvas id="myCanvas" width="800" height="500"></canvas>
    <script>
      var arr=[];
      var x=[];
      var y;
      for(var i=0; i<4; i++) {
        arr[i]= 5 * (i+1);
      }
      var canvas = document.getElementById('myCanvas');
      var context = canvas.getContext('2d');
      var centerX = 0;
      var centerY = 90;
      var radius = 70;
      var temp;
      var xar=[];
      for(var i=0; i<4; i++) {
        centerX=centerX+100;
        context.beginPath();
        context.arc(centerX, centerY, 50, 0, 2 * Math.PI, false);
        context.fillStyle = 'green';
        context.fill();
        context.lineWidth = 5;
        context.strokeStyle = '#003300';
        context.stroke();
        context.fillStyle="black";
        context.fillText(arr[i],centerX,centerY);
      }
      function go() {
        for(i=0; i<4; i++) {
          temp = parseInt(document.getElementById("one"+i).value);
          xar.push(temp);
        }
        console.log(xar);
        var xnext=0;
        for(var i=0; i<4; i++) {
          xnext=xnext+100;
          if(xar[i] == arr[i]) {
            continue;
          }
          context.beginPath();
          context.arc(xnext, 190, 50, 0, 2 * Math.PI, false);
          context.fillStyle = 'red';
          context.fill();
          context.lineWidth = 5;
          context.strokeStyle = '#330000';
          context.stroke();
          context.fillStyle="black";
          context.fillText(xar[i],xnext,190);
          context.arc(xnext, 190, 50, 0, 2 * Math.PI, false);
    
        }
      }
    </script>
    

    【讨论】:

      猜你喜欢
      • 2019-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-25
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多