我的方法中使用的变量显示错误,未解决

时间:2014-04-11 16:13:58

标签: java eclipse syntax-error

所以,我在我的方法中使用了一些变量,但是我得到了错误,我不知道如何解决这个问题,这是我第一堂课的代码:

import java.util.Scanner;

public class Gerbilfood {
    public static void main(String[] args) {
        Gerbil[] gerbil;
        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 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];

        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 each gerbil");
            String n5 = scanner.nextLine();
            g.setId(n5);

            //ids[i] = n5;

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

            System.out.println("Please enter in the name of each gerbil");
            String n6 = scanner.nextLine(); // gerbil name
            g.setName(n6);

            String[] amountfood = new String[n1];
            for (int j = 0; j < n1; j++) {
                System.out.println("how much of" + food[j] + "did the gerbil eat");
                String n8 = scanner.nextLine();
                amountfood[i] = n8;
            }
            String[] bite = new String[n4];
            System.out.println("Does this Gerbil bite? Enter True or False");
            String n77 = scanner.nextLine();
            bite[i] = n77;

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

        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();

                System.out.println();
        } else {
            if (n55.equalsIgnoreCase(n35)) {
                //go back to beginning of program
            } else {
                if (n55.equalsIgnoreCase(n36)) {
                    System.exit(0);
                } else {
                    System.out.println("ERROR");
                }
            }
        }
    }               
}

public static int averagefood(int n4, int n3, int n8) {
    for (int i = 0; i < n4; i++) {
        long percent = Math.round(n8 * 100.0 / n3);

        return averagefood(newName, newId, percent);
    }
}

public static int searchForGerbil() {
    n87 = setId;        
    return 0;
    // return (new Gerbil[i]

并且是我第二堂课的代码:

public class Gerbil {
    private String id;
    private String name;
    private int[] amountfood;
    //private int numbergerbils;
    //private int maxfood;

    private boolean escape;
    private boolean bite;

    public Gerbil(String n5, String n6, int numOfFood, boolean newEscape, boolean newBite) {
        id = n5;
        name = n6;
        amountfood = new int[numOfFood];
        escape = newEscape;
        bite = newBite;
    }

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

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

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

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

1 个答案:

答案 0 :(得分:0)

  

它在System.out.println(averagefood())旁边说;方法   Gerbilfood类型中的averagefood(int,int,int)不适用   对于arguments()

如果你检查你的代码,你的函数定义需要三个参数,而你还没有传递任何参数。

  

getName无法解析为变量

这是Gerbil类中的一个函数,因此需要调用如下: Gerbil gb =新的Gerbil(); gb.getName();

  

令牌上的语法错误&#34;返回&#34;,此令牌后的名称 - getId   无法解析变量 - newId无法解析为a   变量 - newName无法解析为接下来的变量   返回averagefood(newName,newId,percent);

这些变量都没有在Gerbilfood类中定义。您期望使用这些变量传递什么值?