這是我第一篇文章。習題2.2的數學公式 - Daniel Liang
我一直在尋找答案,我正在嘗試在Daniel Liang編寫的Java編程入門(第10版)中做的練習。
我認爲我的大部分代碼是正確的,但我的答案不同於書籍控制檯示例。我使用了本書告訴我使用的數學公式,當我在控制檯中輸入相同的數字時,我的答案是不一樣的。
更具體的它要求我用這個公式來計算面積:面積=平方根(3)/ 4(邊長)^ 2
我可能做錯了與添加這個公式,但周圍的人不停地搜索使用同樣的事情提示(Math.sqrt(3))
這裏是我的代碼,任何幫助appriciated:
import java.util.Scanner;
public class Exercise2 {
public static void main(String[] args) {
// Create new scanner
Scanner input = new Scanner(System.in);
// Prompt the user to enter length and height of the Equilateral triangle
System.out.print("Enter length of the sides" +
" and height of the Equilateral triangle: ");
double lengthOfSides = input.nextDouble();
// Calculate the area & display to user
double area = (Math.sqrt(3)/4) * (Math.pow(lengthOfSides, 2));
System.out.println("The area is: " + area);
//Calculate the volume & display to user
double volume = area * lengthOfSides;
System.out.println("The volume of the Triangular prism is: " + volume);
input.close();
}
}
忘了補充我得到什麼,什麼書得到:
我得到 輸入邊的長度和高度的等邊三角形的:3,5 面積:5.304405598179686 三角棱柱的體積是:18.565419593628903
書: 輸入邊長和等邊三角形的高度:3,5 面積:3.89 三角棱鏡的體積爲:19.48
該公式表明您需要通過'lengthSides^2'的結果倍增'square root(3)/ 4'的結果。如果是,那麼你已經正確實施了,如果沒有,那麼請準確地說出公式是什麼,這樣我們就可以解決它 –
我只是用計算器檢查了一下這個區域:√3÷4×3.5²= 5.304405598 Can你在書中仔細檢查答案,也許在某個地方有錯字? – Boris
只是爲了確保我自己,[公式](http://www.mathwords.com/a/area_equilateral_triangle.htm)也是正確的 – AxelH