177k views
2 votes
I'm having trouble with a java coding assignment and I need help with the modulus at the bottom of the code. Help?

import java.util.Scanner;
import java.lang.Math;

class Lesson_6_Activity_One {
public static void main(String[] args) {

/* Write your code here
* Copy and paste your entire code to Code Runner to complete the activity,
* from the first import statement to the last bracket.
*/
Scanner scan = new Scanner (System.in);
System.out.println("Please enter two integers:");
int o = scan.nextInt();
int t = scan.nextInt();
System.out.println("The average is: " + ((double) * (o + t)/2));

}
}

User Bedbad
by
4.8k points

1 Answer

6 votes

The (double) thingy is a cast, it should be placed in front of a variable to promote that variable from int to double. So remove the asterisk, and the program works:

System.out.println("The average is: " + ((double) (o + t)/2));

Without the cast, the program would do integer calculation, essentially giving you a rounded integer result, which is not what you want.


User Mythics
by
5.7k points