【问题标题】:Methods and passing a double方法和传递一个双
【发布时间】:2013-02-26 14:55:42
【问题描述】:

我不断收到有关这段代码的错误消息。我尝试声明它并以不同的方式传递它。如果代码不是那么先进,我很抱歉。我还是个初学者。

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication5;
import java.util.Scanner;

/**
 *
 * @author period3
 */
public class JavaApplication5 {

/**
 * @param args the command line arguments
 */


public static void main(String[] args) {


    double theresult;
    theresult = area(double radius);

Scanner reader;
reader = new Scanner (System.in);
System.out.println("Please enter the coordinates of a circle:");
newLine();
System.out.println("Outside point:");
newLine();
System.out.println("x1:");
int x1 = reader.nextInt();
newLine();
System.out.println("y1:");
int y1 = reader.nextInt();
newLine();
System.out.println("Center Point:");
newLine();
System.out.println("x2:");
int x2 = reader.nextInt();
newLine();
System.out.println("y2:");
int y2 = reader.nextInt();

System.out.println("The area of the circle is" + theresult);

}
 public static double distance(int x1, int y1, int x2, int y2) 
{
double dx = x2 - x1;
double dy = y2 - y1;
double dsquared = dx*dx + dy*dy;
double result = Math.sqrt (dsquared);
return result;
}

public static double area(int x1, int y1, int x2, int y2) {
double radius = distance (x1, y1, x2, y2);
return radius;
}

public static double area(double radius)
{
    double areaCircle;
    areaCircle = (3.14 * (radius * radius));
    return areaCircle;
}


//NewLine Method
public static void newLine () {
System.out.println ("");
}
}

以及我在第 24 行的错误信息(theresult = area(double radius):

unexpected type
  required: value
  found:    class

'.class' expected

';' expected
----

【问题讨论】:

    标签: class variables declare


    【解决方案1】:

    在使用面积法之前需要先计算半径:

    double radius = distance(x1, y1, x2, y2);
    

    那你需要设置结果:

    theResult = area(radius);
    

    这些都应该发生在您的用户输入之后(在他们为您提供 x1y1x2y2 的值之后,以及 您打印 theResult 之前.

    请注意我如何称呼您的 distancearea 方法。只需将局部变量输入参数即可。

    theResult = area(double radius); <- This is incorrect syntax.
    

    【讨论】:

    • 非常感谢!这很有帮助。我必须改变方法吗?或者只是以正确的语法添加结果
    • @user2105795 不,我觉得它们很好。
    猜你喜欢
    • 1970-01-01
    • 2022-10-18
    • 1970-01-01
    • 2014-01-05
    • 1970-01-01
    • 2019-03-02
    • 2016-12-21
    • 2014-10-14
    • 1970-01-01
    相关资源
    最近更新 更多