1

I have this simple problem I'm trying to do. I'm trying to write a program that doubles these five numbers. Then I want to compute the average of these numbers and print them out. The code runs with no errors, but it will not print my answer for some reason. How can I get it to print the output of the problem or simply print something? I am using Netbeans.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package marina;

/**
 *
 * @author bax
 */
public class Precedence {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    double grade1 = 100;
    double grade2 = 75;
    double grade3 = 88;
    double grade4 = 65;
    double grade5 = 99;
    int x = (int) (grade1+grade2+grade3+grade4+grade5/5.0);

    System.out.println(x);   

    }
}

Output:

run:
BUILD SUCCESSFUL (total time: 0 seconds)
12
  • 2
    What exactly is not working? Do you get an exception? Or not the result you expected? Commented Jan 31, 2013 at 9:31
  • 1
    How do you run your program? From a command-line? From an IDE such as Eclipse? Commented Jan 31, 2013 at 9:32
  • 1
    I just ran your program and got output as 85. What is the problem you are facing? Commented Jan 31, 2013 at 9:33
  • 1
    Make sure you have your file open and press Shift + F6. Notice that if you press just F6 then the main project will be run, not necessarily this project. Also, make sure you hit the Clean and build option, to clean up cache and so on. Commented Jan 31, 2013 at 9:43
  • 1
    @Lulu: It runs the current file. Since this is the solution, I have posted the above comment as answer. Please mark that as correct answer, so the SO community knows that this question is solved. Commented Jan 31, 2013 at 9:46

6 Answers 6

2

(Like I said in the comments:)

Make sure you have your file open and press Shift + F6, which runs the current file. Notice that if you press just F6 then the main project will be run, not necessarily this project. Also, make sure you hit the Clean and build option, to clean up cache and so on.

Sign up to request clarification or add additional context in comments.

Comments

1

group your additions in brackets(elm1+elm2)/5 and then divide it by 5

    int x = (int) ((grade1+grade2+grade3+grade4+grade5)/5);
System.out.println(x);

Comments

0

System.out.println() will print to the console, so make sure you run it there.

Further, you are not calculating an average. Division has higher precedence than addition. Rewrite like:

int x = (int) ((grade1+grade2+grade3+grade4+grade5)/5.0);

3 Comments

Thank you for correcting my calculations. But it still will only give the output I updated in my question.
System.out.println() will print to the standard output, which usually is a console in a terminal, but can be a file or the standard input of another process for example.
Open the output window, not the build window.
0

If you're using Eclipse or another IDE to run your program, there should be a "console" window somewhere in the IDE, that will display the console output.

To run your program without an IDE, run the following in a console (cmd on windows, terminal/bash on linux, ...):

javac MyFile.java
java MyFile

If that doesn't work, you will have to add the JDK binaries to your PATH.

Comments

0

It should print the result but you have also a mistake with the calculation. You only divide grade5 through 5.0.

This is what you want to do I think:

int x = (int) ((grade1+grade2+grade3+grade4+grade5) / 5.0);

Try also:

System.out.println("Result: " + x);

2 Comments

Thank you, that is how I wanted to calculate it. However, it still will not print my answer. Only the output I updated in my question.
How do you run your program? If possible, make a screencap
0
int x = (int) (grade1+grade2+grade3+grade4+grade5/5.0);

(1+1+1+1+5/5)=5

(1+1+1+2+5)/5 = 2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.