好的,我是计算机科学的新手,我很难用这个。如果以下是如何在数据中找到最低的双倍,我将如何找到最高值?
double lowestprice = 99999;
int posoflowest = 0;
int numberofitems = 0;
for (int i = 0; i < posoflowest; i++)
{
if(data[1].getPrice() < lowestprice)
{
lowestprice = data[i].getPrice();
posoflowest = 1;
}
}
答案 0 :(得分:3)
像
这样的东西double highestprice = 0;
for (int i = 0; i < data.length; i++)
{
if(data[i].getPrice() > highestprice)
{
highestprice = data[i].getPrice();
}
}
答案 1 :(得分:1)
你会想要这样的东西:
double highestPrice = 0;
int posOfHighest = 0;
int numberOfItems = data.length();
for (int i = 0; i < numberOfItems; i++)
{
if(data[i].getPrice() > highestPrice )
{
highestPrice = data[i].getPrice();
posOfHighest= i;
}
}
答案 2 :(得分:0)
暗示你可以将双打变成双打数组
double[] array = new double[] { 1.1, 9.15, 7.15, 4.51 };
简单地做
Arrays.sort(array);
稍后,
System.out.println("Largest double in array: " + array[array.length - 1]);
输出:
Largest double in array: 9.15
如果你不能把它们放在一个数组中,你应该尝试一下,考虑到你刚刚开始,这将是一件好事。
答案 3 :(得分:0)
我认为这不会找到最低的 - 我不认为你的'for'条件是正确的,你有'1'你想要'i'。希望这会纠正:
double lowestprice = 99999; // The current lowest value
int posoflowest = 0; // Where in the collection of values the lowest value is
int numberofitems = 0; // How many
// For each double in your collection. This will iterate from the first item in
// data[] to the last.
for (int i = 0; i < numberofitems ; i++)
{
// Is the current price lower than the lowest we have found?
// So the first time around, this will see if the price of the first item in
// data[] is lower than 99999. If it is, we update the lowest price and set
// 'posoflowest'.
if(data[i].getPrice() < lowestprice)
{
lowestprice = data[i].getPrice();
posoflowest = i;
}
}
如果我们认为可以正确地获得最低价值,我们可以添加一些非常相似的东西:
double lowestPrice = 99999; // The current lowest value
double highestPrice = 0; // The current highest price
int posOfLowest = 0; // Where in data[] is the lowest priced item?
int posOfHighest = 0; // Where in data[] is the highest priced item?
int numberOfItems = 0; // How many items in data[]?
// For each double in your collection. This will iterate from the first item in
// data[] to the last.
for (int i = 0; i < numberOfItems ; i++)
{
// Is the current price lower than the lowest we have found?
if(data[i].getPrice() < lowestPrice)
{
lowestPrice = data[i].getPrice();
posOfLowest = i;
}
if(data[i].getPrice() > highestPrice)
{
highestPrice= data[i].getPrice();
posOfHighest = i;
}
}
希望这是有道理的。