这是我重新编写的新编码:
ThreeDPoint.java
public class ThreeDPoint{
private double x, y, z;
public threeDPoint(){
coordX=0;
coordY=0;
coordZ=0;
}
public threeDPoint(double x, double y, double z){
coordX = x;
coordY = y;
coordZ = z;
}
public double getCoord(double x, double y, double z){
return coordX;
return coordY;
return coordZ;
}
}
Main.java
import java.util.Scanner;
public class Main {
double coordX, coordY, coordZ;
public static void main(String[] args){
threeDPoint myThreeDPoint=new threeDPoint();
Scanner sc=new Scanner(System.in);
String coordX, coordY, coordZ;
System.out.println("Input Coordinate X");
myThreeDPoint.coordX(sc.nextDouble());
System.out.println("Input Coordinate Y");
myThreeDPoint.coordY(sc.nextDouble());
System.out.println("Input Coordinate Z");
myThreeDPoint.coordZ(sc.nextDouble());
}
}
它给了我这些错误: 构建输出: C:\ Users \ BurneySoo \ Documents \ Main.java:8:error:找不到符号 threeDPoint myThreeDPoint = new threeDPoint(); ^ 符号:class threeoint 位置:班级主要 C:\ Users \ BurneySoo \ Documents \ Main.java:8:error:找不到符号 threeDPoint myThreeDPoint = new threeDPoint(); ^ 符号:class threeoint 位置:班级主要 2个错误
一般输出: 错误:无法找到或加载主类
我做错了什么?
非常感谢Gagandeep代码。不知怎的,我尝试了它们,但它发出的错误比以前更多。
C:\ Users \ BurneySoo \ Documents \ ThreeDPoint.java:5:错误:方法声明无效;需要返回类型
public threeDPoint(){
^
C:\ Users \ BurneySoo \ Documents \ ThreeDPoint.java:11:错误:方法声明无效;需要返回类型
public threeDPoint(double x, double y, double z){
^
C:\ Users \ BurneySoo \ Documents \ Main.java:14:错误:找不到符号
myTreeDPoint.coordX(sc.nextLine());
^
symbol:变量myTreeDPoint location:class Main
C:\ Users \ BurneySoo \ Documents \ Main.java:17:错误:找不到符号
myTreeDPoint.coordY(sc.nextLine());
^
symbol:变量myTreeDPoint location:class Main
C:\ Users \ BurneySoo \ Documents \ Main.java:20:error:找不到符号
myTreeDPoint.coordZ(sc.nextLine());
^
symbol:变量myTreeDPoint location:class Main
C:\ Users \ BurneySoo \ Documents \ Main.java:22:错误:非静态变量coordY无法从静态上下文引用
myThreeDPoint.setCoord(coordX, coordY, coordZ);
^
C:\ Users \ BurneySoo \ Documents \ Main.java:22:错误:无法从静态上下文引用非静态变量coordZ
myThreeDPoint.setCoord(coordX, coordY, coordZ);
^
C:\ Users \ BurneySoo \ Documents \ Main.java:22:错误:类ThreeCooint中的方法setCoord不能应用于给定的类型;
myThreeDPoint.setCoord(coordX, coordY, coordZ);
^
必需:双倍,双倍,双倍 发现:String,double,double reason:实际参数String不能通过方法调用转换转换为double
C:\ Users \ BurneySoo \ Documents \ ThreeDPoint.java:6:错误:找不到符号
coordX=0;
^
符号:变量coordX location:class ThreeDPoint
C:\ Users \ BurneySoo \ Documents \ ThreeDPoint.java:7:错误:找不到符号
coordY=0;
^
符号:变量coordY location:class ThreeDPoint
C:\ Users \ BurneySoo \ Documents \ ThreeDPoint.java:8:error:找不到符号
coordZ=0;
^
符号:变量coordZ location:class ThreeDPoint
C:\ Users \ BurneySoo \ Documents \ ThreeDPoint.java:12:错误:找不到符号
coordX = x;
^
符号:变量coordX location:class ThreeDPoint
C:\ Users \ BurneySoo \ Documents \ ThreeDPoint.java:13:错误:找不到符号
coordY = y;
^
符号:变量coordY location:class ThreeDPoint
C:\ Users \ BurneySoo \ Documents \ ThreeDPoint.java:14:错误:找不到符号
coordZ = z;
^
符号:变量coordZ location:class ThreeDPoint
14个错误
它仍然不会提示我在常规输出中输入坐标X. 它只给了我:
错误:无法找到或加载主类
答案 0 :(得分:0)
这里的问题是你没有调用构造函数,而是试图调用方法。
为您的threeDPoint类创建一个构造函数,然后使用setter设置字段coordX。你不能打电话
threeDPoint myThreeDPoint=new coordX();
因为这是一个方法,而不是构造函数。你想要的是你的threeDPoint类中的构造函数,如下所示:
public threeDPoint(args){
//dostuff
}
然后设置你的Coords就像
public void setCoordx(double coordx){
this.coordx = coordx}
此外,作为数据隐藏的问题,请务必将您的字段视为私有或受保护。
protected double coordX;
答案 1 :(得分:0)
你必须写
threeDPoint myThreeDPoint=new threeDPoint("Send Arguments Here");
在main方法中,要创建一个对象,现在使用此对象,您必须访问方法myThreeDPoint.coordX(11.11);
将您的ThreeDPoint
课程更改为:
public class ThreeDPoint
{
private double x, y, z;
public ThreeDPoint()
{
this.x = 0;
this.y = 0;
this.z = 0;
}
public ThreeDPoint(double x, double y, double z)
{
this.x = x;
this.y = y;
this.z = z;
}
public double getCoordX()
{
return this.x;
}
public double getCoordY()
{
return this.y;
}
public double getCoordZ()
{
return this.z;
}
}
现在将此类的对象设为
ThreeDPoint myThreeDPoint = new ThreeDPoint();
所以你的主类看起来像这样:
import java.util.Scanner;
public class Main
{
double coordX, coordY, coordZ;
public static void main(String[] args)
{
ThreeDPoint myThreeDPoint = new ThreeDPoint();
Scanner sc=new Scanner(System.in);
String coordX, coordY, coordZ;
System.out.println("Input Coordinate X");
coordX = sc.nextDouble();
System.out.println("Input Coordinate Y");
coordY = sc.nextDouble();
System.out.println("Input Coordinate Z");
coordZ = sc.nextDouble();
// This will initialize your Constructor with Arguments.
ThreeDPoint myThreeDPointArgConst = new ThreeDPoint(coordX, coordY, coordZ);
coordX = myThreeDPointArgConst.getCoordX();
System.out.println("Co-ordinate X is : " + coordX);
coordY = myThreeDPointArgConst.getCoordY();
System.out.println("Co-ordinate Y is : " + coordY);
coordZ = myThreeDPointArgConst.getCoordZ();
System.out.println("Co-ordinate Z is : " + coordZ);
// This will initialize the no-arg constructor.
ThreeDPoint myThreeDPoint = new ThreeDPoint();
coordX = myThreeDPoint.getCoordX();
System.out.println("Co-ordinate X is : " + coordX);
coordY = myThreeDPoint.getCoordY();
System.out.println("Co-ordinate Y is : " + coordY);
coordZ = myThreeDPoint.getCoordZ();
System.out.println("Co-ordinate Z is : " + coordZ);
}
}