错误:"该方法未定义类型"和"构造函数未定义"

时间:2014-04-17 03:12:16

标签: java eclipse error-handling

当我在代码中的某些位置遇到以下错误时,我正在编写程序:

第一个错误是:“构造函数Gerbil(String,String,int [],boolean,boolean,String [])未定义”,代码如下:gerbil [i] = new Gerbil(n5,n6,amountfood) ,逃避[i],咬[i],食物);

第二个错误是:“方法getTypeFood()未定义类型Gerbil”,代码如下:String [] food = g.getTypeFood();

这是我的整个程序代码(2个不同的类)

第一堂课:

    import java.util.Scanner;

    public class Gerbilfood {
    static int n8;
    static int n3;
    static String n55;
    static String n35;
    static String n2;
    public static Gerbil[] gerbil;
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.println("Please input how many types of food items the  gerbils           eat as an integer");
        String n0 = scanner.nextLine();
        int n1 = Integer.parseInt(n0);

        String[] food = new String[n1];

        for (int i = 0; i < n1; i++) {

            System.out.println("Please enter a food name");

            String n2 = scanner.nextLine();

            food[i] = n2;

            int[] maximum = new int[n1];

            System.out.println("Please enter maximum amount of this food per day");
            String n33 = scanner.nextLine();
            int n3 = Integer.parseInt(n33);
            maximum[i] = n3;
        }

        System.out.println("Please enter in the number of gerbils in the lab");
        String n73 = scanner.nextLine();
        int n4 = Integer.parseInt(n73);
        //gerbil = new Gerbil[n4];

         gerbil = new Gerbil[n4];

        int[] combo = new int[n4];
        String[] ids = new String[n4];
        for (int i = 0; i < n4; i++) {

            //Gerbil g = new Gerbil(n1);

            System.out.println("Please enter in the lab id for one of the gerbils");
            String n5 = scanner.nextLine();
            //g.setId(n5);

            //ids[i] = n5;

            //String[] names = new String[n4];

            System.out.println("Please enter in the name given to gerbil whose lab id you just entered");
            String n6 = scanner.nextLine(); // gerbil name
            //g.setName(n6);

            //String[] amountfood = new String[n1];
            int [] amountfood = new int[n1];

            for (int j = 0; j < n1; j++) {
                System.out.println("how much of " +food[j]

                        + " did this gerbil eat");
                String n8 = scanner.nextLine();
                //amountfood[j = n8;
                amountfood[j] = Integer.parseInt(n8);

            }
            boolean[] bite = new boolean[n4];
                System.out
                        .println("Does this Gerbil bite? Enter True or False");
                String n77 = scanner.nextLine();
                if (n77.equalsIgnoreCase("True"))

                bite[i] = true;
                else{
                    bite[i] = false;
                }

                boolean[]escape = new boolean[n4];
                System.out
                          .println("Does this Gerbil escape? Enter True or False");
            String n89 = scanner.nextLine();

            if (n89.equalsIgnoreCase("True"))

            escape[i] = true;

            else{ 
                escape[i] = false;
            }


            gerbil[i] = new Gerbil(n5,n6,amountfood, escape[i], bite[i],food);

            }



            System.out.println("What information would you like to know?");
            String n55 = scanner.nextLine();

            String n33 = "search";
            String n34 = "average";
            String n35 = "restart";
            String n36 = "quit";

            if(n55.equalsIgnoreCase(n34)){
            System.out.println( averagefood());

        }
            else{
                if(n55.equalsIgnoreCase(n33)){

                    System.out.println("Please type the lab id of the gerbil you wish to search for");

                    String n87 = scanner.nextLine();

                      Gerbil g = searchForGerbil(n87);

                    Gerbil  gerbilattributes=searchForGerbil(n87);

                    String gerbid = g.getId();

                    String gerbname = g.getName();

                    boolean gerbbite = g.getBite();

                    boolean gerbescape = g.getEscape();



                    for (int i = 0; i<n1; i++)

                    String[]food = g.getTypeFood();

                    int[]   gerbfoods = g.getAmountFood();

                    for(int i = 0; i < n1; i++)



                    System.out.println(gerbid +" bite = "+ gerbbite + "             " + gerbname + "escape = " + gerbescape + " " + gerbfoods);



                }
                else{
                    if (n55.equalsIgnoreCase(n35)){

                    //GO BACK
                    }
                    else{
                        if (n55.equalsIgnoreCase(n36)){

                            System.exit(0);
                        }
                        else{
                            System.out.println("ERROR");
                        }
                    }
                }
            }
}








    public static String averagefood() {

        int i = 0;
        Gerbil g = gerbil[i];
       String gid = g.getId();

        String gname = g.getName();
        long percent = Math.round(n8 * 100.0 / n3);

        String everything = gid + " " + gname + " " + percent + "\n";
        for ( i = 0; i <=gerbil.length; i++) {







            //turn everything;
             } 


        return everything;
    }

    public static Gerbil searchForGerbil(String n87) {


            for(int i = 0; i< gerbil.length; i++){
                Gerbil g = gerbil[i];

                if(n87.equals(g.getId())){
                return gerbil[i]; 



            }


        // return (new Gerbil[i]);

    } return null;

}
}

