A. 求代碼 使用java命令行參數方式,從鍵盤輸入梯形的上底、下底和高,編程計算該梯形的面積。
publicstaticvoidmain(String[]args){
try{
doubletop=Double.valueOf(args[0]);
doublebottom=Double.valueOf(args[1]);
doubleheight=Double.valueOf(args[2]);
doublearea=(top+bottom)*height/2;
System.out.println("梯形面積是:"+area);
}catch(Exceptione){
System.out.println("命令行參數錯誤");
}
}
javac xxx.java
java xxx 2 4 5
B. java編寫梯形面積
class Tixing //梯形類{ private float Height; //高 private float upBotton;//上底 private float downBotton;//下底 public Tixing(float Height,float upBotton,float downBotton)//構造方法 { this.Height=Height; this.upBotton=upBotton; this.downBotton=downBotton; } public float getTixingArea() //計算梯形面積 { return (upBotton+downBotton)*height/2; }}public class Start{ public static void main(String[] args) { Tixing t=new Tixing(30,20,50);//構造 System.out.println("梯形的面積是:"+t.getTixingArea());//列印輸出 }}C. 跪求 JAVA計算梯形面積代碼
import java.util.Scanner;
public class Sum{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("請輸入上底長度");
int a=sc.nextInt();
System.out.println("請輸入下底長度");
int b=sc.nextInt();
System.out.println("請輸入高");
System.out.println("梯形面積為"+((a+b)*sc.nextInt()));
}
}