繁体   English   中英

在原始类型int上调用构造函数

[英]invoking constructor on primitive type int

我收到这个特定代码行的错误

y = totalfood[c].getAmountFood() + y;

在下面的方法中

public static String averageFood(){
    int x = 0;
    int y = 0;
    int percent = 0;
    String average = null;
            for(int a = 0; a < population; a++){
                    gerbil[a].getfoodeaten();
                    for (int b=0; b < foodeats.length; b++){
                            x += foodeats[b+1];
                    }
                    for( int c = 0; c < maxfood.length; c++){
                            y = totalfood[c].getAmountFood() + y;
                    }
             percent = (int)((x/y*100));
             average = gerbil[a].getId()+" ("+gerbil[a].getName()+" "+percent+"%";
             System.out.println(average);
            }
            return average;
}

错误消息读取Cannot invoke getAmountFood() on the primitive type int 这是什么意思,我该如何解决这个问题?

以下是我的其余代码供参考:

Class gerbillab

package gerbillab;

import java.util.Scanner;

import gerbillab.Gerbil;

public class gerbillab{
public static int population;
public static int[] maxfood;
public static int[] foodeats;
public static int types;
public static String[] idnumber;
public static String g;
public static String gerbilId;
public static Gerbil[] gerbil;
public static String amountoffoodeaten;
public static String gerbilsearch;
public static String thisgerbil;
public static void main(String args[]){
        Scanner keyboard = new Scanner(System.in);

    System.out.println("How many types of food do the gerbils eat?");       
    String f = keyboard.nextLine();
    int totalF = Integer.parseInt(f);

    String[] food = new String[totalF];
    maxfood = new int[totalF];
    for
        (int a = 0; a<food.length; a++){
        System.out.println("Name of food number " + (a+1));
        String foodname = keyboard.nextLine();
        food[a] = foodname;

        System.out.println("Max amount of food " + (a+1));
        String m = keyboard.nextLine();
        int maximum = Integer.parseInt(m);
        maxfood[a] = maximum;
        }

    System.out.println("How many gerbils are in the lab?");
    String numberofGerbils = keyboard.nextLine();
    population = Integer.parseInt(numberofGerbils); 

    idnumber = new String[population];
    String[] nickname = new String[population];
    boolean[] bite = new boolean[population];
    boolean[] escape = new boolean[population];

    gerbil = new Gerbil[population];
        for 
            (int b = 0; b<idnumber.length; b++){
            System.out.println("What is the id number of gerbil " + (b+1));
            String idnumberx = keyboard.nextLine();
            idnumber[b] = idnumberx;

            System.out.println("What is the name for gerbil " + (b+1));
            String nicknamex = keyboard.nextLine();
            nickname[b] = nicknamex;

            int[] foodeats = new int[totalF];
                for
                    (int c = 0; c<foodeats.length; c++){
                     System.out.println("how much " + food[c] + " did this gerbil eat");
                        String amountoffoodeaten = keyboard.nextLine();
                        foodeats[c] = Integer.parseInt(amountoffoodeaten);
                        }


            System.out.println("Does this Gerbil bite? Please enter True or False");
            String doesitbite = keyboard.nextLine();
            if (doesitbite.equalsIgnoreCase("true"))
                bite[b] = true;
            else{
                bite[b] = false;
            }


            System.out.println("Does this Gerbil escape? Enter True or False");
            String doesitescape = keyboard.nextLine();
            if (doesitescape.equalsIgnoreCase("true"))
                escape[b] = true;
            else{
                escape[b] = false;

            }

            gerbil[b] = new Gerbil(idnumberx, nicknamex, foodeats, escape[b], bite[b], maxfood);
        }

        while (true){
            System.out.println("What would you like to know?");
            String question = keyboard.nextLine();
            String search = "search";
            String average = "average";
            String end = "end";
            String restart = "restart";


            if (question.equalsIgnoreCase(search)){
                new gerbillab().searchForGerbil();
                }

            else 
                if (question.equalsIgnoreCase(average)){
                    for(int i = 0; i < idnumber.length; i++){ 
                        System.out.println(idnumber[i]); 
                        }
                    for(int i = 0; i < nickname.length; i++){ 
                        System.out.println(nickname[i]); 
                        }
                    for(int i = 0; i < bite.length; i++){ 
                        System.out.println(bite[i]); 
                        }
                    for(int i = 0; i < escape.length; i++){ 
                        System.out.println(escape[i]); 
                        }
                }
            else
                if (question.equalsIgnoreCase(end)){
                    System.exit(0);
                }
            else
                if (question.equalsIgnoreCase(restart)){
                    new gerbillab().main(args);
                    }
            else
                System.out.println("Try again");
                }
}

public static void searchForGerbil()
{
    System.out.println("Please type in a gerbil lab ID");
    Scanner keyboard = new Scanner(System.in);
    String searchgerbil = keyboard.next();
    for (int i = 0; i <gerbil.length; i++){
            if ( searchgerbil.equals(gerbil[i].getId())){
                System.out.println(gerbil);
        }
        else{
            System.out.println("Gerbil " + searchgerbil + " doesnt exist");
        }
}

}



}

类沙鼠

package gerbillab;
public class Gerbil {

private String idnumber;
private String nickname;
private int[] totalfood;
private String[] foodname;
private boolean escape;
private boolean bite;
private int[] foodeats;
public String gerbilsearch;

public Gerbil(String idnumberx, String gerbilName, int[] gerbilfoodeats, boolean gerbilEscape, boolean gerbilBite, int[] maxfood) {
idnumber = idnumberx; 
nickname = gerbilName; 
foodeats = gerbilfoodeats; 
escape = gerbilEscape; 
bite = gerbilBite; 
totalfood = maxfood;
}

public Gerbil(String[] typefood) {
foodname = typefood;
}
public int[] getfoodeaten() {
return foodeats;
}
public Gerbil(int[] numOfFood) {
totalfood = numOfFood;
}
public int[] getAmountFood() {
return totalfood;
}
public boolean getBite() {
return bite;
}
public boolean getEscape() {
return escape;
}
public String getId() {
    return idnumber;
}
public String getName() {
    return nickname;
}
public void setId(String newId) {
idnumber = newId;
}
public void setName(String newName) {
nickname = newName;
}
public String[] gettypesofFood() {
return foodname;
}
}

原因是因为你将totalfood定义为一个int数组。 你需要它:

private Gerbil[] totalfood;

这对于你错误的行是有意义的,因为getAmountFoodGerbil一个方法,所以totalfood[c]应该返回一个Gerbil

从上下文来看,我猜你打算输入:

y = gerbil[c].getAmountFood() + y;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM