试图纠正一个程序,甚至无法让它运行
我正在使用 Netbeans,并尝试运行一个“Rational”程序,以便我可以看到需要修复的内容。然而,当我尝试运行它时,我收到错误“Rational.Main 类在 Rational 项目中未找到”。我已经尝试重命名程序的几个方面,以使其看到主类(我向你保证,它就在那里),但它仍然给出此错误。我以前见过它,但这是唯一一次它似乎没有及时修复。
编辑:这比我想象的更有问题,这是更新的代码。是的,这是非常错误的。
package Rational;
public class Rational {
int x, y;
public Rational () {
this.x = 0;
this.y = 0;
}
public static void printRational (Rational x) {
System.out.println (x);
}
public Rational (int x, int y) {
this.x = x;
this.y = y;
}
public static void negate (int x) {
x = -x;
System.out.println (x);
}
public static void invert (int x, int y) {
int g = x;
x = y;
y = g;
System.out.print (x);
System.out.print ("/");
System.out.println (y);
}
public static void toDouble (int x, int y) {
double f = x/y;
System.out.println (f);
}
public static int GCD(int a, int b)
{
if (b==0) return a;
return GCD(b,a%b);
}
public static void reduce (int x, int y) {
x = x/(GCD (x,y));
y = y/(GCD (x,y));
System.out.print (x);
System.out.print ("/");
System.out.println (y);
}
public static void add (int x, int y) {
double z = x+y;
System.out.println (z);
}
public static void main(String args[]) {
Rational g = new Rational ();{
g.x = 1;
g.y = 2;
System.out.println ("vgds");
//Rational.printRational (g);
}
}
}
更新的屏幕截图:
I'm using Netbeans, and trying to get a program "Rational" to run, so that I can see what needs to be fixed. However, when I try to run it, I get the error "Rational.Main class wasn't found in Rational project". I've tried renaming several aspects of the program to make it see the main class (it is there, I assure you), but it still gives this error. I've seen it before, but this is the only time it hasn't seemed to fix itself in time.
Edit: This is more problematic than I thought, here's the updated code. Yes, it's very wrong.
package Rational;
public class Rational {
int x, y;
public Rational () {
this.x = 0;
this.y = 0;
}
public static void printRational (Rational x) {
System.out.println (x);
}
public Rational (int x, int y) {
this.x = x;
this.y = y;
}
public static void negate (int x) {
x = -x;
System.out.println (x);
}
public static void invert (int x, int y) {
int g = x;
x = y;
y = g;
System.out.print (x);
System.out.print ("/");
System.out.println (y);
}
public static void toDouble (int x, int y) {
double f = x/y;
System.out.println (f);
}
public static int GCD(int a, int b)
{
if (b==0) return a;
return GCD(b,a%b);
}
public static void reduce (int x, int y) {
x = x/(GCD (x,y));
y = y/(GCD (x,y));
System.out.print (x);
System.out.print ("/");
System.out.println (y);
}
public static void add (int x, int y) {
double z = x+y;
System.out.println (z);
}
public static void main(String args[]) {
Rational g = new Rational ();{
g.x = 1;
g.y = 2;
System.out.println ("vgds");
//Rational.printRational (g);
}
}
}
Updated screenshot:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的类名是 Rational 但你的文件名是 Main.java
只要让它们相同你的问题就会得到解决。
对于 java 中的任何公共类,文件名和类名应该相同,而且,
一个文件只能包含一个公共类。
Your class name is Rational but your file name is Main.java
Just make them same your problem will be solved.
For any public class in java the filename and the class name should be same and also,
a file can contain only one public class.
除了前面的答案提到的文件名问题之外,您还需要将类 Rational 公开,并且如果您将包名与类名相同,您可能会遇到问题。
文件名应该是 Rational.java
签名应该是:
public class Rational
包名称不应该也是 Rational。如果您愿意,可以使用小写的“有理数”。
In addition to the file name problem the previous answer alludes to, you also need to make the class Rational public, and you're probably going to have problems if you make the package name the same as the class name.
The filename should be Rational.java
The signature should be:
public class Rational
The package name should not also be Rational. You can use lower case 'rational' if you like.
也许你想要这样的东西:
May be you wanted something like this: