所以我刚刚完成课堂作业。我们使用顺序和二进制搜索来询问客户他们正在寻找的部件号。然后返回是否找到它,如果找到则返回价格和要求的部件号。所以有2个数组,其中一个用于部件号的整数,一个用于价格的双数组。因此,在顺序和二进制搜索之后查找部件号,我如何获得该部件号的价格?
public class InventoryData
{
Scanner input = new Scanner(System.in);
int[] partNum = new int [200];
double[] priceArray = new double[200];
int partsCount = 0;
int customerPart;
int partPrice;
String inventoryPricing;
double customerPrice;
public InventoryData(String File)
{
inventoryPricing = "file";
}
public void loadArrays()
{
try
{
Scanner infile = new Scanner(new FileInputStream("file"));
while(infile.hasNext())
{
partNum[partsCount] = infile.nextInt();
priceArray[partPrice] = infile.nextDouble();
++partsCount;
}
infile.close();
}
catch (IOException ex)
{
//If file has problems, set the count to -1
partsCount = -1;
ex.printStackTrace();
}
}
public void seqSearch()
{
System.out.println("What part number are you looking for?");
customerPart = input.nextInt();
for(int i=0; i < partNum.length; i++){
if(customerPart == partNum[i])
System.out.println("Sequential search found part" + customerPart);
}
}
public int binarySearch()
{
int first = 0;
int mid=0;
int last = partsCount - 1;
boolean found = false;
System.out.println("What part number are you looking for?");
customerPart = input.nextInt();
while(first <= last && !found)
{
mid = (last + first) /2;
if(partNum[mid] == customerPart)
{
found = true;
}
else if(partNum[mid] < customerPart)
first = mid +1;
else
last = mid-1;
}
if(!found)
{
mid = -1;
}
return mid;
}
public double getPrice()
{
return customerPrice;
}
}// end of inventory data class
答案 0 :(得分:0)
第一个让我提出一些建议: 如果你正在学习如何开发应用程序那么没关系,如果你想要推出som产品然后考虑不使用这样的逻辑,考虑到列表越大,迭代所有数组就越难,直到你也许找点东西......
回到你的问题::)。
你差不多......
for循环中的需要获取数组价格validates_date :start_on, after: lambda { (DateTime.now - 1.month }, before: lambda { (DateTime.now + 1.month }
并获取索引处的元素..
诀窍隐藏在这里: 的 priceArray [I] 强>
priceArray
因此您将获得与用户输入相关的价格值...