What’s M/C? Is it multiple choice? Where is the question from? You need to write the question on paper, leaving spaces between successive lines, adding {} so as to retain the flow of control unchanged. Then you will be able to see whether that code will compile. I presume x and y are already declared and initialised.
Adding code tags shows how you indented the code when you copied it. Is that the same indentation they used in the quiz (please quote the link, book title, or whatever) originally? I have not edited your first post, so as to remove indentation. Remember, in some quizzes, indentation is used to mislead confuse or lead astray. You need to indent it correctly, with added {}, as per the Java™ grammar. Then you can see why it says, “doesn’t compile.” Please post your indented version.
Wow.....I thought this was a forum for beginners to get help. You don't know what I have done before I posted to this forum so how can you judge me?? I have complied it and I don't see why the answer is not 12.
You get a lot more help by writing it down on paper and showing us what you have than by our giving you the solution. If we simply do that, you learn nothing. If you write it on paper (or a word-processor) you will learn how to do it now and next time. I wrote it on paper myself to confirm that the website was giving the correct answer, and it too about 2 minutes.
Tom Petty wrote:Wow.....I thought this was a forum for beginners to get help.
It is. Did you read the "Show Some Effort" link?
Help means you give it your best shot, post what you tried and what happened, or what you think the answer should be, and then we nudge you in the right direction toward figuring out your problem yourself. It doesn't mean just giving you the answer.
You don't know what I have done before I posted to this forum
Of course not. We can only go by what you've posted here, which shows no evidence of effort, nor any indication of what your thoughts on the matter are.
so how can you judge me??
Nobody is judging you. We just want you to be a more active participant in solving your problem. :-)
I have complied it and I don't see why the answer is not 12.
The answer is not 12 because that would require running it, and you can't run it, because you can't compile it, because it has a syntax error.
What error message did you get when you tried compiling it? What do you think that error means?
Campbell Ritchie wrote:No need to be sarcastic, thank you very much.
You get a lot more help by writing it down on paper and showing us what you have than by our giving you the solution. If we simply do that, you learn nothing. If you write it on paper (or a word-processor) you will learn how to do it now and next time. I wrote it on paper myself to confirm that the website was giving the correct answer, and it too about 2 minutes.
There is no sarcasim there. If I new what my mistake was there would be no reason to post on this forum. I took the quiz, I missed the probelm and I am curious as to why. The more I look at it I am thinking it is because there are no braces separating the if statements.
Tom Petty wrote:There is no sarcasim there. If I new what my mistake was there would be no reason to post on this forum. I took the quiz, I missed the probelm and I am curious as to why. The more I look at it I am thinking it is because there are no braces separating the if statements.
Well, as has been suggested several times now - don't think about it; do it.
The likelihood is that the error message will explain exactly what the problem is.
Winston Gutkowski wrote:
The likelihood is that the error message will explain exactly what the problem is.
Technically, yes, but in this case I'd say it's one step removed from the actual coding mistake. A tiny bit of reasoning is all it should take to bridge that gap though.
Hint: The indentation of this code is misleading and makes you think that it's structured in a certain way, while the structure is really something else.
Post by:autobot
I can't renounce my name. It's on all my stationery! And hinted in this tiny ad:
a bit of art, as a gift, that will fit in a stocking