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