Jump to content

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

RainColt
Go to solution Solved by RainColt,
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

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?

 

Link to comment
Share on other sites

Link to post
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 - EVGA Z370 Classified K - G.Skill Trident Z RGB - WD SN750 - Jedi Order Titan Xp - Hyper 212 Black (with RGB Riing flair) - EVGA G3 650W - dual booting Windows 10 and Linux - Black and green theme, Razer brainwashed me.

Draws 400 watts under max load, for reference.

 

How many watts do I needATX 3.0 & PCIe 5.0 spec, PSU misconceptions, protections explainedgroup reg is bad

Link to comment
Share on other sites

Link to post
Share on other sites

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?

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×