我正在可汗学院学习编码。它使用处理作为其基本语言。我了解到 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);