首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用java中的昏迷分离在单行中获取多个输入?

如何使用java中的昏迷分离在单行中获取多个输入?
EN

Stack Overflow用户
提问于 2020-09-22 06:38:11
回答 3查看 4.5K关注 0票数 0

最不提供玛雅购买“N”的产品从商店。这家商店对每件商品提供不同的折扣百分比。她想知道有最低折扣优惠的商品,这样她就可以避免购买和省钱。

输入格式:第一个输入指项目的编号;第二个输入是以逗号(,)分隔的项目名称、价格和折扣百分比。

假设最低折扣优惠是以整数形式提供的。

注:可以有一个以上的产品和最低折扣。

样本输入1:

代码语言:javascript
复制
4

mobile,10000,20

shoe,5000,10

watch,6000,15

laptop,35000,5

样本输出1:

代码语言:javascript
复制
shoe

说明:手机的折扣是2000,鞋子的折扣是500,手表的折扣是900,笔记本电脑的折扣是1750。所以这只鞋的折扣是最低的。

样本输入2:

代码语言:javascript
复制
4

Mobile,5000,10

shoe,5000,10

WATCH,5000,10

Laptop,5000,10

样本输出2:

代码语言:javascript
复制
Mobile 

shoe 

WATCH 

Laptop
EN

回答 3

Stack Overflow用户

发布于 2020-09-22 06:55:05

使用扫描器并使用循环将输入作为字符串(因为您希望有几行由字符串和整数组成),在循环中进一步将字符串拆分为数组,并将包含数字的字符串值解析为整数。你可以这样做:

代码语言:javascript
复制
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]);
 }
}
票数 0
EN

Stack Overflow用户

发布于 2020-09-22 07:10:25

假设每个输入包含一个字符串数组,如下所示:

代码语言:javascript
复制
String[] input = {"mobile,10000,20", "shoe,5000,10", "watch,6000,15", "laptop,35000,5"};

您可以做的是循环遍历所有字符串,并决定每个项目的折扣是否比上一次最糟糕的折扣更糟糕。

代码语言:javascript
复制
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");
票数 0
EN

Stack Overflow用户

发布于 2021-01-11 13:12:02

根据需要,在一条单行中获取多个输入。你可以用这种方式得到它们。首先,您需要获得输入的数量。

代码语言:javascript
复制
numberOfInputs = scanner.nextInt()

一旦得到输入的数量,就可以使用numberOfInputs并将它们放入一个for循环中,然后循环通过以获得所有的String。然后,您可以使用字符串并使用拆分将它们转换为值。

代码语言:javascript
复制
for(int i=0; i< numberOfInputs; i++){
   String inputLine = scanner.next();
   String[] inputArray = inputLine.split(",");
}

我也根据这个问题提供了答案。但是,没有使用映射或数组来处理数据的存储。我继续创建了一个产品类来处理存储问题。

代码语言:javascript
复制
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());
            }
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64004301

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档