Help me with this JAVA bug!
Let me make you clear, I'm not that of a programmer on myself. I'm learning by self from the web and I encountered a very unfamiliar bug in my java program. There is no syntax error but the output is incorrect. I've little knowledge in QBASIC and Python and JAVA is very new to me!
So after I learned a little maths syntax and a bit of loop in Java, I tried to create a program to find if the number is armstrong or not. Well, we all know '153' is a armstrong number but the program says that it isn't. I did maths on my own and I can't find out where the logic went wrong. I know there are very experienced programmers on the web who could help me with this logical error that I have encountered. !
here is the link to pastebin file of the actual code I used. Try to be as detail as possible in the comments because I may not know the technical terms.
@sugamadhiakri has set 0.500 SBD bounty on this post!
Bounties are a new way you can earn rewards irrespective of you Steem Power. Go here to learn how bounties work.
Earn the bounty by commenting what you think the bounty creator wants to know from you.
Find more bounties here and become a bounty hunter.
Happy Rewards Hunting!
Congratulations to the following winner(s) of the bounty!
Find more bounties here and become a bounty hunter.
First of all, the code in the Pastebin is different than the one on the picture...
Your problem in the Pastebin version is, that your checkArm method ALWAYS(!) returns false. Even if the number is armstrong number, you always return false - on the line 27.
The code on the picture doesn't work because of 2 reasons:
Also, just the effectivity advice. You can just do return (a ==c);
That expression already evaluates into True or False. What you basically do is "If True then return True ". It works but it's not really the way how you should do it :) You save 3 lines like this and code looks better.
Keep learning! :)
I have same problem
Hello! Your post has been resteemed and upvoted by @ilovecoding because we love coding! Keep up good work! Consider upvoting this comment to support the @ilovecoding and increase your future rewards! ^_^ Steem On!
Reply !stop to disable the comment. Thanks!
The logic should be:
while(n!=0)
(
b=n%10;
a=a+Math.pow(b,3);
n=n/10;
)
Sorry I don't have flowerbrackets on my keyboard so used round brackets for encapsulating the block.
This is my 9th grade problem so I still remember. This will work. And good to see a person with so much interest to learn programming. You can ask me questions related to programming whenever you want. All the best buddy. It will work.
@matkodurko is correct. Some other things to consider:
The ^ caret operator in java is bitwise exclusive or (XOR) and not the exponent operator Link to docs
You can either use Math.pow(), probably much easier, multiply your b, by itself 3 times. So change line 29 on your photo above to:
a = a + (b * b * b);
Note: I use the round brackets to make the order of operations unambiguous. They're not needed since multiplication is resolved before addition but I think having the brackets makes the code easier to read.
Also a note on style: this doesn't affect your result, but generally if you're going to copy the value of a method's parameter for later use then it's usually easier to read if you change your local copy rather than the function parameters. How this affects your code, from line 27 onwards you exchange all n's for c's and c's for n's. That'll mean that on your check, before your are returning true or false, you will be comparing a with n. That's a bit easier to read imo and also reduces the risk of issues when you deal with pass-by-reference parameters (in java or some other language).