0

我将如何通过用户输入搜索元素的位置。例如,用户被问到“你想在数组 [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

4 回答 4

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]);

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

于 2013-08-24T20:08:12.517 回答
0

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

我知道这与问题无关。

于 2013-08-24T20:11:10.110 回答
0

我很快就修改了您现有的代码。简而言之,我添加了一个简单地扫描用户输入以查找双精度的方法,然后我获取该双精度并循环遍历您提供的数组。如果找到匹配项,它会打印出在数组的哪个位置找到了该元素。
希望这可以帮助!

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;
    }
}
于 2013-08-24T20:26:03.633 回答
0

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

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

于 2013-08-24T20:54:56.940 回答