Reputation: 143
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)
Upvotes: 1
Views: 27343
Reputation: 22977
(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.
Upvotes: 2
Reputation: 363795
int x = (int) (grade1+grade2+grade3+grade4+grade5/5.0);
(1+1+1+1+5/5)=5
(1+1+1+2+5)/5 = 2
Upvotes: 0
Reputation: 1507
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);
Upvotes: 0
Reputation: 78
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.
Upvotes: 0
Reputation: 33511
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);
Upvotes: 0
Reputation: 46408
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);
Upvotes: 1