?? printarrayelement.java
字號:
/**
* A class to creat an array and print the array element.
* @author Ruixiao Wu
* @version 1.0 24/05/2006
*/
import java.util.ArrayList;
public class PrintArrayElement {
int size;
int maximun;
int position;
public PrintArrayElement(int size,int maximun,int position){//constructor
this.size = size;
this.maximun = maximun;
this.position = position;
}//End constructor
/**
* A method to creat an array randomly.
* And print the element with user input.
* @param size
* @param maximun
*/
public void randomisedArray(int size, int maximun){
ArrayList<Integer> myList= new ArrayList<Integer>();
for(int i=0;i<size;++i){
int randomNUmber = (int)(Math.random()*maximun+1);
myList.add(randomNUmber);
}
System.out.println("The array element in position "+position+" is "+myList.get(position)+".");
}//End method
/**
* The main method to run the program.
* The method contain the try and catch method.
*/
public static void main(String[] args){
int size;
int maximun;
int position;
try{//try whether the input is an integer.
size = Integer.parseInt(args[0]);maximun = Integer.parseInt(args[1]); position = Integer.parseInt(args[2]);
}
catch(Exception wrongInput){//catch the exception and print the error.
System.out.println("Error: Wrong input! Please input three integer!");
return;//return in order to break the program.
}
size = Integer.parseInt(args[0]);
maximun = Integer.parseInt(args[1]);
position = Integer.parseInt(args[2]);
try{//try whether the position is larger than size.
if(size < position)
throw new Exception("Error: The array created has only "+size+" elements."+
"\nPosition "+position+" is greater than the size of the array.");
}//throw the exception
catch(Exception IndexOutOfBoundsException){//catch the exception and print the error.
System.out.println(IndexOutOfBoundsException.getMessage());
return;//return in order to break the program.
}
PrintArrayElement PAE = new PrintArrayElement(size,maximun,position);
PAE.randomisedArray(size,maximun);
}//End main method
}//End class
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -