如何使用java中的coma分离在单行中获取多个输入



最低报价Maya从商店购买了"N"个产品。这家商店对每种商品都提供不同比例的折扣。她想知道折扣最低的商品,这样她就可以避免购买并省钱。

[输入格式:第一个输入表示项目数;第二个输入是项目名称、价格和折扣百分比,由逗号(,(]

假设最低折扣优惠为整数形式。

注意:可以有多个最低折扣的产品。

样本输入1:

4
mobile,10000,20
shoe,5000,10
watch,6000,15
laptop,35000,5

样本输出1:

shoe

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

样本输入2:

4
Mobile,5000,10
shoe,5000,10
WATCH,5000,10
Laptop,5000,10

样本输出2:

Mobile 
shoe 
WATCH 
Laptop

使用扫描仪,使用循环将输入作为字符串(因为您需要由字符串和整数组成的几行(,然后在循环中将字符串拆分为数组,并将包含数字的字符串值解析为整数。你可以这样做:

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]);
}
}

假设每个输入都由一个字符串数组组成,如下所示:

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

你可以做的是遍历所有字符串,并为每个项目决定折扣是否比之前最差的折扣更差。

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");

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

numberOfInputs = scanner.nextInt()

一旦你得到了输入的数量,你就可以使用numberOfInputs,把它们放在for循环中,然后循环得到所有的String。然后,您可以使用这些字符串,并使用split将它们转换为值。

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

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

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());
}
}
}
}

最新更新