【问题标题】:Error: The operator += is undefined for the argument type(s) double, int[]错误:运算符 += 未定义参数类型 double、int[]
【发布时间】:2014-06-02 04:39:47
【问题描述】:

我正在尝试将运算符 += 用于我的程序中的一种方法,该方法将沙鼠的食物总量除以沙鼠的数量,有点像平均值。错误如下:

  1. “对于参数类型 double、int[],运算符 += 未定义”,代码如下:
    平均 += g.getAmountFood();

  2. "The operator / is undefined for the argument type(s) Gerbil, int" 在以下代码中: 平均 = 沙鼠[i] / 沙鼠.长度

这是我的第一个主类的代码: 导入 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() {


// girbil[0] .. girbil[n] / n = average!!!
average = gerbil[i] / gerbil.length
double average = 0.0;
for (int i = 0; i <= gerbil.length; i++) {
    Gerbil g = gerbil[i];


    average +=  g.getAmountFood();
}
average /= gerbil.length;

for (int i = 0; i <= gerbil.length; i++) {
    Gerbil g = gerbil[i];
    String gid = g.getId();
    String gname = g.getName();
    String everything = gid + " " + gname + " " + average + "\n";

}



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;

}
}

以下是我的第二个“沙鼠”课

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;


}

}

【问题讨论】:

  • 错误信息非常清楚。你不明白什么?你想用运算符做什么?目的是什么?
  • 我正在尝试返回用户输入的每个沙鼠的沙鼠属性信息(沙鼠id、名称和计算结果)。这是我对平均食物方法的期望输出示例:BB110 (Big Bertha) 50% SA330 (Sally Ann) 67%
  • 阅读消息并尝试真正理解它的含义。 [] 是什么意思?
  • 我建议阅读有关 java 类型和运算符的文档,然后重写整个代码。尤其是主班,因为非常混乱和糟糕的格式。

标签: java methods error-handling


【解决方案1】:

只是大致了解错误的含义:

对于参数类型 double、int[],未定义运算符 +=" 在以下代码中:average += g.getAmountFood();

您的getFoodAmount() 正在返回一个int[],它是一个整数数组。如果要添加值,则需要遍历数组并在数组中的每个数字上使用+=。

你的第二个错误:

操作符 / 未定义参数类型 Gerbil, int" at 以下代码:average = gerbil[i] / gerbil.length

嗯,呃,我找不到线路。请帮帮我,我很乐意帮助你。 ;D

【讨论】:

  • public static String averagefood() { // girbil[0] .. girbil[n] / n = average!!!平均 = gerbil[i] / gerbil.length 双平均 = 0.0; for (int i = 0; i
  • 那是另一行所在的地方
  • 我猜这是你之前的错误的溢出。更正那个,看看是否解决了:)
  • 解决我提到的第一个错误。您需要阅读运算符基础知识。
  • @user3543620 如果回答对你有帮助,请采纳:)
【解决方案2】:

The operator += is undefined for the argument type(s) double, int[]" at the following code: average += g.getAmountFood();

这是因为 getAmountFood() 方法返回一个整数数组。声明一个数组并将 getAmountFood() 方法返回的数组分配给新数组,然后你可以循环访问它来访问各个元素。所以你可以这样做:

 int[] amountFood =  g.getAmountFood(); 

 double average = 0;

 for(int i : amountFood){
     average += i;    
 }
 average /= gerbil.length;

"The operator / is undefined for the argument type(s) Gerbil, int" at the following code: average = gerbil[i] / gerbil.length

这里你使用了i,它在这个范围内没有声明。你在前面的for循环中声明了i,它不能从方法averagefood()访问。

你应该写

   food = g.getTypeFood();

而不是:

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

【讨论】:

  • 我认为你不需要这一行:average = gerbil[i] / gerbil.length
  • 好的,谢谢...现在它说 g.getFood() 无法解析 g?
  • 是的,你写 String[] storeFood = g.getFood();
  • 从你的代码中删除它: for (int i = 0; i
  • 等等,所以把你刚刚输入的代码放到我的代码中还是删除它?如果不删除,那我应该删除什么?
猜你喜欢
  • 2015-12-14
  • 2018-05-25
  • 2016-01-26
  • 2017-06-04
  • 1970-01-01
  • 2021-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多