import java.util.Scanner;
public class Perimeter {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Please enter X1: ");
double X1 = scan.nextDouble();
System.out.println("Please enter Y1: ");
double Y1 = scan.nextDouble();
System.out.println("Please enter X2: ");
double X2 = scan.nextDouble();
System.out.println("Please enter Y2: ");
double Y2 = scan.nextDouble();
System.out.println("Please enter X3: ");
double X3 = scan.nextDouble();
System.out.println("Please enter Y3: ");
double Y3 = scan.nextDouble();
finddistance(X1,Y1,X2,Y2,X3,Y3);
double answer = triperimeter(base1, base2, base3);
System.out.println(answer);
}
public static finddistance(double X1, double Y1, double X2, double Y2, double X3, double Y3){
double base1 = Math.sqrt(Math.pow((X2 - X1),2) + Math.pow((Y2 - Y1),2));
double base2 = Math.sqrt(Math.pow((X3 - X1),2) + Math.pow((Y3 - Y1),2));
double base3 = Math.sqrt(Math.pow((X3 - X2),2) + Math.pow((Y3 - Y2),2));
}
public static double triperimeter(double base1, double base2, double base3){
return (double) base1 + base2 + base3;
}
}
我们正在学习如何立即使用多种方法编写代码,我认为我的错误如下:
在方法finddistance中,我需要以某种方式与代码中的所有方法共享/返回变量base123,但是我不知道该怎么做。
当我编译这段代码时,它只会给我一个错误消息:无效的方法声明,需要返回类型。在公共静态finddistance线上。
答案 0 :(得分:1)
我怀疑您的老师想要的是拥有一种findDistance
方法,该方法只能计算一个距离,但是您会调用三次。然后,当您确定了这三种长度时,可以根据需要将它们全部添加到单独的方法中。
还请注意,通常使用Math.hypot
代替Math.sqrt
和Math.pow
更准确。
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Please enter Ax: ");
double ax = scan.nextDouble();
System.out.println("Please enter Ay: ");
double ay = scan.nextDouble();
System.out.println("Please enter Bx: ");
double bx = scan.nextDouble();
System.out.println("Please enter By: ");
double by = scan.nextDouble();
System.out.println("Please enter Cx: ");
double cx = scan.nextDouble();
System.out.println("Please enter Cy: ");
double cy = scan.nextDouble();
double lengthA = findDistance(bx, by, cx, cy);
double lengthB = findDistance(ax, ay, cx, cy);
double lengthC = findDistance(ax, ay, bx, by);
double answer = perimeter(lengthA, lengthB, lengthC);
System.out.println(answer);
}
public static double findDistance(double x1, double y1, double x2, double y2){
return Math.hypot( x2 - x1, y2 - y1 );
}
public static double perimeter(double length1, double length2, double length3) {
return length1 + length2 + length3;
}
答案 1 :(得分:0)
该错误非常明确地表明“需要返回类型”。您缺少方法finddistance
的返回类型。您可能需要修改代码:
triperimeter
方法类似这样的东西:
import java.util.Scanner;
public class Perimeter
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Please enter X1: ");
double X1 = scan.nextDouble();
System.out.println("Please enter Y1: ");
double Y1 = scan.nextDouble();
System.out.println("Please enter X2: ");
double X2 = scan.nextDouble();
System.out.println("Please enter Y2: ");
double Y2 = scan.nextDouble();
System.out.println("Please enter X3: ");
double X3 = scan.nextDouble();
System.out.println("Please enter Y3: ");
double Y3 = scan.nextDouble();
double bases[] = finddistance(X1, Y1, X2, Y2, X3, Y3);
double answer = triperimeter(bases[0], bases[1], bases[2]);
System.out.println(answer);
}
public static double[] finddistance(double X1, double Y1, double X2, double Y2, double X3, double Y3)
{
double base1 = Math.sqrt(Math.pow((X2 - X1), 2) + Math.pow((Y2 - Y1), 2));
double base2 = Math.sqrt(Math.pow((X3 - X1), 2) + Math.pow((Y3 - Y1), 2));
double base3 = Math.sqrt(Math.pow((X3 - X2), 2) + Math.pow((Y3 - Y2), 2));
return new double[] { base1, base2, base3 };
}
public static double triperimeter(double base1, double base2, double base3)
{
return (double) base1 + base2 + base3;
}
}
您还可以选择设置base1
,base2
和base3
全局静态变量。
答案 2 :(得分:0)
尝试返回双打数组
public static double [] finddistance(double X1, double Y1, double X2, double Y2, double X3, double Y3){
double [] rv = new double [3];
rv [0] = Math.sqrt(Math.pow((X2 - X1),2) + Math.pow((Y2 - Y1),2));
rv [1] = Math.sqrt(Math.pow((X3 - X1),2) + Math.pow((Y3 - Y1),2));
rv [2] = Math.sqrt(Math.pow((X3 - X2),2) + Math.pow((Y3 - Y2),2));
return rv;
}
修改
然后将您的呼叫更改为
double[] rv = finddistance(X1,Y1,X2,Y2,X3,Y3);
然后
double answer = triperimeter(rv[0], rv[1], rv[2]);
答案 3 :(得分:0)
这些行
double base1 = Math.sqrt(Math.pow((X2 - X1),2) + Math.pow((Y2 - Y1),2));
double base2 = Math.sqrt(Math.pow((X3 - X1),2) + Math.pow((Y3 - Y1),2));
double base3 = Math.sqrt(Math.pow((X3 - X2),2) + Math.pow((Y3 - Y2),2));
看起来很相似,你不觉得吗?不要尝试同时计算所有三个函数,而应尝试重用该函数,因为您可能希望以后再使用它,但只能在一个基础上使用它。
答案 4 :(得分:-1)
声明静态变量
double base1=0; double base2=0; double double base3=0;
main()之上并在finddistance()中分配值;
将返回类型设为'void'到finddistance()
答案 5 :(得分:-2)
double answer = triperimeter(base1, base2, base3);
1)参数未定义,base1,base2是什么?尝试定义
2)public static finddistance(double X1, double Y1, double X2, double Y2, double X3, double Y3)
方法声明不正确,方法应至少具有返回类型。
3)遵循方法类的命名约定标准