非常基础的Java [英] Very Basic Java

查看:59
本文介绍了非常基础的Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的新手.这是我的代码:

I'm very new to Java. Here is my code:

public class funk {

  int largest(int a,int b,int c) 
  {

    if(a>b)
    { 
      if(a>c)
      {
        return a;
      }
      else if(b>c)
      {
        return b;
      }
    else 
    {
      return c;
    }
  }
}

public class firstprog {

  public static  void main(String args[]) {

    int a=7;
    int b=8;
    int c=9;
    funk punk=new funk();
    System.out.println(punk.largest(a,b,c));
    
  }

}

Eclipse给我的错误是公共类型funk必须在其自己的文件中定义.

The error that Eclipse gives me is The public type funk must be defined in its own file.

为什么会这样?

推荐答案

是的,问题是每个文件只能有一个公共类,并且该文件的名称应与该类的名称相同.您可以只删除第一类定义前面的public.一种更好的方法是将其设为主类的静态方法.

Yes the problem is that you can only have one public class per file and this file should have the same name than the class. You can just remove the public in front of the definition of the first class. A better way to do would be to make it a static method of the main class.

要解决第二个问题,您可以执行以下操作:

To solve you second problem you can do this:

public class firstprog {


    public static int largest(int a,int b,int c) 
    {
        if(a>b)
        { 
            if(a>c)
                return a;
            else 
                if(b>c)
                    return b;
                else 
                    return c;
        }
        else
        {
            if(b>c)
                return b;
            else 
                return c;
        }
    }   

    public static  void main(String args[]) {

        int a=19;
        int b=2;
        int c=1;

        System.out.println(largest(a,b,c));  
  }
}

这篇关于非常基础的Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