gpt4 book ai didi

java - 如何使数组按 Product[counter].getProductTitle() 排序?

转载 作者:行者123 更新时间:2023-11-29 08:19:33 25 4
gpt4 key购买 nike

问题:productArray 是从哪里来的:

for ( Product product : productArray )

&

Arrays.sort( productArray, new ProductComparator() );

问题:我做错了什么?我如何进行这种排序?

相关Post从昨天开始


                                 EDIT

:::编辑:::好的,我在这里听取了你的建议

Product productArray[] = new Product[ARRAY_LENGTH]

现在这里坏了

/tmp/jc_22339/InventoryPart2.java:99: cannot find symbol
symbol : variable productArray
location: class InventoryPart2
for ( Product product : productArray ) {
^
/tmp/jc_22339/InventoryPart2.java:101: cannot find symbol
symbol : variable productArray
location: class InventoryPart2
inventoryValue += productArray.calcInventoryValue();
^

如果我这样做的话

for ( Product product : productArray[] ) {

我明白了

/tmp/jc_23524/InventoryPart2.java:69: '.class' expected
for ( Product product : productArray[] ) {
^

所以我又卡住了。


开始程序

:::更新代码:::

    /**
This program stores a collection of a product and its variables in a java array
It will sort and display the information with a total
*/

// Import statements go here
import java.util.Scanner;// Import and use scanner
import java.util.Arrays;


public class InventoryPart2 {

/**
* @param args the command line arguments
*/

public static void main(String[] args) {// begin main

// Define your array of product objects and index here
final int ARRAY_LENGTH = 5;// declare constant
final int version = 2;// declare int version number

// Create instance of Scanner class
Scanner input = new Scanner( System.in );// new Scanner for CL input
// Set counter to loop x times to populate your array of product objects
int counter = 0;

// Initialize your product array with the number of objects to populate it with
Product productArray[] = new Product[ARRAY_LENGTH];// create array Product of class Product

// Welcome message
System.out.printf( "\n%s%d\n" ,
"Welcome to the Productentory Program v.", version );

// Construct default values for Product
productArray[0] = new Product("EThe Invisible Man", 0, 8.50);
productArray[1] = new Product("DThe Matrix", 1, 17.99);
productArray[2] = new Product("CSe7en", 7, 12.99);
productArray[3] = new Product("BOceans Eleven", 11, 9.99);
productArray[4] = new Product("AHitch Hikers Guide to the Galaxy", 42, 18.69);

/*// Loop as many times as your counter variable
{

// Instantiate a product object

// Prompt for product name and call the set method on your product object

// Prompt for item number and call the set method on your product object

// Prompt for units in stock and call the set method on your product object

// Prompt for unit price and call the set method on your product object

// store product object in array element

//Destroy product object reference
product = null;

// Flush the buffer
input.nextLine();

}*/

// Sort product array by product name using Comparator implementation class
sortProductArray();

// Print sorted array
for ( Product product : productArray[] ) {
if ( counter == 0 )
System.out.printf( "\n%s\n", "Sorted Inventory of DVD movies");
System.out.printf( "\n\n%s%d\n%s%s\n%s%d\n%s%,.2f\n%s%,.2f\n",
"Item Number: ",counter,
"DVD Title: ",productArray[counter].getProductTitle(),
"Copies in stock: ",productArray[counter].getUnitsInStock(),
"Price each disk: $",productArray[counter].getUnitPrice(),
"Value of disks: $",productArray[counter].calcInventoryValue());//End print
counter++;
if ( counter == productArray.length)// on last counter of loop print total
System.out.printf( "\n%s%,.2f\n\n\n",
"Collection Value: $",calcTotalInventoryValue());
}

// Calculate total Inventory value

}


// method to calculate the total Inventory value
private static double calcTotalInventoryValue()
{

double inventoryValue = 0;

// Iterate array of product objects and calculate total value of entire Inventory
for ( Product product : productArray ) {
// accumulate inventory value from each product object in array
inventoryValue += productArray.calcInventoryValue();
}

return totalInventoryValue;

} // end method calcInventoryValue


// method to sort product array
private static void sortProductArray()
{

Arrays.sort( productArray, new ProductComparator() );



} // end method calcInventoryValue


}

最佳答案

这个

Product Product[] = new Product[ARRAY_LENGTH];

应该是这样的:

Product productArray[] = new Product[ARRAY_LENGTH];

-- 编辑

您还需要更改引用此“变量”的相关行。

如果我可以建议,如果你对此感到困惑,你可以让你的老师/书来刷新你对“变量”的内存。

如果您不明白,请不要担心:不断提问,直到您明白为止。如果你有足够的兴趣去学习,你就被淘汰了。因此,保持兴趣、提出问题、进行自己的研究,生活就会变得美好。

关于java - 如何使数组按 Product[counter].getProductTitle() 排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1314801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com