second class:

public class Gerbil {

    private String id;
    private String name;
    private int[] amountfood;
    private int numbergerbils;
    private String[] food;

    private boolean escape;
    private boolean bite;

    public Gerbil(String n5, String n6, int[]numOfFood, boolean newEscape, boolean newBite, String[] n2) {

        id = n5;
        name = n6;
        amountfood = numOfFood;
        escape = newEscape;
        bite = newBite;
        food = n2;
    }

    public Gerbil(String[] typefood){

        food = typefood;
    }
    public Gerbil(int [] numOfFood) {
        amountfood = numOfFood;


    }


public int[]  getAmountFood(){


    return  amountfood;
}

public boolean getBite(){

return bite;
}

public boolean getEscape(){

return escape;
}
    public String getId() {




        return id;
    }

    public String getName() {
        return name;
    }

    public void setId(String newId) {
        id = newId;
    }

    public void setName(String newName) {
        name = newName;
    }

public String [] getTypeFood(){

        return food;

        return null;
    }

}

请输入沙土鼠吃多少种食物

2

请输入食物名称

面包

请每天输入此食物的最高金额

5

请输入食物名称

大蒜

请每天输入此食物的最高金额

6

请输入实验室中的沙鼠数量

2

请输入其中一只沙鼠的实验室ID

hgklll

请输入您刚刚输入实验室ID的沙鼠名称

拉​​里

这种沙鼠吃了多少面包

1

这种沙鼠吃了多少大蒜

1

这只沙鼠咬伤了吗?输入True或False

这只沙鼠逃脱了吗?输入True或False

请输入其中一只沙鼠的实验室ID

hjdddd

请输入您刚刚输入实验室ID的沙鼠名称

戴夫

这种沙鼠吃了多少面包

1

这种沙鼠吃了多少大蒜

1

这只沙鼠咬伤了吗?输入True或False

这只沙鼠逃脱了吗?输入True或False

您想知道哪些信息?

搜索

请输入您要搜索的沙鼠的实验室ID

