Java ProductLoop用户问有多少产品最贵

Java ProductLoop用户问有多少产品最贵,java,Java,我在这个节目上玩得很开心。有人能告诉我我做错了什么吗?程序提示用户输入产品目录中的产品数量。然后,程序应提示用户输入产品目录中每种产品的名称和价格。输入所有产品后,程序应输出目录中最昂贵产品的产品信息(名称和价格)。您的解决方案可以跟踪价格最高的产品 import java.util.Scanner; public class ProductTester { private static final String price = null; public static voi

我在这个节目上玩得很开心。有人能告诉我我做错了什么吗?程序提示用户输入产品目录中的产品数量。然后,程序应提示用户输入产品目录中每种产品的名称和价格。输入所有产品后,程序应输出目录中最昂贵产品的产品信息(名称和价格)。您的解决方案可以跟踪价格最高的产品

import java.util.Scanner;

public class ProductTester
{
    private static final String price = null;

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        System.out.print("Enter the number of Products: ");
        int count = in.nextInt();
        int name = 1;
        int item = 1;
        for (int i = 1; i <= count; i++) {
            System.out.print("Enter the Name of Product " + item + " :\n");
            String name1 = in.toString();
            System.out.print("Enter the Price of Product " + item + " :");
            int price = in.nextInt();

            item++;
        }
        System.out.println("Product = " + name + "$ " + price);
    }
}
import java.util.Scanner;
公共类ProductTester
{
私有静态最终字符串价格=null;
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
System.out.print(“输入产品编号:”);
int count=in.nextInt();
int name=1;
int项=1;

对于(int i=1;i首先,您读取的输入错误:

这一行:

String name1 = in.toString();
应该是:

String name1 = in.next();
通过在.toString()
中调用
,您正在调用将返回此
扫描仪的字符串表示形式的方法。此方法从
对象
类覆盖,该对象是Java中所有类的父类,通常用于调试目的

在.next()中调用
时,您将读取
扫描仪的输入流中的下一个
字符串
标记

其次,在循环中没有使用
name1
变量或
int price
变量

本部分:

然后,程序应提示用户输入产品目录中每种产品的名称和价格

现在,您所做的只是在循环中创建变量
name1
price
,而不是将它们用于任何事情。在上一次打印语句中,打印的
name
price
是您在程序开始时指定的值

// the values shown bellow are printed

// ...
private static final String price = null;
// ...
int name = 1;
// ...
因此,您的程序将始终打印:

Product = 1$ null
您应该删除
最终静态字符串price
变量,因为它没有任何实际用途

对于存储产品,您可能希望使用Java集合中的对象,例如
ArrayList
。为此,您应该创建一个名为
Product
的类,该类保存产品名称和价格,然后构建它们的列表

class Product {
    String name;
    int price;
}
这将是一种更好、更干净、面向对象的方法来解决您的问题。但是,您还可以在两个变量中跟踪最大的价格和产品名称(就像您的计划一样),因此我将向您展示如何做到这一点

输入所有产品后,程序应输出目录中最昂贵产品的产品信息(名称和价格)

因此,您可以使用两个变量,
String expProdName
int expProdPrice
来跟踪最昂贵的产品信息。您还需要两个临时变量,
String currProdName
int currProdPrice
来从用户处获取当前输入的信息

在循环中,您使用变量
item
打印产品订单,但您可以使用循环计数器
i
来执行此操作

您应该将迄今为止最昂贵的价格
expProdPrice
初始化为
Integer.minu值
,这是可能的最低整数值,以便与新价格进行比较一定会找到最高价格。但是,考虑到产品价格不应为负值,您也可以将其初始化为-1

然后在循环中,每次读取新价格时,将其与存储在
expProdPrice
中的值进行比较,如果该值更大,则将最昂贵的价格更新为此新价格。 您还需要更新产品名称

让我们看看代码的外观:

public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    System.out.print("Enter the number of Products: ");
    int count = in.nextInt();

    String expProdName = ""; // expensive product name
    int expProdPrice = Integer.MIN_VALUE; // expensive product price

    String currProdName = ""; // current product name
    int currProdPrice = -1; // current product price

    for (int i = 1; i <= count; i++) {
        System.out.print("Enter the Name of Product " + i + " :\n");
        currProdName = in.next();
        System.out.print("Enter the Price of Product " + i + " :\n");
        currProdPrice = in.nextInt();

        // if current price larger that the most expensive price thus far
        // update the most expensive price to the current price and
        // update the name of the most expensive product to current product's name
        if(currProdPrice > expProdPrice) { 
            expProdPrice = currProdPrice;
            expProdName = currProdName; 
        }
    }
    System.out.println("\nMost expensive product is:\n\nProduct name:\t" + expProdName + 
                       "\nProduct price:\t" + expProdPrice + "$");
}
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
System.out.print(“输入产品编号:”);
int count=in.nextInt();
字符串EXPRODNAME=“”;//昂贵的产品名称
int expProdPrice=Integer.MIN_VALUE;//昂贵的产品价格
字符串currProdName=“;//当前产品名称
int currProdPrice=-1;//当前产品价格
对于(inti=1;i这里


String name1=in.toString();我知道哪里出了问题,但我也发现了一个逻辑错误,那就是我的system.out.print(“…”):我把price作为变量的地方实际上应该是其他的东西,而不是价格问题的变量。我知道哪里出了问题。我改成了。next();用于我的产品线。我还需要改变一些其他事情,但这是我觉得我应该开枪自杀的时候之一。我应该知道。嘿,谢谢。是的,我已经解决了。谢谢。我正在试图找出如何获得最昂贵的计算。我遇到的问题是我的价格变量是开着的一点也不奇怪。为了避免听起来很愚蠢,我需要想出另一种方法。以获得最昂贵的产品。我假设有一个输出,在询问价格后插入电源后,将显示最昂贵的产品。
String name1 = in.next(); <-- reads one token which is type
                               String from System.in for you