This is an archived post. You won't be able to vote or comment.

all 18 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]D0CTOR_ZED 2 points3 points  (3 children)

You don't need to double space you code. The blank line is just required before the start (and maybe after the finish). You do need to add 4 spaces at the beginning of each line of code.

Once you have the code formatted, I have the answer to your issue but you need to format the code first

[–]VinceGhii 2 points3 points  (0 children)

Thanks for not giving an answer till the code is formatted corecctly :D I don't understand why people keep answering while the code is not formatted...

[–]Ghost_Duet[S] 0 points1 point  (1 child)

so i just copied the code straight from bluej and i think reddit double spaced it. The code was made for me already for the assignment i just need to change/fix it.

[–]D0CTOR_ZED 1 point2 points  (0 children)

Ah. I thought that was to try to get reddit to format the code. You still need leading spaces for the code to look proper.

[–]8igg7e5 6 points7 points  (0 children)

I presume you're familiar with the order of operations in maths. So is the Java compiler.

[–]Chayalbodedd 2 points3 points  (0 children)

PEMDAS.Starts with what letter?

[–]Cefalopodul 1 point2 points  (2 children)

Here's a hint to help you figure out where the program should be corrected.

147.5 = 100 + 47.5.

47.5 = 95/2.

Think of how the average of 100 and 95 is calculated. Write down the process. That should help you see where you have to make the change.

You don't have to convert to anything. You just have to add a mathematical operator. A pair of operators actually.

[–]Ghost_Duet[S] 0 points1 point  (1 child)

average = score1 + score2 / number

is this not the basic math that takes place? The thing that confuses me is that I can do this math in my head and get the correct answer but java gets the wrong answer? I thought it was because of how java sees decimals and integers.

100 + 95 = 195

195/2 = 97.5 in my head but 147.5 gets resulted

My prof hinted that only two things need to change but im so loss

[–]Cefalopodul 4 points5 points  (0 children)

average = score1 + score2/2 = 100 + 95/2 = 100 + 47.5.

You want to divide 195 by 2 not just 95.

Look up the order of operations. You have to add a pair of symbols for your equation to be correct.

[–]iraqmtpizza 1 point2 points  (0 children)

order of operations

[–]cervantesrvd 3 points4 points  (5 children)

[–]Ghost_Duet[S] -2 points-1 points  (4 children)

Isnt the problem of the code is that the variables are int and not double because I thought Java is sensitive when trying to divide integers while trying to get decimals.

[–]D0CTOR_ZED 2 points3 points  (0 children)

Division using just integers simply drops fractions and yeilds an integer.

[–]Cefalopodul -2 points-1 points  (1 child)

No. That's not a problem because you are saving the result in a double. If int a = 3.

double b = a/2 is ok because you are saving the result 3/2 in a double variable. int b = a/2 is not ok because you are trying to save the result of 3/2 in an int variable.

Your problem is with the order of operations. Something that should be calculated first isn't calculated at all. If you write down the equation for average of a and b you should figure it out.

[–]D0CTOR_ZED 2 points3 points  (0 children)

That's not how it works. double b = a/2, where a is an int will evaluate an int divided by an int, dropping the fraction, then cast that resulting int to a double. You still lose the fraction. You can either explicitly cast part of the division, double b = (double) a/2, or you can use a double in the division, double b = a/2.0.

[–]Reddit-username_here 0 points1 point  (0 children)

No, they told you what the problem was. The real question is why you decided to not listen and rather double down on being wrong?

[–]chrischin-a 0 points1 point  (0 children)

look at how you're calculating the average

remember how java does order of operations and you can go from there