【发布时间】:2022-01-07 06:46:45
【问题描述】:
我正在尝试制作一个购物车程序,只使用数组。 (不是数组列表) 但是,我的“addItem”方法有错误,它说
-
方法 addItem(int[], int) 未为类型数组定义
-
方法 addItem(String[], String) 未定义数组类型
-
方法 addItem(double[], double) 对于 Scanner* 类型未定义
而且我不知道如何调试这个..有人可以帮我吗? (我还没有完成所有的菜单 - 开关/机箱。只想先完成“添加项目”)导入 java.util.Arrays; 导入 java.util.Scanner;
公共类 A_retail {
public static void main(String[] args) { Scanner myinput = new Scanner(System.in); int total; int quantity; String name = null; double price = 0; String option; System.out.println("How many kinds of items do you have in total?"); total = myinput.nextInt(); int[] Quantity = new int[total]; String[] Name = new String[total]; double[] Price = new double[total]; for (int i = 0; i<total; i++) { System.out.println("Please type the quantity of your item(s) (ex : If you have 2 shirts, please type 2)); Quantity[i] = myinput.nextInt(); System.out.println("Please type the name of your item(s)"); Name[i] = myinput.next(); System.out.println("Please type the price of your item(s)"); Price[i] = myinput.nextDouble(); } System.out.println("Please choose one option below \n1) Add item(s) \n2) Remove item(s) \n3) View Cart \n4) Checkout \n5) Exit"); option = myinput.next(); switch(option) { case "1" , "Add item(s)": System.out.println("How many items do you want to add?"); int add = myinput.nextInt(); for(int i=0; i<add; i++) { System.out.print("Please type the quantity of your item(s)"); quantity = myinput.nextInt(); Quantity = Arrays.addItem(Quantity, quantity); //error in addItem System.out.print("Please type the name of your item(s)"); name = myinput.next(); Name = Arrays.addItem(Name, name); //error in addItem System.out.print("Please type the price of your item(s)"); price = myinput.nextDouble(); Price = myinput.addItem(Price, price); //error in addItem } break; } }//end item public static int[] addItem(int[] array, int item) { int [] newArray = new int [array.length + 1]; for (int i = 0; i<array.length; i++) { newArray[i] = array[i]; } newArray[newArray.length-1] = item; return newArray; }//end addItem public static String[] addItem(String[] array, String item) { String [] newArray = new String [array.length + 1]; for (int i = 0; i<array.length; i++) { newArray[i] = array[i]; } newArray[newArray.length-1] = item; return newArray; }//end addItem public static double[] addItem(double[] array, double item) { double [] newArray = new double [array.length + 1]; for (int i = 0; i<array.length; i++) { newArray[i] = array[i]; } newArray[newArray.length-1] = item; return newArray; }//end addItem}//结束类
【问题讨论】:
-
它的字面意思就是:数组中不存在具有该签名的方法
-
通过 addItem(..., ...) 调用它们,而不使用Something。在前面。