Please note the print statements, in the code segments below. My question is how come If I try to add two doubles in the print statement it prints incorrectly, but if I add them outside the print statement and store the result in a variable than I am able to print it correctly.
Why does this work and print out the correct result?
public static void main(String argsp[]){ Scanner input = new Scanner(System.in); double first, second, answer; System.out.println("Enter the first number: "); first = input.nextDouble(); System.out.println("Enter the second number: "); second = input.nextDouble(); answer = first + second; System.out.println("the answer is " + answer); } Why does this print out the wrong result?
public static void main(String argsp[]){ Scanner input = new Scanner(System.in); double first, second; System.out.println("Enter the first number: "); first = input.nextDouble(); System.out.println("Enter the second number: "); second = input.nextDouble(); System.out.println("the answer is " + first+second); }