gpt4 book ai didi

java - 在线编译器不会运行多个类

转载 作者:行者123 更新时间:2023-12-01 23:09:49 26 4
gpt4 key购买 nike

我正在使用在线编译器“onlinegdp”来运行java,但我无法在编程中使用多个文件。完全相同的代码在 Eclipse 上工作,所以我不确定问题出在哪里。

我对java了解不够,无法尝试除Object myObject = newObject();之外的任何东西。 。所以任何事情都会有帮助。

这就是我的 worker 类(Class)的内容

public class Worker
{
private int hours;
private double rate;
public Worker ()
{
hours = 999;
rate = 999;
}
public Worker (int nHours, double nRate)
{
hours = nHours;
rate = nRate;
}
public int getHours ()
{
return hours;
}
public void setHours (int nHours)
{
hours = nHours;
}
public double getRate ()
{
return rate;
}
public void setRate (double nRate)
{
rate = nRate;
}
public double paycheck ()
{
return rate * hours;
}
public void raiseRate (double raiseRate)
{
rate = raiseRate + rate;
}

}

这是主类

public class Main
{

public static void main (String[]args)
{
Worker bob = new Worker ();
System.out.println (bob.getHours ());
System.out.println (bob.getRate ());
bob.setHours (9);
bob.setRate (7.9);
System.out.println (bob.getHours ());
System.out.println (bob.getRate ());
System.out.println (bob.payCheck ());
System.out.println (bob.raiseRate ());
}
}

我收到此错误
Main.java:14: 错误:找不到符号
Worker bob = new Worker();
^
符号:Worker类
位置:Main 类

最佳答案

我不知道 onlinegdp - 在线编译器如何保存另一个类文件并引用它。如果它们有功能,那么您可以尝试将它们添加到包中并使用包导入该文件。

It's pain to see the folder/project structure on most of the online compiler. They are meant to run small snippet & not to run the whole project.

下面是您可以在 onlinegdp 上运行的代码片段,而无需创建不同的文件。

public class Main
{
public static void main (String[]args)
{
Worker bob = new Worker ();
System.out.println (bob.getHours ());
}

static public class Worker
{
private int hours;
private double rate;
public Worker ()
{
hours = 999;
rate = 999;
}
public Worker (int nHours, double nRate)
{
hours = nHours;
rate = nRate;
}
public int getHours ()
{
return hours;
}
public void setHours (int nHours)
{
hours = nHours;
}
public double getRate ()
{
return rate;
}
public void setRate (double nRate)
{
rate = nRate;
}
public double paycheck ()
{
return rate * hours;
}
public void raise (double raise)
{
rate = raise + rate;
}

}

}

关于java - 在线编译器不会运行多个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58381294/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com