Java Can';似乎无法访问我的数组值,即使它工作时也是如此
出于某种原因,我似乎无法访问我创建的For循环之外的数组值。我已经在本地声明了数组。但是当我必须由用户输入一个值时,由于某种原因,绑定值会一直给我Java Can';似乎无法访问我的数组值,即使它工作时也是如此,java,arrays,Java,Arrays,出于某种原因,我似乎无法访问我创建的For循环之外的数组值。我已经在本地声明了数组。但是当我必须由用户输入一个值时,由于某种原因,绑定值会一直给我0。有没有一种方法可以从for循环中的数组中获取信息,可以说是“泛化”,这样它就可以用于其他函数中 import java.util.*; public class Array { public static void main(String[] args) throws IndexOutOfBoundsException {
0
。有没有一种方法可以从for循环中的数组中获取信息,可以说是“泛化”,这样它就可以用于其他函数中
import java.util.*;
public class Array {
public static void main(String[] args) throws IndexOutOfBoundsException {
int[] array = new int[100];
try{
for(int i: array){
array[i] = (int)(Math.random()*100);
System.out.println(array[i]);
}
Scanner input = new Scanner(System.in);
System.out.println("Please enter an index for which you would like to see: ");
int index = input.nextInt();
System.out.println(array[index]);
}catch(IndexOutOfBoundsException ex){
System.out.println("Please enter in an index thta is not out of bounds");
}finally{System.out.println("--------------------------");}
}
}
这是因为您使用的是增强型
for loop
您没有初始化数组的每个元素,唯一获得该值的元素是第一个元素零。声明数组时,所有值都设置为零。因此,在增强的for循环中
设置了数组[0]
的值,这就是为什么输出为零
将增强型for loop
替换为:
int[] array = new int[100];
try{
for(int i=0;i<array.length;i++){
array[i] = (int)(Math.random()*100);
System.out.println(array[i]);
}
Scanner input = new Scanner(System.in);
System.out.println("Please enter an index for which you would like to see: ");
int index = input.nextInt();
System.out.println(array[index]);
}catch(IndexOutOfBoundsException ex){
System.out.println("Please enter in an index thta is not out of bounds");
}finally{
System.out.println("--------------------------");
}
int[]数组=新的int[100];
试一试{
对于(int i=0;i尝试使用此for循环,它应该可以工作:
for(int i=0;i<array.length;i++){
array[i] = (int)(Math.random()*100);
System.out.println(array[i]);
}
运行此代码时,第一个和第二个元素将打印1和2
希望现在一切都清楚了。当你声明数组时,你给它100个索引。它们会自动变成所有的0。你的for-each循环只是给数组中的第0个索引分配一个随机数,其他的都保持为0。你实际上并没有访问数组中的第i个元素
这将修复您的代码:
public static void main(String[] args) {
int[] array = new int[100];
try{
for(int i=0; i<array.length; i++){
array[i] = (int)(Math.random()*100);
System.out.println(array[i]);
}
Scanner input = new Scanner(System.in);
System.out.println("Please enter an index for which you would like to see: ");
int index = input.nextInt();
System.out.println(array[index]);
}catch(IndexOutOfBoundsException ex){
System.out.println("Please enter in an index thta is not out of bounds");
}finally{System.out.println("--------------------------");}
}
}
publicstaticvoidmain(字符串[]args){
int[]数组=新的int[100];
试一试{
对于(inti=0;我认为这是一个面向头部的手掌。但是逻辑上不应该是“增强的”for具有常规for循环的相对功能,然后是一些?@Dcdw51我在我的答案中添加了解释,请查看。增强for循环仅用于迭代,而不用于常规数组赋值,因为赋值变量数组的索引值不会增加
public static void main(String[] args) {
int[] array = new int[100];
try{
for(int i=0; i<array.length; i++){
array[i] = (int)(Math.random()*100);
System.out.println(array[i]);
}
Scanner input = new Scanner(System.in);
System.out.println("Please enter an index for which you would like to see: ");
int index = input.nextInt();
System.out.println(array[index]);
}catch(IndexOutOfBoundsException ex){
System.out.println("Please enter in an index thta is not out of bounds");
}finally{System.out.println("--------------------------");}
}
}