【问题标题】:What does "The method is undefined for the type Arrays" mean?“数组类型的方法未定义”是什么意思?
【发布时间】: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。在前面。

标签: java arrays methods


【解决方案1】:
Quantity = Arrays.addItem(Quantity, quantity);

您正在使用 Arrays 类的 addItem 方法

 Scanner myinput = new Scanner(System.in);

 ...

 Price = myinput.addItem(Price, price); 

您正在使用 Scanner 类的 addItem 方法

您没有使用您在 A_retail 类中声明的 addItem 方法

试试这个

Quantity = addItem(Quantity, quantity);
Name = addItem(Name, name);
Price = addItem(Price, price); 

【讨论】:

    猜你喜欢
    • 2017-03-09
    • 2011-04-19
    • 1970-01-01
    • 1970-01-01
    • 2021-07-26
    • 2016-11-07
    • 2019-11-29
    • 2020-12-04
    • 1970-01-01
    相关资源
    最近更新 更多