Java 无法连接两个用户输入
作为编程初学者,我正在尝试编写一个简单的Java程序,用于比较用户输入的某些项目的价格(最低和最高价格) 到目前为止,我已经成功地编写了代码,告诉用户哪个价格最低,哪个价格最高,但我无法通知用户价格指的是哪个项目。有什么想法吗Java 无法连接两个用户输入,java,Java,作为编程初学者,我正在尝试编写一个简单的Java程序,用于比较用户输入的某些项目的价格(最低和最高价格) 到目前为止,我已经成功地编写了代码,告诉用户哪个价格最低,哪个价格最高,但我无法通知用户价格指的是哪个项目。有什么想法吗 System.out.println("\nEnter the name of item 1 "); String itemOne = scanner.next(); System.out.println("\nEnter the price
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);
唯一的想法是学习更多关于Java(和OOP)的知识,这个解决方案可能是您的快速入门,看看吧
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;
}
}
import java.util.Comparator;
导入java.util.List;
导入java.util.Scanner;
导入java.util.stream.collector;
导入java.util.stream.IntStream;
公开课问题63869241{
静态最终扫描仪=新扫描仪(System.in);
公共静态void main(字符串[]args){
//通过1、10从用户获取10项输入
列表项=IntStream.rangeClosed(1,2)
.mapToObj(问题63869241::getItemInputFromUser)
.collect(Collectors.toList());
项目费用项目=getMostExpensiveItem(项目);
Item cheapestItem=getCheapestItem(项目);
System.out.println(“\n最昂贵的项目””+expensiveItem.getItemName()
+'的定价为:“+expensiveItem.getPrice());
System.out.println(“\n最便宜的项目””+cheapestItem.getItemName()
+'的定价为:“+cheapestItem.getPrice());
}
私有静态项getItemInputFromUser(int序列){
System.out.println(“\n输入项目名称”+顺序);
字符串名称=scanner.next();
System.out.println(“\n输入'+name+'':''的价格;
双倍价格=scanner.nextDouble();
返回新项目(名称、价格);
}
私有静态项getMostExpensiveItem(列表项){
returnitems.stream()
.sorted(Comparator.comparing(Item::getPrice).reversed())
.findFirst().get();
}
私有静态项getCheapestItem(列表项){
returnitems.stream()
.排序(比较器.比较(项::getPrice))
.findFirst().get();
}
}
类项目{
私有字符串itemName;
私人双价;
公共字符串getItemName(){
返回itemName;
}
public void setItemName(字符串itemName){
this.itemName=itemName;
}
公开双价{
退货价格;
}
公共定价(双倍价格){
这个价格=价格;
}
公共项目(字符串itemName,双倍价格){
this.itemName=itemName;
这个价格=价格;
}
}
实现这一点的面向对象方法是使用某种Item
类,将name
和price
作为字段,使用它们的getter和setter。否则,您可以使用当前方法,例如,将highest
与priceItemOne
进行比较。如果它们相等,则价格最高的项目是itemOne
。否则,它是itemTwo
。创建三个索引<代码>整数索引=0,最小索引=0,最大索引=0代码>现在,当您找到一个最高的新值时,请将maxIndex设置为当前索引。同样,对于lowest和minIndex,你的示例比我的代码更结构化,你说得对,我需要畅销书才能获得更多的知识。。我一定会的。我有一个关于允许用户输入的项目范围的问题。我目前正在使用“if”和“else”语句询问用户要比较多少项。这是可行的,但假设我想让用户决定要比较多少项,而不必询问有多少项。那可能吗?我找不到任何相关信息。上述答案完全基于您的问题描述,不/不应涵盖任何额外的需求变更。根据你的评论,看起来你想解决一个问题,现在又要求解决另一个问题。如果是,请单独提问,如果不是,请在问题中(不是通过多次编辑:-)或在评论中一次发布所有查询。这不是社区工作的方式:)第二个问题是我在发布第一个问题后想到的,我习惯于社区不允许发布多个问题,因为这些问题可以在同一个线程中讨论。无论如何,很高兴知道它是如何工作的,下次我会发布一个新问题。非常感谢您的帮助:)
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);
}