사용자 입력을 통해 요소의 위치를 검색하는 방법은 무엇입니까? 예를 들어, 사용자에게 "배열 [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;
}
}
감사합니다. 도움에 감사드립니다! – user2690972