標題:
TQC+ 106 數學函數
[打印本頁]
作者:
周政輝
時間:
2016-12-17 10:27
標題:
TQC+ 106 數學函數
假設有一數學函數 f(x)=3(x^3)+2x-1
請撰寫一函數 f 來回傳 f(x) 的值, 取至小數後第四位, 參考執行畫面如下:
[attach]1826[/attach]
作者:
蔡季樺
時間:
2016-12-17 11:09
public class JPA01 {
public static void main(String args[])
{
System.out.printf("f(-3.2)=%.4f\r\n",f(-3.2));
System.out.printf("f(-2.1)=%.4f\r\n",f(-2.1));
System.out.printf("f(0)=%.4f\r\n",f(0));
System.out.printf("f(2.1)=%.4f\r\n",f(2.1));
}
public static double f(double x)
{
return 3*(x*x*x)+2*x-1;
}
}
複製代碼
作者:
王彥甯
時間:
2016-12-17 11:12
本帖最後由 王彥甯 於 2016-12-17 11:16 編輯
public class JPA01 {
public static void main(String args[]){
double x;
x = -3.2;
System.out.printf("f(%.1f)=%.4f\n",x,f(x));
x = -2.1;
System.out.printf("f(%.1f)=%.4f\n",x,f(x));
x = 0;
System.out.printf("f(%.1f)=%.4f\n",x,f(x));
x=2.1;
System.out.printf("f(%.1f)=%.4f\n",x,f(x));
}
private static Object f(double x) {
// TODO Auto-generated method stub
return 3*(Math.pow(x, 3))+2*x-1;
}
}
複製代碼
作者:
張健勳
時間:
2016-12-17 11:33
public class JPA01 {
public static void main(String[]args) {
f(-3.2);
System.out.println("");
f(-2.1);
System.out.println("");
f(0);
System.out.println("");
f(2.1);
}
public static void f ( double x ) {
float result = (float)(3*(Math.pow(x,3)+2*x-1));
System.out.printf("f(%f) = %.4f",x,result);
}
}
複製代碼
作者:
蔡庭豪
時間:
2016-12-20 17:14
import java.text.DecimalFormat;
public class JPA01 {
public static void main (String args[]){
double x;
System.out.printf("f(-3.2)=%.4f%n",f(-3.2));
System.out.printf("f(-2.1)=%.4f%n",f(-2.1));
System.out.printf("f(0)=%.4f%n",f(0));
System.out.printf("f(2.1)=%.4f%n",f(2.1));
}
public static double f(double x){
return 3*Math.pow(x, 3)+2*x-1;
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2