hjdddd hjdddd bite = false daveescape = true [I @ 629e5e21 hjdddd bite = false daveescape = true [I @ 629e5e21

对于最后一部分(请输入您要搜索的实验室ID),我试图让它返回字符串中的食物名称和数量。示例:姓名:Big Bertha(将逃脱,不会咬人),食物:红色药丸 - 25/50,蓝色药丸 - 50/100

2 个答案:

答案 0 :(得分:0)

假设您在两个单独的,适当命名的文件中有两个类,这里​​有两个问题:

在Gerbilfood,您尝试重新定义可变食物的main方法:

String[] food = new String[n1];
...
String[]food = g.getTypeFood();

在Gerbil中,您的getTypeFood方法有两个返回值:

public String [] getTypeFood(){
    return food;
    return null;
}

首先解决这两个问题。

答案 1 :(得分:0)

乍一看下面的方法是错误的,因此Gerbil类不会编译。

public String [] getTypeFood(){

    return food;

    return null;
}

删除其中一个return语句。

public String [] getTypeFood(){
  return food;

}

完整代码:

public class Gerbil {

private String id;
private String name;
private int[] amountfood;
private int numbergerbils;
private String[] food;

private boolean escape;
private boolean bite;

public Gerbil(String n5, String n6, int[] numOfFood, boolean newEscape, boolean newBite, String[] n2) {

    id = n5;
    name = n6;
    amountfood = numOfFood;
    escape = newEscape;
    bite = newBite;
    food = n2;
}

public Gerbil(String[] typefood) {

    food = typefood;
}

public Gerbil(int[] numOfFood) {
    amountfood = numOfFood;

}

public int[] getAmountFood() {

    return amountfood;
}

public boolean getBite() {

    return bite;
}

public boolean getEscape() {

    return escape;
}

public String getId() {

    return id;
}

public String getName() {
    return name;
}

public void setId(String newId) {
    id = newId;
}

public void setName(String newName) {
    name = newName;
}

public String[] getTypeFood() {

    return food;


}

}



import java.util.Scanner;

public class Gerbilfood {

static int n8;
static int n3;
static String n55;
static String n35;
static String n2;
public static Gerbil[] gerbil;

public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);

    System.out.println("Please input how many types of food items the  gerbils           eat as an integer");
    String n0 = scanner.nextLine();
    int n1 = Integer.parseInt(n0);

    String[] food = new String[n1];

    for (int i = 0; i < n1; i++) {

        System.out.println("Please enter a food name");

        String n2 = scanner.nextLine();

        food[i] = n2;

        int[] maximum = new int[n1];

        System.out.println("Please enter maximum amount of this food per day");
        String n33 = scanner.nextLine();
        int n3 = Integer.parseInt(n33);
        maximum[i] = n3;
    }

    System.out.println("Please enter in the number of gerbils in the lab");
    String n73 = scanner.nextLine();
    int n4 = Integer.parseInt(n73);
    //gerbil = new Gerbil[n4];

    gerbil = new Gerbil[n4];

    int[] combo = new int[n4];
    String[] ids = new String[n4];
    for (int i = 0; i < n4; i++) {

        //Gerbil g = new Gerbil(n1);
        System.out.println("Please enter in the lab id for one of the gerbils");
        String n5 = scanner.nextLine();
        //g.setId(n5);

        //ids[i] = n5;
        //String[] names = new String[n4];
        System.out.println("Please enter in the name given to gerbil whose lab id you just entered");
        String n6 = scanner.nextLine(); // gerbil name
        //g.setName(n6);

        //String[] amountfood = new String[n1];
        int[] amountfood = new int[n1];

        for (int j = 0; j < n1; j++) {
            System.out.println("how much of " + food[j]
                    + " did this gerbil eat");
            String n8 = scanner.nextLine();
            //amountfood[j = n8;
            amountfood[j] = Integer.parseInt(n8);

        }
        boolean[] bite = new boolean[n4];
        System.out
                .println("Does this Gerbil bite? Enter True or False");
        String n77 = scanner.nextLine();
        if (n77.equalsIgnoreCase("True")) {
            bite[i] = true;
        } else {
            bite[i] = false;
        }

        boolean[] escape = new boolean[n4];
        System.out
                .println("Does this Gerbil escape? Enter True or False");
        String n89 = scanner.nextLine();

        if (n89.equalsIgnoreCase("True")) {
            escape[i] = true;
        } else {
            escape[i] = false;
        }

        gerbil[i] = new Gerbil(n5, n6, amountfood, escape[i], bite[i], food);

    }

    System.out.println("What information would you like to know?");
    String n55 = scanner.nextLine();

    String n33 = "search";
    String n34 = "average";
    String n35 = "restart";
    String n36 = "quit";

    if (n55.equalsIgnoreCase(n34)) {
        System.out.println(averagefood());

    } else {
        if (n55.equalsIgnoreCase(n33)) {

            System.out.println("Please type the lab id of the gerbil you wish to search for");

            String n87 = scanner.nextLine();

            Gerbil g = searchForGerbil(n87);

            Gerbil gerbilattributes = searchForGerbil(n87);

            String gerbid = g.getId();

            String gerbname = g.getName();

            boolean gerbbite = g.getBite();

            boolean gerbescape = g.getEscape();

            for (int i = 0; i < n1; i++) {
                food = g.getTypeFood();
            }


                int[] gerbfoods = g.getAmountFood();

             System.out.print("Lab :"+gerbid + " Name:"+ gerbname + " ("+ ((gerbbite==true)?"will bite":"will not bite")  +  "," + ((gerbescape==true)?"will escape":"will not escape") + ")");
            for (int i = 0; i < n1; i++) {
                System.out.print( " " + food[i] + ":"+ gerbfoods[i]);
            }

        } else {
            if (n55.equalsIgnoreCase(n35)) {

                //GO BACK
            } else {
                if (n55.equalsIgnoreCase(n36)) {

                    System.exit(0);
                } else {
                    System.out.println("ERROR");
                }
            }
        }
    }
}

public static String averagefood() {

    int i = 0;
    Gerbil g = gerbil[i];
    String gid = g.getId();

    String gname = g.getName();
    long percent = Math.round(n8 * 100.0 / n3);

    String everything = gid + " " + gname + " " + percent + "\n";
    for (i = 0; i <= gerbil.length; i++) {

        //turn everything;
    }

    return everything;
}

public static Gerbil searchForGerbil(String n87) {

    for (int i = 0; i < gerbil.length; i++) {
        Gerbil g = gerbil[i];

        if (n87.equals(g.getId())) {
            return gerbil[i];

        }

    // return (new Gerbil[i]);
    }
    return null;

}
}