为什么在下面代码中两处星号位置定义int b=1,输出结果不同

import java.util.Scanner;
public class Main{
public static void main(String [] args){
Scanner s = new Scanner(System.in);
double x = s.nextDouble();
int a = -1;
double d = 0,c;
// int b = 1; ****
for(int i = 1;i<=10;i++){
a = (a+2);
c = Math.pow(-1,i+1);
int b = 1; //****
for(int j = 1;j<=a;j++){
b = bj;
}
d = d + c
Math.pow(x,a)/b;
}
System.out.printf("%.3f",d);
}
}

第一处,定义在循环外只赋一次值。第二处,定义在循环内,每次循环都会重新赋值。

发问题注意代码缩进,方便别人看代码,支持md文档,可以使用代码块发问题

import java.util.Scanner;
public class Main{
    public static void main(String [] args){
        Scanner s = new Scanner(System.in);
        double x = s.nextDouble();
        int a = -1;
        double d = 0,c;
        // int b = 1; ****
        for(int i = 1;i<=10;i++){
            a = (a+2);
            c = Math.pow(-1,i+1);
            int b = 1; //****
            for(int j = 1;j<=a;j++){
                b = bj;
            }
            d = d + cMath.pow(x,a)/b;
        }
        System.out.printf("%.3f",d);
    }
}