[英]How to find the location of an element in an array?
我將如何通過用戶輸入來搜索元素的位置。 因此,例如,詢問用戶“您要在數組[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;
}
}
盡管這聽起來像是作業,但到目前為止,您至少已經嘗試了大部分。
這應該工作:
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]);
如果搜索不匹配任何內容或有多個結果,則可能需要添加它以進行處理。
將來的建議。 highest
和lowest
變量分配給數組的第一個元素。 然后它是普遍的。 如果溫度低於-1度怎么辦?
我知道這與問題無關。
我很快就修改了您現有的代碼。 簡而言之,我添加了一種方法,該方法僅掃描用戶輸入是否為double,然后采用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;
}
}
考慮使用ArrayList<Double>
代替原始數組double[]
來存儲溫度,然后使用indexOf(Double d)
方法,該方法將返回列表中首次出現的指定值的索引(如果是,則返回-1不包含值)。
另外,從此ArrayList構造一個TreeSet( TreeSet tree = new TreeSet(temps);
),該對象構造一個包含排序溫度的對象,然后使用tree.first()
和tree.last()
方法獲取最低和最高溫度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.