这是循环遍历的一种方式,在《java编程思想》一书中称为“增强的for循环”。
意思大概是:把a类型的集合c中的每个元素赋值给b。我写个代码你理解一下:
List<String> strs = new ArrayList<String>()strs.add("hello")
strs.add("world")
for( String s : strs){ //把strs中的每个元素依次赋值给s。第一次吧hello复制给s
System.out.println(s) // 第一次输出hello,第二次输出world
}
所以此程序的输出结果是:
helloworld
执行过程与下面的程序是一个意思:
int size = strs.size()for(int i =0 i < size i++){
System.out.println(strs.get(i))
}
假设我这段代码和你写的是一样的(你那个实在是看不清楚,希望下次能用截图软件截图,不然真心不好回答):
public static void main(String[] args) {int c, p = 0
for (int a = 100 a < 200 a++) {
//System.out.println(a)
for (int b = 2 p == 2 b++) {
//System.out.println(b)
c = a % b
if (c == 0) {
p = 1
} else {
p = 0
}
if (p == 1)
break
if (b > a) {
p = 1
System.out.println(a)
}
}
}
}
其中这段代码并没有运行:
for (int b = 2 p == 2 b++) {//System.out.println(b)
c = a % b
if (c == 0) {
p = 1
} else {
p = 0
}
if (p == 1)
break
if (b > a) {
p = 1
System.out.println(a)
}
}
你在调试的时候可以在适当位置加入System.out.println()代码来看哪个地方出现问题,同时也要养成良好的代码风格,提高代码的可读性。
至于图片中的错误我并没能重现,可能是没看清楚符号吧。
欢迎追问,记得用截图软件或者复制贴上代码,谢谢。