如何允许用户对数组进行编辑

时间:2018-04-17 14:35:45

标签: java arrays input edit

我必须创建3个数组,一个用于容纳5个产品ID,一个用于容纳5个产品价格,一个用于容纳5个产品库存。我需要一种方法来打印所有产品ID,打印所有产品价格,并打印所有库存。我需要一种方法来允许用户对任何产品ID,产品价格或库存进行编辑(这是我正在努力的方法)。每次编辑完成后,我必须重新打印所有数据。我还需要一种方法,在完成所有编辑后打印所有正确的数据,以及额外的列,每种产品的总价格和总价格。在此先感谢您的帮助!

import java.util.Scanner;
public class Inventory {
d   public static void main(String[] args) {

        String[] productIDArray = new String[5];
        double[] priceArray = new double[5];
        int[] inventoryArray = new int[5];


        input(productIDArray, priceArray, inventoryArray);
        print(productIDArray, priceArray, inventoryArray);
        edit(productIDArray, priceArray, inventoryArray);

    }

    public static void input (String[] productIDArray, double[] priceArray, int[] inventoryArray) {
        Scanner input = new Scanner(System.in);

        for (int i = 0; i < 5; i++) {
            System.out.print("Enter the product ID, the price and inventory: ");
            productIDArray[i] = input.next();
            priceArray[i] = input.nextDouble();
            inventoryArray[i] = input.nextInt();
        }       
    }


    public static void print (String[] productIDArray, double[] priceArray, 
    int[] inventoryArray) {

        for (int i = 0; i < 5; i++) {
            System.out.println(productIDArray[i]);
            System.out.println(priceArray[i]);
            System.out.println(inventoryArray[i]);
        }


    }


     public static void edit (String[] productIDArray, double[] priceArray, int[] inventoryArray) {
         Scanner input = new Scanner(System.in);
         int whatToEdit = 0;
         String oldProductID = " ";
         String newProductID = " ";
         double oldPrice = 0;
         double newPrice = 0;
         int oldInventory = 0;
         int newInventory = 0;
         String yesNo = " ";

         while (true) {
             System.out.print("Do you want to make an edit? (Y/N)");
             if (yesNo = y.toUppercase) {
                 System.out.print("Enter what you want to edit: ");
                 System.out.print("Do you want to edit a product ID (1), price (2), or  
                 inventory (3)? ");
                 whatToEdit = input.nextInt();
                 if (whatToEdit == 1) {
                     System.out.print("Enter the product ID you want to edit and the edit: ");
                     productID = input.next();  
                     newProductID = input.next();
                     productIDArray[product] = newProductID;     
                 } else if (whatToEdit == 2) {
                     System.out.print("Enter the price you want to edit and the edit: ");
                     oldPrice = input.nextDouble();
                     newPrice = input.nextDouble();
                     priceArray[oldPrice] = newPrice;
                 } else if (whatToEdit == 3) {
                     System.out.print("Enter the inventory you want to edit and the edit: ");
                     oldInventory = input.nextInt();
                     newPrice = input.nextInt();
                     inventoryArray[oldInventory] = newInventory;
                 }
             }  
        } else if (yesNo == n.toUppercase) {
            break;
        }
        print(productIDArray, priceArray, inventoryArray);
    }

    public static void totalPrice (String[] productIDArray, double[] priceArray, int[] inventoryArray) {

    } 
}

1 个答案:

答案 0 :(得分:0)

首先检查他们想要编辑的数组。然后检查他们想要更改的阵列的哪个部分。然后,您只需获取数组中哪一部分的用户输入以及该部分的新值。

让我们说他们选择了库存阵列。获取数组的哪个部分的输入,然后输入要替换它的数字。

def write_png(A, filename):
    m, n = A.shape

    w = png.Writer(n, m, greyscale=True, bitdepth=1)

    class RowIterator:
        def __init__(self, A):
            self.A = A.tocsr()
            self.current = 0
            return

        def __iter__(self):
            return self

        def __next__(self):
            if self.current+1 > A.shape[0]:
                raise StopIteration
            out = numpy.ones(A.shape[1], dtype=bool)
            out[self.A[self.current].indices] = False
            self.current += 1
            return out

    with open(filename, 'wb') as f:
        w.write(f, RowIterator(A))

    return

编辑: 要检查他们想要编辑的数组,您可以执行以下操作。

 inventoryArray[usersinput] = //Users next inputted number they want to replace it with 

然后再次读取用户输入并使用if或switch语句来决定如何处理他们选择的数组。

编辑2: 比较和toUppercase的问题。我找不到你使用toUpperCase()定义变量'y'的位置。所以你还需要在某处定义。

    System.out.print("Enter 1 for product array, 2 for price array, and 3 for inventory: ");