Answer:
Type casting error
Explanation:
Logically, the program is correct and it is expected to return the average value. However, test1 and test2 are int values and when you applies any algebraic expression on Int, it will result into Int.
So, in this part (test1 + test2 )/2, two integers are adding and the final result is still an integer and this part will act as an integer even if you multiple or divide with any external number.
For example,
test1 = 4
test2 = 5
test1 + test2 = 9
Now, if you divide it by 2, it will still react as an integer portion. So, 9/2 would result in 4 instead of 4.5. All the calculation is done on the right side before assigning it to a double variable. Therefore, the double variable is still getting the int value and your program is not working correctly.
The solution is to typecast the "(test1 + test2 )/2" portion to double using Double.valueOf(test1 + test2)/2 ;
I have also attached the working code below.
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
System.out.println("Hello World");
Scanner scan = new Scanner (System.in);
int test1 = scan.nextInt();
int test2 = scan.nextInt();
double average = Double.valueOf(test1 + test2)/2 ;
System.out.println("Answer: " + average);
}
}