如何找到数组元素的位置? [英] How to find the location of the element of an array?
问题描述
我有一个包含5个元素的数组。现在,我想要的是,当我输入其中一个元素时,程序应该显示输入数组的地址。
import java.util.Scanner;
public class location_of_an_element_in_array
{
public static void main( String [] args)
{
int arr [];
arr = new int [ 5 跨度>];
int i;
int search;
扫描仪输入= 新扫描仪(System.in)
for (i = 1; i< = 5; i ++)
{
System.out.println( 输入数字:);
arr [i] = input.nextInt();
}
System.out.println( \ n什么号码的位置你想搜索?);
search = input.nextInt();
}
}
这是我到目前为止所做的。请帮忙说明如何完成所需的任务。
你的代码很弱:
- 类名以大写字母开头。总是!
- 每个代码行都必须以;结束,然后根据需要分配变量 - 在循环标题中,当要求输入时值。这限制了范围(在这个例子中没有,但一般来说)
- for循环:
- 所有与人类相邻的都是从0算起来的。请来。
- 因此从0到4计算一个包含5个元素的循环(结束条件< 5优于< = 4)。
public class Location_of_an_element_in_array {
public static void main( String [] args){
int arr [] = new int [ 5 ];
// fill array
扫描仪输入= new Scanner(System.in);
for ( int i = 0 ; i< 5 ; i ++){
System.out.println( 输入数字:);
arr [i] = input.nextInt();
}
// search
系统。 out.println( \ n您要搜索哪个号码的位置?);
int search = input.nextInt();
// TODO:在这里添加搜索循环!
}
}
现在代码运行。
请使用Eclipse或Netbeans用于编码 - 两个IDE都将指导您完成整个过程!没有理由不使用其中之一。
这是一项家庭作业。您必须自己添加搜索。请问你什么时候陷入困境,调试代码可以帮助你很多(根据你正在使用的IDE谷歌那个)。
有有趣!块引用>
i have an array with 5 elements in it. Now, what I want is, when I enter one of those elements, the program should show me the address of that entered array.
import java.util.Scanner; public class location_of_an_element_in_array { public static void main(String[] args) { int arr[]; arr = new int[5]; int i; int search; Scanner input = new Scanner(System.in) for(i=1;i<=5;i++) { System.out.println("Enter number:"); arr[i] = input.nextInt(); } System.out.println("\nWhich number's locattion you want to search?"); search = input.nextInt(); } }
This is what I''ve done so far. Please help on how to achieve the desired task.解决方案Your code is quite weak:
- class names start with capital letter. Always!
- each code line has to end with a ";"
- init and assign variables as you need them - in the loop-header, when asking for a value. That limits the scope (not here in the example, but in general)
- the for-loop:
- all being beside of humans do count from 0. Do so too please.
- a loop with 5 elements is counted therefore from 0-4 (end condition "<5" is preferable over "<=4").
public class Location_of_an_element_in_array { public static void main(String[] args) { int arr[] = new int[5]; // fill Array Scanner input = new Scanner(System.in); for (int i = 0; i < 5; i++) { System.out.println("Enter number:"); arr[i] = input.nextInt(); } // search System.out.println("\nWhich number's location you want to search?"); int search = input.nextInt(); //TODO: add loop with search here! } }
Now the code runs.
Please use Eclipse or Netbeans for coding - both IDE will guide you through the process! There is no reason not to use one of them.
This is a homework task. You will have to add the search yourself. Please ask when you get stuck in that, also debugging the code can help you a lot (google for that according to the IDE you''re using).
Have fun!
这篇关于如何找到数组元素的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!