【问题标题】:How to print the array column value using the if statement in Java?如何使用 Java 中的 if 语句打印数组列值?
【发布时间】:2019-02-06 10:03:34
【问题描述】:

我的 Java 静态销售点 (POS) 系统出现问题。

我在 for 循环和 if 语句的帮助下使用 2D 数组,但在收到我的 POS 时显示数组列时遇到问题。

POS Output1

public static void productdata(){

    for(int row = 0; row < Product.length; row++) {
    for (int col = 0; col < 4 ; col++){
        Product[row][col] = "";

        }
    }
    for(int row = 0; row < POS.length; row++){
    for (int col = 0; col < 3 ; col++){
    if(col == 0){
        POS[row][col] = "0";
    } else{
        POS[row][col] = "";
            }
        }
    }
    // Product [x] [y] x = row; y = col //
    Product [0] [0] = "PP1"; // PRODUCT CODE //
    Product [0] [1] = "\t9350"; // PRODUCT PRICE // 
    Product [0] [2] = "FVP PowerPack Guyabano\t\t\t\t"; // PRODUCT NAME //
    Product [0] [3] = "12"; // PRODUCT QUANTITY //

在所附图片中,我展示了用于我的产品数据的数组。问题是,购买产品的收据显示的是产品代码 (PP1) [--见红色小框--] 而不是产品名称 (FVP PowerPack Guyabano) [--见紫色框--],我不知道如何更改它或显示产品名称...

POS Output2

System.out.print("\n============================================================================");
    System.out.print("\n POS SYSTEM");
    System.out.print("\n FIRST VITA PLUS");
    System.out.print("\n 2/F Suntree Tower,");
    System.out.print("\n No. 13 Meralco Avenue,");
    System.out.print("\n Ortigas Center, Pasig City");
    System.out.print("\n 1605 Philippines\n");

    if (TryAgain == 'n' || TryAgain == 'N'){
        System.out.print("\n \t\t\t\t****PRODUCT PURCHASED****\n\n");
        stop4:
    for(int row = 0; row < POS.length; row++){
    for(int col = 0; col < 3; col++){

            if((POS[row][col].equals("0")) || (POS[row][col].equals(""))){
                break stop4;
            } else{
        System.out.print(POS[row][col] + "\t");
            }
        }
            System.out.println();
            Double totalprice = Double.parseDouble(POS[row][2]);
            TOTAL = totalprice + TOTAL;
    }
    System.out.print("\n============================================================================");

...在这一部分,我展示了输出和创建客户收据的方法(带有产品代码(应该是产品名称)、产品价格和购买数量)。

【问题讨论】:

  • 不太清楚你在问什么。请创建一个Minimal, Complete, and Verifiable example
  • 你能解释一下你的问题到底是什么吗?
  • 先生 @MeetTitan,是的先生,我是来自菲律宾的学生,这是我们的项目,从我们选择的产品公司、餐厅、咖啡店等创建一个简单/静态的销售点。所以我'很抱歉,如果这段代码不够好。不用担心我们不会部署这个 PoS 系统。
  • Sir @Milos : 在所附照片“POS Output1”中,正如您在一般输出中看到的那样,收到购买的产品,我想更改“PP1”(产品代码)带有产品名称(FVP PowerPack Guyabano),但我不知道如何更改它。
  • 很抱歉@TA先生,这是我第一次在这里提问。

标签: java multidimensional-array pos


【解决方案1】:

您似乎正在尝试打印二维数组数据。

你可以试试这个:

for (int row = 0; row < rowLength; row++) 
 {
   for (int col = 0; col < colLength; col++)
    {
       System.out.print(arrayName[row][col]+" ");
     }
    System.out.print("\n");
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-05
    • 1970-01-01
    • 2021-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多