英文:
Not able to connect two user inputs
问题
作为编程初学者,我尝试编写一个简单的Java程序,用于比较用户输入的一些物品的价格(最低价和最高价)。
到目前为止,我已经成功编写了代码,告诉用户哪个价格最低和最高,但我无法告诉用户价格所对应的物品。有什么想法吗?
System.out.println("请输入物品1的名称:");
String itemOne = scanner.next();
System.out.println("请输入" + itemOne + "的价格:");
double priceItemOne = scanner.nextDouble();
System.out.println("请输入物品2的名称:");
String itemTwo = scanner.next();
System.out.println("请输入" + itemTwo + "的价格:");
double priceItemTwo = scanner.nextDouble();
double highest = Math.max(priceItemOne, priceItemTwo);
System.out.println("最贵的物品是:" + highest);
double lowest = Math.min(priceItemOne, priceItemTwo);
System.out.println("最便宜的物品是:" + lowest);
英文:
As a beginner of programming, I'm trying to write a simple Java program for comparing the prices (lowest and highest price) of some items entered by the user.
So far, I've managed to write the code telling the user which price is the lowest and highest, but I'm not able to inform the user which item the price refers to. Any ideas?
System.out.println("\nEnter the name of item 1 ");
String itemOne = scanner.next();
System.out.println("\nEnter the price of " + itemOne + ": ");
double priceItemOne = scanner.nextDouble();
System.out.println("\nEnter the name of item 2 ");
String itemTwo = scanner.next();
System.out.println("\nEnter the price of " + itemTwo + ": ");
double priceItemTwo = scanner.nextDouble();
double highest = Math.max(priceItemOne, priceItemTwo);
System.out.println("\nThe most expensive item is " + highest);
double lowest = Math.min(priceItemOne, priceItemTwo);
System.out.println("\nThe cheapest item is " + lowest);
答案1
得分: 0
以下是代码的翻译部分:
唯一的想法是更多地了解Java(和面向对象编程),这个解决方案可能是一个快速入门的选择,看一看。
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Question63869241 {
static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
// pass 1, 10 for taking 10 item inputs from user
List<Item> items = IntStream.rangeClosed(1, 2)
.mapToObj(Question63869241::getItemInputFromUser)
.collect(Collectors.toList());
Item expensiveItem = getMostExpensiveItem(items);
Item cheapestItem = getCheapestItem(items);
System.out.println("\n最贵的物品 '" + expensiveItem.getItemName()
+ "' 的价格是: " + expensiveItem.getPrice());
System.out.println("\n最便宜的物品 '" + cheapestItem.getItemName()
+ "' 的价格是: " + cheapestItem.getPrice());
}
private static Item getItemInputFromUser(int sequence) {
System.out.println("\n输入物品名称 " + sequence);
String name = scanner.next();
System.out.println("\n输入 '" + name + "' 的价格: ");
double price = scanner.nextDouble();
return new Item(name, price);
}
private static Item getMostExpensiveItem(List<Item> items) {
return items.stream()
.sorted(Comparator.comparing(Item::getPrice).reversed())
.findFirst().get();
}
private static Item getCheapestItem(List<Item> items) {
return items.stream()
.sorted(Comparator.comparing(Item::getPrice))
.findFirst().get();
}
}
class Item {
private String itemName;
private Double price;
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Item(String itemName, Double price) {
this.itemName = itemName;
this.price = price;
}
}
英文:
The only idea is to learn more about Java (and OOP), this solution may be a quick starter for you, have a look.
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Question63869241 {
static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
// pass 1, 10 for taking 10 item inputs from user
List<Item> items = IntStream.rangeClosed(1,2)
.mapToObj(Question63869241::getItemInputFromUser)
.collect(Collectors.toList());
Item expensiveItem = getMostExpensiveItem(items);
Item cheapestItem = getCheapestItem(items);
System.out.println("\nThe most expensive item '" + expensiveItem.getItemName()
+ "' is priced at: " + expensiveItem.getPrice() );
System.out.println("\nThe cheapest item '" + cheapestItem.getItemName()
+ "' is priced at: " + cheapestItem.getPrice() );
}
private static Item getItemInputFromUser(int sequence){
System.out.println("\nEnter the name of item " + sequence);
String name = scanner.next();
System.out.println("\nEnter the price of '" + name + "': ");
double price = scanner.nextDouble();
return new Item(name, price);
}
private static Item getMostExpensiveItem(List<Item> items){
return items.stream()
.sorted(Comparator.comparing(Item::getPrice).reversed())
.findFirst().get();
}
private static Item getCheapestItem(List<Item> items){
return items.stream()
.sorted(Comparator.comparing(Item::getPrice))
.findFirst().get();
}
}
class Item {
private String itemName;
private Double price;
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Item(String itemName, Double price) {
this.itemName = itemName;
this.price = price;
}
}
答案2
得分: 0
Scanner scan = new Scanner(System.in);
scan.useLocale(Locale.ENGLISH);
System.out.print("Enter the name of item 1: ");
String oneName = scan.nextLine();
System.out.print("Enter the price of item 1: ");
double onePrice = scan.nextDouble();
scan.nextLine();
System.out.print("Enter the name of item 2: ");
String twoName = scan.nextLine();
System.out.print("Enter the price of item 2: ");
double twoPrice = scan.nextDouble();
int compare = Double.compare(onePrice, twoPrice);
if (compare == 0)
System.out.format(Locale.ENGLISH, "Both prices for item 1 (%s) and item 2 (%s) are equal: %.2f\n", oneName, twoName, onePrice);
else if (compare > 0) {
System.out.format(Locale.ENGLISH, "The most expensive item is 1 (%s): %.2f\n", oneName, onePrice);
System.out.format(Locale.ENGLISH, "The cheapest item is 2 (%s): %.2f\n", twoName, twoPrice);
} else {
System.out.format(Locale.ENGLISH, "The most expensive item is 2 (%s): %.2f\n", twoName, twoPrice);
System.out.format(Locale.ENGLISH, "The cheapest item is 1 (%s): %.2f\n", oneName, onePrice);
}
英文:
Scanner scan = new Scanner(System.in);
scan.useLocale(Locale.ENGLISH);
System.out.print("Enter the name of item 1: ");
String oneName = scan.nextLine();
System.out.print("Enter the price of item 1: ");
double onePrice = scan.nextDouble();
scan.nextLine();
System.out.print("Enter the name of item 2: ");
String twoName = scan.nextLine();
System.out.print("Enter the price of item 2: ");
double twoPrice = scan.nextDouble();
int compare = Double.compare(onePrice, twoPrice);
if (compare == 0)
System.out.format(Locale.ENGLISH, "Both prices for item 1 (%s) and item 2 (%s) are equal: %.2f\n", oneName, twoName, onePrice);
else if (compare > 0) {
System.out.format(Locale.ENGLISH, "The most expensive item is 1 (%s): %.2f\n", oneName, onePrice);
System.out.format(Locale.ENGLISH, "The cheapest item is 2 (%s): %.2f\n", twoName, twoPrice);
} else {
System.out.format(Locale.ENGLISH, "The most expensive item is 2 (%s): %.2f\n", twoName, twoPrice);
System.out.format(Locale.ENGLISH, "The cheapest item is 1 (%s): %.2f\n", oneName, onePrice);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论