gpt4 book ai didi

java - 不太明白如何匹配两个数组

转载 作者:行者123 更新时间:2023-12-01 09:55:41 25 4
gpt4 key购买 nike

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package order;
import java.util.Arrays;
/**
*
* @author Alexander
*/
public class Order {
private static String[] products = {"Compass", "Eraser", "Pen", "Pencil","Pencil Case", "Pencil Sharpener", "Ruler", "Scissors"};
private static double[] prices = {4.5, 0.5, 0.3, 0.6, 10, 0.3, 1.2, 2.5};
public static int orderNum = 0; //
private String productName ;
private double price;
private int discount;
private final int minDiscount = 0;
private final int maxDiscount = 50;
private int quantity;
private final int minQuantity = 0;
private final int maxQuantity = 1000;
private double total;
private String message;
private boolean isDiscounted = false;
private boolean isValidOrder = true;

public Order (){
isValidOrder = false;
message = "**ERROR** : Order number cannot be totalled as no details have been supplied.";
orderNum++;
}
public Order (String productName, int quantity) {
this.quantity = quantity;
this.productName = productName;
testQuantity(quantity);
if (isValidOrder = true){
calculate();
}
orderNum++;
}
public Order (String productName, int quantity, int discount) {
this.productName = productName;
this.quantity = quantity;
this.discount = discount;
testQuantity(quantity);
testDiscount(discount);
getPrice(productName);
if (isValidOrder = true){
calculate();
}
orderNum++;
}
public String getOrderDetails(){
if(isValidOrder == true && isDiscounted == true){
message = "Order Number: " + orderNum + "\n" + "Product Name: " + productName + "\n" + "Product Price: $" + price + "\n" + "Order Quantity: " + quantity + "\n" + "Discount: " + discount + "%" + "\n" + "Total Price: $" + total;
}
else if(isValidOrder == true && isDiscounted == false){
message = "Order Number: " + orderNum + "\n" + "Product Name: " + productName + "\n" + "Product Price: $" + price + "\n" + "Order Quantity: " + quantity + "\n" + "Total Price: $" + total;
}
return message;
}

private void getPrice(String pce) {
Arrays.sort(products);
int searchProductArray = Arrays.binarySearch(products, pce);
if (searchProductArray >= 0) {
price = prices[searchProductArray];
productName = products [searchProductArray];
isValidOrder = true;
}
else {
price = 0.0;
isValidOrder = false;
message = "**ERROR**: Invalid product name";
}
}

public void calculate (){
if (isDiscounted == false){
total = quantity * price;
}
else {
total = quantity * price - quantity * price * (discount/10);
}
}

public void testQuantity(int quantity){
boolean isValidOrder = true;
if (quantity <= minQuantity) {
message = "**ERROR**: Invalid quantity. Quantity cannot be 0 or less";
isValidOrder = false;
}
else if (quantity > maxQuantity) {
message = "**ERROR**: Invalid quantity. Quantity cannot be greater than 1000";
isValidOrder = false;
}
else {
this.quantity = quantity;
this.isValidOrder = true;
}
}

public void testDiscount (int discount) {
boolean isDiscounted = false;
if (discount <= minDiscount) {
message = "**ERROR**: The discount rate cannot be lower than or equal to 0";
isDiscounted = false;
}
else if (discount > maxDiscount) {
message = "**ERROR**: The discount rate cannot be greater than 50";
isValidOrder = false;
}
else {
this.discount = discount;
this.isDiscounted = true;
this.isValidOrder = true;
}
}



/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Order O1 = new Order();
O1.getPrice("Compass");

System.out.println(O1.getOrderDetails());
}
}

getPrice方法接收一个参数:productName。方法是分配一个val,需要使用两个数组来完成这个任务。一个名为 products 的数组用于保存产品名称列表。另一个名为“价格”的数组用于保存该产品的价格。索引位置应该匹配。您需要使用binarySearch 方法找到productName 在products 数组中的索引位置。获得此位置后,您将需要使用此索引位置从价格数组中分配价格。

这是我到目前为止所拥有的,但我不完全确定它是否正确。

最佳答案

不,一般来说这是不正确的。原因是您对产品数组进行排序,但保持价格数组不变,因此产品通常与它们的价格不对应,即索引位置不匹配。

  private void getPrice(String s) {
Arrays.sort(products); <--- sort products, but prices' indices remain unchanged
int searchedProductIndex = Arrays.binarySearch(products, s);
if (searchedProductIndex >= 0) {
this.price = prices[searchedProductIndex];
this.isValidOrder = true;
} else {
price = 0.0;
isValidOrder = false;
message = "**ERROR**: Invalid product name";
}
} <--- closing curly bracket

请记住以下几点。

  • 二分查找只能用于排序集合
  • 当您对产品数组进行排序时,您必须相应地修改价格数组,以便索引位置匹配。您不会在您的应用中执行此操作。
  • 另外,请注意检查您的语法。您复制的代码无法编译。例如。您错过了 getPrice 函数的右括号。

关于java - 不太明白如何匹配两个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37247964/

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