Java错误消息

时间:2015-06-26 21:03:35

标签: java

我对在处理class-

的Java程序时遇到的错误消息感到困惑
public

参考下面的错误消息:

 ----jGRASP exec: javac -g Program3.java

Program3.java:26: error: incompatible types
    productNum = input.nextInt();
                              ^
  required: int[]
  found:    int
1 error

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.

2 个答案:

答案 0 :(得分:0)

您的代码中存在许多错误。第一个是你不能将数组的值设置为整数,你需要指定一个索引,如:productNum[0] = input.nextInt();

尝试使用以下代码替换现有的main函数:

public static void main(String[] args)
{
    int product;
    System.out.print("Enter Product Number (1-5) or -1 to Quit:");
    Scanner input = new Scanner(System.in);
    product = input.nextInt();
    while(product != -1)
    {
        final int NUMBER_OF_ITEMS = 5; //This isn't being used for anything and could be removed
        int[] productNum = {1,2,3,4,5}; //This also isn't really being used for anything and could be removed
        double[] price = {2.98, 4.50, 9.98, 4.49, 6.87};
        double quantity, total;
        System.out.print("Enter Quantity:");
        quantity = input.nextDouble();
        total = price[product - 1] * quantity;
        System.out.println(total + "\n");
        System.out.print("Enter Product Number (1-5) or -1 to Quit:");
        product = input.nextInt();
    }
}

答案 1 :(得分:0)

您正在为整数数组指定一个整数。这就像做

String lol = "random text";
int random = lol;

这会引发错误,因为整数不能保存字符串的值。你的情况也会发生同样的事情。您正尝试将整个数组的值设置为单个int的值。也许做productNum[0] = input.nextInt();。这样,您将数组内的int的值设置为int。你必须编写一个for循环代码,这样你就可以填充数组的更多元素而不是数字0,但在我看来,这并不难做到。