简体   繁体   English

Java错误消息

[英]Java error message

I am confused on the error message that I am getting while working on a Java program for class- 我对使用Java程序进行类学习时遇到的错误消息感到困惑,

 ----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.

Error message in reference to below: 错误消息参考如下:

import javax.swing.*;
import java.util.Scanner;

public class Program3
{
public static void main(String[] args)
{
    final int NUMBER_OF_ITEMS = 5;
    int[]productNum = {1,2,3,4,5};   //array of product numbers
    double[]price = {2.98, 4.50, 9.98, 4.49, 6.87};   //array of
            price
    double quantity, product, total;
    Scanner input = new Scanner(System.in);
    System.out.print("Enter Product Number (1-5) or -1 to Quit:");
    productNum = input.nextInt();
            System.out.print("Enter Quantity:");
    quantity = input.nextDouble();
    total = product*quantity;
    System.out.print(total);
}
}

There are a number of errors in your code. 您的代码中存在许多错误。 The first being that you can't set the value of an array to an integer, you need to specify an index like: productNum[0] = input.nextInt(); 第一个是不能将数组的值设置为整数,需要指定一个索引,例如: productNum[0] = input.nextInt();

Try the following code to replace your existing main function: 尝试使用以下代码替换现有的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();
    }
}

You are assigning an integer to an array of integers. 您正在将整数分配给整数数组。 It's like doing 就像在做

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

That would raise an error because an integer can't hold the value of a string. 这将引发错误,因为整数不能容纳字符串的值。 The same thing happens in your case. 您的情况也一样。 You are trying to set the value of the whole array to the value of a single int . 您试图将整个数组的值设置为单个int的值。 Maybe do productNum[0] = input.nextInt(); 也许productNum[0] = input.nextInt(); . That way, you will set the value of an int inside the array to an int. 这样,您可以将数组内部的int值设置为int。 You would have to code a for loop so that you can fill in more elements of the arrays than number 0, but that wouldn't be that hard to do in my opinion. 您将必须编写一个for循环,以便您可以填充数组中比数字0更多的元素,但是在我看来,这样做并不难。

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

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