0

我正在尝试编写一个程序,到目前为止的其余代码都可以工作,但是我发现了一个不兼容的类型:双重要求:第 38 行的杂货项目。谁能帮我解释为什么我收到这个错误以及如何正确吗?谢谢你。这是我的代码:

 import java.util.Scanner;

public class GroceryList {

  private GroceryItem[]groceryArr; //ARRAY HOLDS GROCERY ITEM OBJECTS
  private int numItems;
  private String date;
  private String storeName;
  public GroceryList(String inputDate, String inputName) {

//FILL IN CODE HERE
// CREATE ARRAY, INITIALIZE FIELDS
    groceryArr = new GroceryItem[10];
    numItems = 0;

  }

  public void load() {

    Scanner keyboard = new Scanner(System.in);
      double sum = 0;
      System.out.println ("Enter the trip date and then hit return:");
    date = keyboard.next();
    keyboard.nextLine();
    System.out.println("Enter the store name and then hit return:");
    storeName = keyboard.next();
    keyboard.nextLine();
    double number = keyboard.nextDouble();

    //NEED TO PROMPT USER FOR, AND READ IN THE DATE AND STORE NAME.


    System.out.println("Enter each item bought and the price (then return).");
    System.out.println("Terminate with an item with a negative price.");
    number = keyboard.nextDouble();

    while (number >= 0 && numItems < groceryArr.length) {
      groceryArr[numItems] = number;
      numItems++;
      sum += number;
      System.out.println("Enter each item bought and the price (then return).");
      System.out.println("Terminate with an item with a negative price.");
      number = keyboard.nextDouble();
    }


     /* 
    //READ IN AND STORE EACH ITEM. STORE NUMBER OF ITEMS
  }

  private GroceryItem computeTotalCost() {
    //add code here
  }

  public void print() {
    \\call computeTOtalCost
  }
  */
}
}
4

1 回答 1

0

“groceryArr[numItems] = 数量;”

groceryArr[numItems] 是 GroceryItem() 的一个实例 - 'number' 是双精度

您需要在 GroceryItem() 对象中使用双变量来存储“数字”值。

于 2013-02-25T23:59:47.663 回答