如何在Processing中使用类似goto的功能?

时间:2014-12-12 04:01:33

标签: processing khan-academy

我正在学习Khan学院的代码。它使用处理作为基本语言。我了解到Java没有goto功能。那么如何使用处理实现goto函数。感谢解释说明。

这是我的代码,我已经评论了我想要使用goto功能的地方:



/* Returns either the index of the location in the array,
  or -1 if the array did not contain the targetValue */

var doSearch = function(array, targetValue) {
    var min = 0;
    var max = array.length - 1;
    var guess;

    //startover:

    if (guess === max || guess === min) {
      //goto notFound;
    }

    guess = round((min + max) / 2);

    if (targetValue === array[guess]) {
      return guess;
    } else {
      if (targetValue > array[guess]) {
        min = guess + 1;
        //goto startover;
      } else {
        max = guess - 1;
        //goto startover;
      }
    }
  }
  //notFound:
return -1;
};

var primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37,
  41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97
];

var result = doSearch(primes, 73);
println("Found prime at index " + result);

//Program.assertEqual(doSearch(primes, 73), 20);




1 个答案:

答案 0 :(得分:0)

您可能做的最好的事情是使用for循环并使用var client = new net.Socket(); client.connect(port, ip, function() { }); client.on('connect', function() { console.log('connected'); }); client.on('data', function(data) { console.log(data); }); break作为continue s。

goto