简体   繁体   English

我如何在 java 中使用逗号分隔在单行中获取多个输入

[英]how can i take multiple input in single line using coma separation in java

Least offer Maya buys “N” no of products from a shop.最低报价 Maya 从一家商店购买了“N”件产品。 The shop offers a different percentage of discount on each item.商店对每件商品提供不同比例的折扣。 She wants to know the item that has the minimum discount offer, so that she can avoid buying that and save money.她想知道提供最低折扣的商品,这样她就可以避免购买并省钱。

[Input Format: The first input refers to the no of items; [输入格式:第一个输入为条目数; the second input is the item name, price and discount percentage separated by comma(,)]第二个输入是项目名称、价格和折扣百分比,以逗号 (,) 分隔]

Assume the minimum discount offer is in the form of Integer.假设最低折扣报价的形式为 Integer。

Note: There can be more than one product with a minimum discount.注意:最低折扣可以有不止一种产品。

Sample Input 1:示例输入 1:

4

mobile,10000,20

shoe,5000,10

watch,6000,15

laptop,35000,5

Sample Output 1:样本 Output 1:

shoe

Explanation: the discount on the mobile is 2000, the discount on the shoe is 500, the discount on the watch is 900 and the discount on the laptop is 1750. So the discount on the shoe is the minimum.说明:手机优惠2000,鞋子优惠500,手表优惠900,笔记本优惠1750,所以鞋子优惠最低。

Sample Input 2:示例输入 2:

4

Mobile,5000,10

shoe,5000,10

WATCH,5000,10

Laptop,5000,10

Sample Output 2:样本 Output 2:

Mobile 

shoe 

WATCH 

Laptop

Use a scanner and take the input as a string using a loop(as you want several lines consisting of strings and integers), further within the loop split the string to array and parse the string value containing numbers to integers.使用扫描仪并使用循环将输入作为字符串(因为您需要几行由字符串和整数组成的行),进一步在循环中将字符串拆分为数组并将包含数字的字符串值解析为整数。 You could do something like this:你可以这样做:

public static void main(String[] args){
 Scanner sc = new Scanner(System.in);
 int noOfItems = sc.nextInt();
 String input="";
 int price[] = new int[noOfItems];
 int percentage[] = new int[noOfItems];
 String itemName[] = new String[noOfItems];
 String[] values;
 for(int i=0;i<noOfItems;++i){
  input = sc.nextLine();
  values = input.split(",");
  itemName[i] = values[0];
  price[i] = Integer.parseInt(values[1]);
  percentage[i] = Integer.parseInt(values[2]);
 }
}

Assuming each input consists of an array of strings like the following:假设每个输入都包含一个字符串数组,如下所示:

String[] input = {"mobile,10000,20", "shoe,5000,10", "watch,6000,15", "laptop,35000,5"};

What you could do is loop through all strings and decide for each item if the discount is worse than the previous worst discount.您可以做的是循环遍历所有字符串,并确定每个项目的折扣是否低于之前的最差折扣。

String worstItem = null;
double worstDiscount = 0;

for(String item : input) {
    // Split the string in multiple strings
    String[] splittedString = item.split(",");
    String itemName = splittedString[0]; // First item is the items name
    int price = Integer.parseInt(splittedString[1]); // The items price, parsed as an integer
    int discount = Integer.parseInt(splittedString[2]);

    // Calculate discount price
    double discountPrice = price * (discount / 100);

    // Check if the discount price is worse than the discount of a previous item
    if(discountPrice < worstDiscount) {
        worstDiscount = discountPrice;
        worstItem = itemName;
    }
}

System.out.println(worstItem + " has the worst discount");

Based on the needs, to get the multiple inputs in a single line.根据需要,在一行中获取多个输入。 You can get them in this way.你可以通过这种方式得到它们。 First, you need to get the number of inputs.首先,您需要获取输入的数量。

numberOfInputs = scanner.nextInt()

Once you get the number of inputs, you can then use the numberOfInputs and put them in a for loop and then loop through to get all the Strings.一旦获得输入的数量,就可以使用 numberOfInputs 并将它们放入 for 循环中,然后循环获取所有字符串。 You can then utilize the strings and convert them into values using split.然后,您可以使用这些字符串并使用 split 将它们转换为值。

for(int i=0; i< numberOfInputs; i++){
   String inputLine = scanner.next();
   String[] inputArray = inputLine.split(",");
}

I have also provided the answer based on the question.我也根据问题提供了答案。 However, rather than using a map or array to deal with the storage of the data.但是,而不是使用 map 或数组来处理数据的存储。 I went ahead and created a product class to deal with the storage.我继续创建了一个产品 class 来处理存储。

import java.util.Scanner;

class Product {
    String name;
    int price, discountPercentage, discountPrice;

    public Product(String name, int price, int discountPercentage, int discountPrice) {
        this.name = name;
        this.price = price;
        this.discountPercentage = discountPercentage;
        this.discountPrice = discountPrice;
    }

    public String getName() {
        return name;
    }

    public int getDiscountPrice() {
        return discountPrice;
    }
}

public class Main{

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = 0, worstPrice = 0;

        n = sc.nextInt();
        Product[] productItems = new Product[n]; //Product Array with the length of the input.

        for (int i = 0; i < n; i++) {
            String input = sc.next(); //Take the input from the user.
            String[] inputArray = input.split(","); //Split the input based on comma.
            String name = inputArray[0]; //The array item contains the name.
            int price = Integer.parseInt(inputArray[1]); //The Second array item contains the price.
            int discountPercent = Integer.parseInt(inputArray[2]); //The Third array contains the discount percentage.
            int discountPrice = (price * discountPercent) / 100; //Based on the price & discount percentage, get the discount price.

            // Create a product object using the values and assign it to the product items array.
            productItems[i] = new Product(name, price, discountPercent, discountPrice);

            // The below statement, will get us the lowest discount price.
            if (i == 0 || discountPrice < worstPrice) worstPrice = discountPrice;
        }

        // Print all the product items, which have the worst price in the provided input.
        for (Product productItem : productItems) {
            if (productItem.getDiscountPrice() == worstPrice) {
                System.out.println(productItem.getName());
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM