请注意下面代码段中的打印语句。我的问题是,如果我尝试在 print 语句中添加两个双精度值,它会打印不正确,但是如果我将它们添加到 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);
}