如何在数组中找到元素的位置?

时间:2013-08-24 20:02:11

标签: java arrays location element

我如何通过用户输入搜索元素的位置。例如,用户被问到“你想在数组中搜索哪个元素[21.1,1.3,81.1]?”,用户输入“81.1”,给它们的位置。

这是我尝试使用的代码:

import java.net.URL;
import java.util.Scanner;



public class LowestTemperature
{
    public static void main( String[] args) throws Exception
    {
        double[] temps = arrayFromUrl("http://learnjavathehardway.org/txt/avg-daily-temps-atx.txt");

        System.out.println( temps.length + " tempatures in database.");

        double highest = -1, lowest = 9999.99; 
        Scanner input = new Scanner(System.in); 

        for ( int i = 0; i<temps.length; i++)
        {
            if ( temps[i] < lowest ) 
            {
                lowest = temps[i];
            }
            if ( temps[i] > highest)
            {
                highest = temps[i];
            }
        }

        System.out.print( "The lowest average daily tempature was ");
        System.out.println( lowest + "F (" + fToC(lowest) + "C)" );
        System.out.print( "The highest average daily tempature was ");
        System.out.println( highest + "F (" + fToC(highest) + "C)" );   
    }

    public static double[] arrayFromUrl( String url ) throws Exception
    {
        Scanner fin = new Scanner((new URL(url)).openStream());
        int count = fin.nextInt();

        double[] dubs = new double[count];

        for ( int i = 0; i<dubs.length; i++)
            dubs[i] = fin.nextDouble();
        fin.close();

        return dubs;
    }

    public static double fToC( double f)
    {
        return (f-32)*5/9;
    }
}

4 个答案:

答案 0 :(得分:0)

即使这听起来像是家庭作业,但到目前为止,你至少尝试了大部分内容。

这应该有效:

Scanner inputScan = new Scanner(System.in); 
double input = inputScan.nextDouble();

int pos = 0;

for (int i = 0;i < temps.length;i ++){
    if (temps[i] == input){
        pos = i;
        break;
    }
}

System.out.println("Temperature matching your search: "+temps[pos]);

如果搜索与任何内容不匹配或者有多个结果,您可能需要添加它来处理。

答案 1 :(得分:0)

将来的建议。变量highestlowest分配给数组的第一个元素。然后它是普遍的。如果温度低于-1度怎么办?

我知道它与问题无关。

答案 2 :(得分:0)

我很快就修改了你现有的代码。 简而言之,我添加了一个简单地扫描用户输入为double的方法, 然后我接受那个双重循环遍历你提供的数组。 如果找到匹配,则打印出找到该元素的数组的位置 希望这有帮助!

package lowesttemperature;

import java.net.URL;
import java.util.Scanner;


public class LowestTemperature
{
    public static void main( String[] args) throws Exception
    {
        double[] temps = arrayFromUrl("http://learnjavathehardway.org/txt/avg-daily-temps-atx.txt");

        System.out.println( temps.length + " tempatures in database.");

        double highest = -1, lowest = 9999.99; 
        Scanner input = new Scanner(System.in); 

        for ( int i = 0; i<temps.length; i++)
        {
            if ( temps[i] < lowest ) 
            {
                lowest = temps[i];
            }
            if ( temps[i] > highest)
            {
                highest = temps[i];
            }
        }

        System.out.print( "The lowest average daily tempature was ");
        System.out.println( lowest + "F (" + fToC(lowest) + "C)" );
        System.out.print( "The highest average daily tempature was ");
        System.out.println( highest + "F (" + fToC(highest) + "C)" ); 

        double term = searchTerm();
        for (int i = 0; i<temps.length; i++)
        {
            if(temps[i] == term){
                System.out.println("The term you searched for is at position " + i + " in the array"); 
            }
        }
    }

    public static double[] arrayFromUrl( String url ) throws Exception
    {
        Scanner fin = new Scanner((new URL(url)).openStream());
        int count = fin.nextInt();

        double[] dubs = new double[count];

        for ( int i = 0; i<dubs.length; i++)
            dubs[i] = fin.nextDouble();
        fin.close();

        return dubs;
    }

    public static double searchTerm(){
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter a double: "); 
        double input = scan.nextDouble();
        return input;
    }

    public static double fToC( double f)
    {
        return (f-32)*5/9;
    }
}

答案 3 :(得分:0)

考虑使用ArrayList<Double>而不是基本数组double[]来存储温度,然后使用indexOf(Double d)方法返回列表中第一次出现的指定值的索引(如果不包含值,则返回-1)。

此外,从此ArrayList(TreeSet tree = new TreeSet(temps);)构造一个TreeSet,它构造一个包含已排序温度的对象,然后使用tree.first()tree.last()方法获得最低和最高温度。< / p>