Search In
• More options...
Find results that contain...
Find results in...

# Need help squashing a simple bug (b=a+b);

3 minutes ago, Fasauceome said:

What language is this? Have you tried b+=a?

Its java, basically the same thing as c#.

I haven't but I just solved it. int b = 0; needed to be declared outside of the loop because it reset b back to 0 every time it looped

## Recommended Posts

Posted · Original PosterOP

I just started school and I'm having trouble with a program the loop works as intended except b=b+a;

It was my belief that b=b+a; would add the value of "a" to "b" and as the program loops "b" would store the grand total of every "a" value  calculated, but instead "b" ends up always equal to "a"

Scanner UserInput = new Scanner(System.in);
int input = UserInput.nextInt();                     //input ==1234
int length = String.valueOf(input).length();//length ==4

while (length>0)
{
length--;
int a = (int)( input/(Math.pow(10, length)));// a==1,a==2,a==3,a==4
int b = 0;
b=b+a;                                                    //b should ==10    by the end of the loop 1+2+3+4
System.out.println("b Sum =" + b);         //these 3 lines are just for debugging
System.out.println ("a =" + a);
System.out.println ("length" +length);
input = (int)(input - (Math.pow(10,length)) * a);
}

Why do you always die right after I fix you?

##### Share on other sites

What language is this? Have you tried b+=a?

I WILL find your ITX build thread, and I WILL recommend the SIlverstone Sugo SG13B

Primary PC:

i7 8086k (won) - EVGA Z370 Classified K - G.Kill Trident Z RGB - Force MP500 - Jedi Order Titan Xp - The venerated Hyper 212 Evo (with RGB Riing flair) - EVGA G2 650W - Black and green theme, Razer branwashed me.

Draws 400 watts under max load, for reference.

Linux Proliant ML150 G6:

Dual Xeon X5560 - 24GB ECC DDR3 - GTX 750 TI - old Seagate 1.5TB HDD - Dark moded Ubuntu (and Win7, cuz why not)

##### Share on other sites
Posted · Original PosterOP · Best Answer
3 minutes ago, Fasauceome said:

What language is this? Have you tried b+=a?

Its java, basically the same thing as c#.

I haven't but I just solved it. int b = 0; needed to be declared outside of the loop because it reset b back to 0 every time it looped

Why do you always die right after I fix you?