讀取數組中某一個數據
//ReadArray.java
import java.util.Scanner;
public class ReadArray{
public static void main(String[] args){
int[] array=new int[5];
for(int i=0;i<array.length;i++)
array[i]=i+4;
Scanner input=new Scanner(System.in);
System.out.println("請輸入您要讀取的數組中第幾個元素:");
int index=input.nextInt();
try{
System.out.println("元素的值爲:"+array[index-1]);
}catch(java.lang.ArrayIndexOutOfBoundsException e){
System.out.println("您輸入有誤,數組中只有五個元素");
}finally{
System.out.println("程序結束");
}
}
}
import java.util.Scanner;
public class ReadArray{
public static void main(String[] args){
int[] array=new int[5];
for(int i=0;i<array.length;i++)
array[i]=i+4;
Scanner input=new Scanner(System.in);
System.out.println("請輸入您要讀取的數組中第幾個元素:");
int index=input.nextInt();
try{
System.out.println("元素的值爲:"+array[index-1]);
}catch(java.lang.ArrayIndexOutOfBoundsException e){
System.out.println("您輸入有誤,數組中只有五個元素");
}finally{
System.out.println("程序結束");
}
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.