[英]how can i take multiple input in single line using coma separation in java
最低報價 Maya 從一家商店購買了“N”件產品。 商店對每件商品提供不同比例的折扣。 她想知道提供最低折扣的商品,這樣她就可以避免購買並省錢。
[輸入格式:第一個輸入為條目數; 第二個輸入是項目名稱、價格和折扣百分比,以逗號 (,) 分隔]
假設最低折扣報價的形式為 Integer。
注意:最低折扣可以有不止一種產品。
示例輸入 1:
4
mobile,10000,20
shoe,5000,10
watch,6000,15
laptop,35000,5
樣本 Output 1:
shoe
說明:手機優惠2000,鞋子優惠500,手表優惠900,筆記本優惠1750,所以鞋子優惠最低。
示例輸入 2:
4
Mobile,5000,10
shoe,5000,10
WATCH,5000,10
Laptop,5000,10
樣本 Output 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 循環中,然后循環獲取所有字符串。 然后,您可以使用這些字符串並使用 split 將它們轉換為值。
for(int i=0; i< numberOfInputs; i++){
String inputLine = scanner.next();
String[] inputArray = inputLine.split(",");
}
我也根據問題提供了答案。 但是,而不是使用 map 或數組來處理數據的存儲。 我繼續創建了一個產品 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.