十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
class Js{
专注于为中小企业提供成都网站建设、网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业丽江免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了近1000家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
private int a;
private int b;
private int c;
public Js(int a,int b,int c){
this.a=a;
this.b=b;
this.c=c;
}
public int geta(){
return a;
}
public int getb(){
return b;
}
public int getc(){
return c;
}
public double getArea(){
double abc=a+b+c;
double s=abc/2;//不过,貌似老兄的面积计算公式给的不对。
return s;
}
public double getJsLength(){
return a+b+c;
}
}
public class jxcomputer {
public static void main(String [] args){
Js i=new Js(3,6,8);
System.out.println("a="+i.geta());
System.out.println("b="+i.getb());
System.out.println("c="+i.getc());
System.out.println("面积area="+i.getArea());
System.out.println("周长Length="+i.getJsLength());
}
}
/**
* 利用海伦公式求面积就行了
如果周长为l,p为周长的一半,a,b,c为三边
面积s的平方 = p*(p-a)*(p-b)*(p-c)
【注意】,开发在java中使用Math。sqrt(要开平方的数)
* */
public class Triangle {
private boolean isTriangle = false; //用于判断3个点去定的3个边是否能组成三角形
private double a = 0.0d; //边a
private double b = 0.0d; //边b
private double c = 0.0d; //边c
private double l = 0.0d; //周长
private double s = 0.0f; //面积
//(x1,y1),(x2,y2),(x3,y3)分别为三角形三个顶点坐标
public Triangle(int x1, int y1, int x2, int y2, int x3, int y3){
Point p1 = new Point(x1,y1);
Point p2 = new Point(x2,y2);
Point p3 = new Point(x3,y3);
a = getSide(p1,p2); //利用两点求边
b = getSide(p1,p3);
c = getSide(p2,p3);
if(isTriangle){ //如果是三角形再求周长和面积
getL();
getS();
}
}
//利用两个点来获取边
private double getSide(Point p1, Point p2){
double sub1 = 0.0d; //横坐标之差
double sub2 = 0.0d; //纵坐标之差
sub1 = p1.x p2.x ? p1.x - p2.x : p2.x - p1.x;
sub1 = p1.y p2.y ? p1.y - p2.y : p2.y - p1.y;
return Math.sqrt((sub1*sub1 + sub2*sub2));
}
//获取周长
private void getL(){
l = a + b + c;
}
//获取面积
public void getS(){
double p = l/2;
s = Math.sqrt(p*(p-a)*(p-b)*(p-c));
}
public void print(){
System.out.println("三边长为:"+a+","+b+","+c);
System.out.println("周长为:"+l);
System.out.println("面积为:"+s);
}
//点类(仅供 Triangle 类使用的内部类)
private class Point{
public int x = 0;
public int y = 0;
public Point(int px, int py){
x = px;
y = py;
}
}
public static void main(String[] args) {
//求(1,1)、(1,4)、(5,1)三个坐标组成的三角形的周长和面积
Triangle tri = new Triangle(1,1,1,4,5,1);//实例化一个三角形
tri.print(); //输出属性
}
}
你这里没有说出这个三角形具体是哪一个三角形,所以不好写。我以等边三角形为例子。
public class Triangle{
double length;//定义边长成员变量
double bottonLength;//定义底边的长
double high;//定义高
public double getCircumference(double length){ //定义计算周长的方法
return length*3;
}
public double getArea(double bottonLength,double high){ //定义计算面积的方法
return (bottonLength*high)/2;
}
}
public class Test{
public static void main(String[] args){
Triangle t = new Triangle();
t.getCircumference(23);//调用计算周长的方法算出边长为23的三角形周长
}
}
import java.util.Scanner;
public class Test{
private static double length;//边长
private static double bottom;//底
private static double tall;//高
private String perimeter;
private String area;
//无构造器
private Test(){}
//有构造器
private void Test(double length,double bottom,double tall){
this.length=length;
this.bottom=bottom;
this.tall=tall;}
//set和get方法
public void setlength(double length){
this.length=length;}
public void setbottom(double bottom){
this.bottom=bottom;}
public void settall(double tall){
this.tall=tall;}
public double getlength(){
return this.length;}
public double getbottom(){
return this.bottom;}
public double gettall(){
return this.tall;}
//周长和面积
public double getperimeter(){
return length+length+bottom;}
public double getarea(){
return bottom*tall/2;}
public static void main(String[]args){
Scanner sc = new Scanner(System.in);
System.out.println("请输入三角形的边长");
length = sc.nextDouble();
System.out.println("请输入三角形的底");
bottom = sc.nextDouble();
System.out.println("请输入三角形的高");
tall = sc.nextDouble();
System.out.println("三角形的周长是:"+new Test().getperimeter()+"三角形的面积是"+new Test().getarea());
}
}
public class Triangle {
int x,y,z; //这是三角形三条边的长度
private void triangle(int x,int y,int z) {
this.x=x;
this.y=y;
this.z=z;
}
private int perimeter(){
return this.x+this.y+this.z;
}
private double area(){
int p = this.perimeter() / 2;
return Math.sqrt(p*(p-this.x)*(p-this.y)*(p-this.z));
}
public static void main(String[] args) throws Exception {
Triangle t = new Triangle(3,4,5);
System.out.println("该三角形的周长为:" + t.perimeter());
System.out.println("该三角形的面积为:" + t.area());
}
}
上面代码保存为Triangle.java即可测试运行~~手写代码,不保证完全正确,但思路绝对是正确的