[英]Java Loop Not Printing Result As Expected
package com.company;
public class Main {
public static int deleteFives(int start, int end) {
int count = 0;
for (int j = start; start < end; j++) {
count++;
}
return count;
}
public static void main(String[] args) {
int result = deleteFives(1, 100);
System.out.println(result);
}
}
当我实际调用该方法时,此循环没有任何输出。 我只是想显示“计数”。 我的输出实际上是空白的。 我很困惑,因为我看不到任何缺陷。
因为在循环定义中未使用j。 您在代码中写了start <end而不是j <end。 下面的代码是打印结果
public static int deleteFives(int start, int end) {
int count = 0;
for (int j = start; j < end; j++) {
count++;
}
return count;
}
public static void main(String[] args) {
int result = deleteFives(1, 100);
System.out.println(result);
}
变量J不用于比较。 在条件检查中将变量J与start交换。
看到这个并像这样练习:
package com.practice;
public class Main {
private static int startCounting(int start, int end) {
int count = 0;
while(start++<end)
++count;
return count;
}
public static void main(String[] args) {
int countingResult = startCounting(1, 100);
System.out.println(countingResult);
}
}
start <end似乎在循环中是错误的。 我认为应该是j <end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.