⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 printarrayelement.java

📁 a program that takes in three arguments: the first one represents the size of an array that will be
💻 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 + -