package excise02;
public class test12 {
public static void main(String[] args) {
int score=53;
int count=0;
System.out.println("加分前的分数为:"+score);
for(int i=53;i<60;i++){
score=i;
count+=1;
}
System.out.println("加分后的分数为:"+score);
System.out.println("共加了"+count+"次!");
}
}输出结果为
加分前的分数为:53
加分后的分数为:59
共加了7次!
如果把代码改成
package excise02;
public class test1202 {
public static void main(String[] args) {
int score=53;
int count=0;
System.out.println("加分前的分数为:"+score);
for(score=53;score<60;score++){
count+=1;
}
System.out.println("加分后的分数为:"+score);
System.out.println("共加了"+count);
}
}这样就没有错了
我指定一个整型变量i,放到循环条件中去,再用i给score赋值,虽然多此一举,但是感觉没有错啊,想不出来为什么会错.
最后一次循环 i++自增1是判定!循环终止后i=60,score=59,score和变量i并不同步,循环还是7次没错。你改成后面那么score与for循环中的变量score同步了就对了
你做的没问题啊!都没错,你编辑的时候报错了吗?