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

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);
    }
}


当我运行该程序时,它询问我出现多少次以及何时将其放入两个问题中,而我却无法输入任何内容。实际上,产品问题应该首先出现,然后是价格问题。

最佳答案

首先,您正在读取错误的输入:

这行:

String name1 = in.toString();


应该:

String name1 = in.next();


通过调用in.toString(),您正在调用将返回此Scanner的字符串表示形式的方法。此方法从Object类覆盖,该类是Java中所有类的父类,通常用于调试目的。

调用in.next()时,您将读取String的输入流中的下一个Scanner令牌。

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

对于这一部分:


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


现在,您要做的就是在循环中创建变量name1price,而不用它们做任何事情。在最后一个打印语句中,打印的nameprice是您在程序开始时分配的值。

// the values shown bellow are printed

// ...
private static final String price = null;
// ...
int name = 1;
// ...


因此,您的程序将始终打印:

Product = 1$ null


您应该删除final static String price变量,因为它没有任何实际用途。

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

class Product {
    String name;
    int price;
}


这将是一种更好,更清洁,面向对象的方法来解决您的问题。但是,您也可以在两个变量中跟踪最大价格和产品名称(似乎您的计划是这样),所以我将向您展示如何做到这一点。


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


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

在循环内部,您正在使用变量item来打印产品的订单,但是您可以只使用循环计数器i来完成。

您应该将expProdPrice(这是迄今为止最昂贵的价格)初始化为Integer.MIN_VALUE(这是可能的最低整数值),以便与新价格进行比较可以确定找到了最高价格。但是,考虑到产品价格不应为负,您也可以将其初始化为-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 + "$");
}

10-06 12:57
查看更多