这是一个基于特定算法工作的程序。它在java中完美运行但是 每当我运行html代码我都没有得到错误,但它只显示前三个数字,而在java代码中for循环继续,直到显示整数
Java代码
package test1;
public class Test1
{
public static void main(String arg[])
{
int num = 1;
counterA(num);
}
public static void counterA(int num)
{
for (int i = num; i <= 24; i += 5)
{
System.out.println(i);
counterB(i);
}
}
public static void counterB(int i)
{
counterA(i * 3);
}
HTML代码
<script>
var num1 ;
counterA(num1);
var total=0;
var counterA=function(num11)
{
for (num1 = num11; num1 <= 24; num1 += 5)
{
console.log(num1);
counterB(num1);
}
}
var counterB =function (num11)
{
counterA(num11 * 3);
}
</script>
<body>
<button onclick="counterA(1)">Try it</button>
</body>
</html>
答案 0 :(得分:1)
试试这个,
<!DOCTYPE html>
<html>
<script>
function counterA(num11)
{
var num1;
for (num1 = num11; num1 <= 24; num1 += 5)
{
alert(num1);
counterB(num1);
}
}
function counterB(num11)
{
counterA(num11 * 3);
}
</script>
<body>
<button onclick="counterA(1)">Try it</button>
</body>
</html>
这给我的输出为1,3,9,14,19,24,8,24,13,18,23,6,18,23,11,16和21。
答案 1 :(得分:1)
您的代码中有一些错误:
counterA
定义之前调用它。num1
循环for
醇>
使代码生效的可能选项:
var num1 ;
counterA(num1);
var total=0;
for (num1 = num11; ...
替换为for(var num1 = num11; ...
答案 2 :(得分:0)
你可以做到
<html>
<script type="text/javascript">
function counterA(num11){
for (var num1 = num11; num1 <= 24; num1 += 5) {
alert(num1);
counterA(num1*3);
}
}
</script>
<body>
<button onclick="counterA(1)">Try me</button>
</body>
</html>