[英]Product Loop User asked how many Product Most Expensive
我正在這個程序上玩。 有人可以告訴我我在做什么錯嗎? 程序提示用戶在產品目錄中輸入產品編號。 然后,程序應提示用戶輸入產品目錄中每種產品的名稱和價格。 輸入所有產品后,程序應輸出目錄中最昂貴產品的產品信息(名稱和價格)。 您的解決方案,以最高的價格跟蹤產品。
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
類覆蓋, Object
類是Java中所有類的父類,通常用於調試目的。
調用in.next()
您將讀取Scanner
輸入流中的下一個String
令牌。
其次,您沒有在循環中使用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
您應該只刪除final static String price
變量,因為它沒有任何實際用途。
為了存儲產品,您可能要使用Java集合中的對象,例如ArrayList
。 為此,您應該創建一個名為Product
的類,其中包含產品名稱和價格,然后構建它們的列表。
class Product {
String name;
int price;
}
這將是一種更好,更清潔,面向對象的方法來解決您的問題。 但是,您也可以在兩個變量中跟蹤最大價格和產品名稱(似乎您的計划是這樣),所以我將向您展示如何做到這一點。
輸入所有產品后,程序應輸出目錄中最昂貴產品的產品信息(名稱和價格)。
因此,可以使用兩個變量String expProdName
和int expProdPrice
來跟蹤最昂貴的產品信息。 您還需要兩個臨時變量String currProdName
和int currProdPrice
以從用戶獲取當前輸入的信息。
在循環內部,您正在使用可變item
來打印產品的順序,但是您可以僅使用循環計數器i
來執行此操作。
您應該將expProdPrice
初始化為Integer.MIN_VALUE
,這是到目前為止最昂貴的價格, 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 + "$");
}
這里
String name1 = in.toString();<-- you do not read anything from the console
you just convert Scanner to String which does not make sense at all
改成
String name1 = in.next(); <-- reads one token which is type
String from System.in for you
java.util.Scanner.next()方法從此掃描器中查找並返回下一個完整的令牌。 完整的標記在其前面,然后是與定界符模式匹配的輸入。 即使先前調用hasNext()返回true,此方法也可能在等待輸入掃描時阻塞。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.