java编程求三角形面积
先创建一个Point类,然后定义Trianglele类。在Trianglele类中定义三个Point的实体来表示一个三角形的三个点,再定义一个方法setTri对这三个点进...
先创建一个Point类,然后定义Trianglele类。在Trianglele类中定义三个Point的实体来表示一个三角形的三个点,再定义一个方法setTri对这三个点进行初始化,然后定义两个方法求三角形的周长、面积。在main()中创建一个对象,求给定三点的三角形的周长、面积。
我写的是:
import java.lang.Math;
class Point
{
public int x,y;
public Point(int a,int b)
{
x=a;y=b;
}
}
public class sanjiaoxing {
/**
* @param args
*/
double sa,sb,sc;
class Trianglele
{
public void setTr()
{
Point A=new Point(0,0);
Point B=new Point(3,0);
Point C=new Point(0,4);
sa=Math.hypot((A.x-B.x),(A.y-B.y));
sb=Math.hypot((B.x-C.x),(B.y-C.y));
sc=Math.hypot((C.x-A.x),(C.y-A.y));
}
}
double zhouchang()
{
return sa+sb+sc;
}
double getArea()
{
double b,c;
b=(sa*sa+sb*sb-sc*sc)/(2*sa*sc);
c=sa*sc*(Math.sqrt(1-b*b))/2;
return c;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
sanjiaoxing your=new sanjiaoxing();
System.out.println("zhouchang "+your.zhouchang());
System.out.println("mianji "+your.getArea());
}
}
请问哪里错了啊? 展开
我写的是:
import java.lang.Math;
class Point
{
public int x,y;
public Point(int a,int b)
{
x=a;y=b;
}
}
public class sanjiaoxing {
/**
* @param args
*/
double sa,sb,sc;
class Trianglele
{
public void setTr()
{
Point A=new Point(0,0);
Point B=new Point(3,0);
Point C=new Point(0,4);
sa=Math.hypot((A.x-B.x),(A.y-B.y));
sb=Math.hypot((B.x-C.x),(B.y-C.y));
sc=Math.hypot((C.x-A.x),(C.y-A.y));
}
}
double zhouchang()
{
return sa+sb+sc;
}
double getArea()
{
double b,c;
b=(sa*sa+sb*sb-sc*sc)/(2*sa*sc);
c=sa*sc*(Math.sqrt(1-b*b))/2;
return c;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
sanjiaoxing your=new sanjiaoxing();
System.out.println("zhouchang "+your.zhouchang());
System.out.println("mianji "+your.getArea());
}
}
请问哪里错了啊? 展开
展开全部
楼主sanjiaoxing这个类里面的Trianglele类没有被执行到,可以把Trianglele类里面的内容写在sanjiaoxing这个类的构造方法里,这样就在NEW这个sanjiaoxing类时初始化三个点,不过这样就定死了..如果想要手动传参的话,可以另外定一个方法来实现初始化,然后在main方法里面调用一下.把调试好的代码给你贴下,希望可以帮到你...
package cd.itcast.test;
import java.lang.Math;
class Point {
public int x, y;
public Point(int a, int b) {
x = a;
y = b;
}
}
public class sanjiaoxing {
/**
* @param args
*/
double sa, sb, sc;
public sanjiaoxing() {
Point A = new Point(0, 0);
Point B = new Point(3, 0);
Point C = new Point(0, 4);
sa = Math.hypot((A.x - B.x), (A.y - B.y));
sb = Math.hypot((B.x - C.x), (B.y - C.y));
sc = Math.hypot((C.x - A.x), (C.y - A.y));
System.out.println(sa+sb+sc);
}
double zhouchang() {
return sa + sb + sc;
}
double getArea() {
double b, c;
b = (sa * sa + sb * sb - sc * sc) / (2 * sa * sc);
c = sa * sc * (Math.sqrt(1 - b * b)) / 2;
return c;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
sanjiaoxing your = new sanjiaoxing();
System.out.println("zhouchang " + your.zhouchang());
System.out.println("mianji " + your.getArea());
}
}
package cd.itcast.test;
import java.lang.Math;
class Point {
public int x, y;
public Point(int a, int b) {
x = a;
y = b;
}
}
public class sanjiaoxing {
/**
* @param args
*/
double sa, sb, sc;
public sanjiaoxing() {
Point A = new Point(0, 0);
Point B = new Point(3, 0);
Point C = new Point(0, 4);
sa = Math.hypot((A.x - B.x), (A.y - B.y));
sb = Math.hypot((B.x - C.x), (B.y - C.y));
sc = Math.hypot((C.x - A.x), (C.y - A.y));
System.out.println(sa+sb+sc);
}
double zhouchang() {
return sa + sb + sc;
}
double getArea() {
double b, c;
b = (sa * sa + sb * sb - sc * sc) / (2 * sa * sc);
c = sa * sc * (Math.sqrt(1 - b * b)) / 2;
return c;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
sanjiaoxing your = new sanjiaoxing();
System.out.println("zhouchang " + your.zhouchang());
System.out.println("mianji " + your.getArea());
}
}
追问
真的没有执行到嘞,如果我想像题目那样定义一个Trianglele类,怎么做才能执行到它呢?是要new一下这个类吗?(我刚学java,头疼死了。。。。。)
追答
如果你要执行到他,可以定义在sanjiaoxing这个类里面,把他定义成一个方法,然后在MAIN函数里面调用一下,如果要另外创建一个类,也可以,如果不想在MAIN方法里面NEW,可以把他定义成静态的,调用一下就OK了...比如:
class Trianglele{
public Trianglele(){
Point A = new Point(0, 0);
Point B = new Point(3, 0);
Point C = new Point(0, 4);
sa = Math.hypot((A.x - B.x), (A.y - B.y));
sb = Math.hypot((B.x - C.x), (B.y - C.y));
sc = Math.hypot((C.x - A.x), (C.y - A.y));
System.out.println(sa+sb+sc);
}
}
这样定义之后在main里面new一下就好.如果不想写死三个点,可以在new的时候把参数传到构造方法里面.希望对你有帮助吧..
展开全部
代码有些乱,面积公式有错误
用海伦公式:s=根号下p(p-a)(p-b)(p-c),其中,p=(a+b+c)/2
class Point {
public int x, y;
public Point(int a, int b) {
x = a;
y = b;
}
}
public class sanjiaoxing {
double sa, sb, sc;
public sanjiaoxing() {
Point A = new Point(0, 0);
Point B = new Point(3, 0);
Point C = new Point(0, 4);
sa = Math.hypot((A.x - B.x), (A.y - B.y));
sb = Math.hypot((B.x - C.x), (B.y - C.y));
sc = Math.hypot((C.x - A.x), (C.y - A.y));
}
double zhouchang(){
return sa + sb + sc;
}
double getArea() {
double b, c;
b = (sa+sb+sc) /2;
c =Math.sqrt(b*(b-sa)*(b-sb)*(b-sc));
return c;
}
public static void main(String[] args) {
sanjiaoxing your = new sanjiaoxing();
System.out.println("周长:" + your.zhouchang());
System.out.println("面积:" + your.getArea());
}
}
//运行结果:
run:
周长:12.0
面积:6.0
成功生成(总时间:0 秒)
用海伦公式:s=根号下p(p-a)(p-b)(p-c),其中,p=(a+b+c)/2
class Point {
public int x, y;
public Point(int a, int b) {
x = a;
y = b;
}
}
public class sanjiaoxing {
double sa, sb, sc;
public sanjiaoxing() {
Point A = new Point(0, 0);
Point B = new Point(3, 0);
Point C = new Point(0, 4);
sa = Math.hypot((A.x - B.x), (A.y - B.y));
sb = Math.hypot((B.x - C.x), (B.y - C.y));
sc = Math.hypot((C.x - A.x), (C.y - A.y));
}
double zhouchang(){
return sa + sb + sc;
}
double getArea() {
double b, c;
b = (sa+sb+sc) /2;
c =Math.sqrt(b*(b-sa)*(b-sb)*(b-sc));
return c;
}
public static void main(String[] args) {
sanjiaoxing your = new sanjiaoxing();
System.out.println("周长:" + your.zhouchang());
System.out.println("面积:" + your.getArea());
}
}
//运行结果:
run:
周长:12.0
面积:6.0
成功生成(总时间:0 秒)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你去死.....................................................................................................................................
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询