试图纠正一个程序,甚至无法让它运行

发布于 2024-11-05 01:52:41 字数 1641 浏览 4 评论 0原文

我正在使用 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);

      }
}
}

更新的屏幕截图:

https://i.sstatic.net/SX55P.png

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:

https://i.sstatic.net/SX55P.png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清醇 2024-11-12 01:52:41

你的类名是 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.

勿忘初心 2024-11-12 01:52:41

除了前面的答案提到的文件名问题之外,您还需要将类 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.

欢你一世 2024-11-12 01:52:41

也许你想要这样的东西:

package Rational;

public class Rational {
int x, y;

public Rational() {
    this.x = 0;
    this.y = 0;
}

public static void print(Rational r)
{       
    System.out.println("" + (r.x + r.y));
}

// other stuff

public static void main(String args[]) {
    Rational g = new Rational();
    g.x = 1;
    g.y = 2;

    Rational.print(g);


}
}

May be you wanted something like this:

package Rational;

public class Rational {
int x, y;

public Rational() {
    this.x = 0;
    this.y = 0;
}

public static void print(Rational r)
{       
    System.out.println("" + (r.x + r.y));
}

// other stuff

public static void main(String args[]) {
    Rational g = new Rational();
    g.x = 1;
    g.y = 2;

    Rational.print(g);


}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文