以下のコード セグメントの print ステートメントに注意してください。私の質問は、print ステートメントに 2 つの double を追加しようとすると正しく印刷されないのですが、それらを print ステートメントの外に追加して結果を変数に格納すると、正しく印刷できないからです。
なぜこれが機能し、正しい結果が出力されるのですか?
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);
}
これが間違った結果を出力するのはなぜですか?
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);
}