新聞中心
用JAVA程序編寫一道:求一長方體的體積,(長3寬4高5)
package com.tmgps.test;
創(chuàng)新互聯公司于2013年開始,是專業(yè)互聯網技術服務公司,擁有項目網站制作、成都做網站網站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元富裕做網站,已為上家服務,為富裕各地企業(yè)和個人服務,聯系電話:18982081108
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("請輸入長方體的長");
int a=input.nextInt();
System.out.println("請輸入長方體的寬");
int b=input.nextInt();
System.out.println("請輸入長方體的高");
int c=input.nextInt();
System.out.println("長方體的體積為:"+a*b*c);
}
}
長寬高都可以自己輸入
java怎么編寫求長方形表面積和體積的代碼
//主要代碼 System.out.print("長:") double num1=input.nextDouble(); //代碼太多以后簡寫System...("寬:"); double num2=.... System....("高:")double num3=.... System...("表:"+(num1*num2+num1*num3+num2*num3)*2+" 體積:"+n1*n2*n3)//打字不容易求給分
java編寫程序,從鍵盤輸入圓柱體的底半徑r和高h,然后計算其體積并輸出.要最簡單的
代碼:
public?class?Test?{
public?static?void?main(String[]?args)?{
Scanner?scanner?=?new?Scanner(System.in);
System.out.println("請輸入圓柱體的底半徑:");
int?r?=?scanner.nextInt();
System.out.println("請輸入圓柱體的高:");
int?h?=?scanner.nextInt();
double?pi?=?Math.PI;
//?取整數部分
int?v?=?(int)?pi?*?r?*?r?*?h;
System.out.println("圓柱體的體積是:"?+?v);
}
}
效果:
java 求體積
在main方法中實現Shape的對象時,使用Trangle或者Rectangle分別進行實例化。這樣對于同一個Pillar的對象,可以調用兩個不同類的實現方法來計算。具體代碼請參考下面。
Shape.java
public?abstract?class?Shape?{
public?int?dim1;
public?int?dim2;
public?abstract?int?getArea();
}
Trangle.java
public?class?Trangle?extends?Shape?{
public?int?getArea()?{
return?(super.dim1?*?super.dim2)?/?2;
}
}
Rectangle.java
public?class?Rectangle?extends?Shape?{
public?int?getArea()?{
return?super.dim1?*?super.dim2;
}
}
Pillar.java
public?class?Pillar?{
public?Shape?shape;
public?int?height;
public?int?getVolumn()?{
return?this.shape.getArea()?*?this.height;
}
public?static?void?main(String[]?args)?{
//?三棱柱
Pillar?p?=?new?Pillar();
p.shape?=?new?Trangle();
p.shape.dim1?=?2;
p.shape.dim2?=?4;
p.height?=?3;
System.out.println("三棱柱的體積是:"?+?p.getVolumn());
//?四棱柱
p.shape?=?new?Rectangle();
p.shape.dim1?=?2;
p.shape.dim2?=?4;
p.height?=?3;
System.out.println("四棱柱的體積是:"?+?p.getVolumn());
}
}
下面是執(zhí)行后的結果:
關于類的,寫一個球的體積的java代碼
import?java.util.Scanner;
/**
*?計算球的體積
*?
*?@author?young
*
*/
public?class?Volume?{
public?static?void?main(String[]?args)?{
System.out.print("請輸入r:");
Scanner?reader?=?new?Scanner(System.in);
double?r?=?0,?v?=?0;
r?=?reader.nextDouble();
v?=?4?*?3.14159?/?3?*?r?*?r?*?r;
System.out.println("球體積為:"?+?String.format("%.2f",?v));
}
}
分享文章:java求體積代碼 java求長方體體積
文章來源:http://fisionsoft.com.cn/article/dodgpdc.html