无法连接两个用户输入。

huangapple go评论61阅读模式
英文:

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&lt;Item&gt; items = IntStream.rangeClosed(1,2)
.mapToObj(Question63869241::getItemInputFromUser)
.collect(Collectors.toList());
Item expensiveItem = getMostExpensiveItem(items);
Item cheapestItem = getCheapestItem(items);
System.out.println(&quot;\nThe most expensive item &#39;&quot; + expensiveItem.getItemName()
+ &quot;&#39; is priced at: &quot; + expensiveItem.getPrice()  );
System.out.println(&quot;\nThe cheapest item &#39;&quot; + cheapestItem.getItemName()
+ &quot;&#39; is priced at: &quot; + cheapestItem.getPrice() );
}
private static Item getItemInputFromUser(int sequence){
System.out.println(&quot;\nEnter the name of item &quot; + sequence);
String name = scanner.next();
System.out.println(&quot;\nEnter the price of &#39;&quot; + name + &quot;&#39;: &quot;);
double price = scanner.nextDouble();
return new Item(name, price);
}
private static Item getMostExpensiveItem(List&lt;Item&gt; items){
return items.stream()
.sorted(Comparator.comparing(Item::getPrice).reversed())
.findFirst().get();
}
private static Item getCheapestItem(List&lt;Item&gt; 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(&quot;Enter the name of item 1: &quot;);
String oneName = scan.nextLine();
System.out.print(&quot;Enter the price of item 1: &quot;);
double onePrice = scan.nextDouble();
scan.nextLine();
System.out.print(&quot;Enter the name of item 2: &quot;);
String twoName = scan.nextLine();
System.out.print(&quot;Enter the price of item 2: &quot;);
double twoPrice = scan.nextDouble();
int compare = Double.compare(onePrice, twoPrice);
if (compare == 0)
System.out.format(Locale.ENGLISH, &quot;Both prices for item 1 (%s) and item 2 (%s) are equal: %.2f\n&quot;, oneName, twoName, onePrice);
else if (compare &gt; 0) {
System.out.format(Locale.ENGLISH, &quot;The most expensive item is 1 (%s): %.2f\n&quot;, oneName, onePrice);
System.out.format(Locale.ENGLISH, &quot;The cheapest item is 2 (%s): %.2f\n&quot;, twoName, twoPrice);
} else {
System.out.format(Locale.ENGLISH, &quot;The most expensive item is 2 (%s): %.2f\n&quot;, twoName, twoPrice);
System.out.format(Locale.ENGLISH, &quot;The cheapest item is 1 (%s): %.2f\n&quot;, oneName, onePrice);
}

huangapple
  • 本文由 发表于 2020年9月13日 17:36:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63869241.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定