如何在java中从用户处获取多个值输入(使用不同类型并用逗号分隔)

如何在java中从用户处获取多个值输入(使用不同类型并用逗号分隔),java,Java,我想接受来自用户的多个值,如productName、Price、gstAmount,如下所示: sample input:2 //System.out.println("enter no of products"); mobile,2356,15 watch,200,10 在此之后,计算最低价格的项目 sample output:watch 您可以使用BufferedReader或Scanner类从控制台读取。之后,您可以进行排序以获得最低价商品。按照以下步骤操作: 每次询问用户是否希望输入

我想接受来自用户的多个值,如productName、Price、gstAmount,如下所示:

sample input:2 //System.out.println("enter no of products");
mobile,2356,15
watch,200,10
在此之后,计算最低价格的项目

sample output:watch

您可以使用BufferedReader或Scanner类从控制台读取。之后,您可以进行排序以获得最低价商品。

按照以下步骤操作:

  • 每次询问用户是否希望输入其他项目后,使用扫描仪从用户处读取3个值,如果是,则循环else关闭扫描仪

  • 对于对象中的每个条目存储,有3个字段,分别为名称、价格、gst,然后添加到数组中

  • 按价格顺序对数组排序并选择最低值

  • 按如下方式操作:

    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            System.out.print("Enter no of products: ");
            int np = Integer.parseInt(in.nextLine());
            String productName, minPricedProduct = "", input;
            String[] inputArr;
            int price, gst, grossPrice;    
            int min = Integer.MAX_VALUE;
            for (int i = 1; i <= np; i++) {
                System.out.print("Enter Product name, Price, GST amount (like productname,1234,123): ");
                input = in.nextLine();
                inputArr = input.split(",");
                productName = inputArr[0];
                price = Integer.parseInt(inputArr[1]);
                gst = Integer.parseInt(inputArr[2]);
                grossPrice = price + gst;
                if (grossPrice < min) {
                    min = grossPrice;
                    minPricedProduct = productName;
                }
            }
            System.out.println(minPricedProduct);
        }
    }
    

    注意:您应该将此程序作为指南,并尝试改进它,例如,我没有故意进行异常处理,以便您可以自己进行处理并进一步学习。

    好的。那你就这么做吧。如果您遇到任何问题,请告诉我们。您在说什么?一个代码示例将对OP和未来的访问者有所帮助。否则,这篇文章应该是一篇简单的评论
    Enter no of products: 3
    Enter Product name, Price, GST amount (like productname,1234,123): spectacles,4500,125
    Enter Product name, Price, GST amount (like productname,1234,123): watch,200,10
    Enter Product name, Price, GST amount (like productname,1234,123): mobile,2356,15
    watch