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

📄 diamond.java

📁 如果输入为1个奇数值n
💻 JAVA
字号:
/** 
public class Diamond

The Diamond class can be used to get the graph of diamond which depends on the input value in the command line. The Rectangle class is also defined in this file. 

Version:1.0

Author: Fan Zhang (Student ID: 291652)

Organisation: Department of Computer Science and Software Engineering, Faculty of Engineering, University of Melbourne
*/

 
public class Diamond
{

	public static void main(String args[])   /* Prints a diamond if there is only one input value in the command line, and the width of diamond have the                                                    same number of the odd input value; If the input value is even, the width of the diamond should be one                                                        less than the input value; If there are two input values, prints out a rectangle. For negative values,                                                        no print, and for fractional input values, the integer part determines the diamond or rectangle's width. */
	
        {
		if(args.length == 1)    
		{

	        	double N = Double.parseDouble(args[0]);  // Transfering the input value type to double type
        		int X=(int)N;  //Transfering the double type value N to int type, and give the value to X

	       	 	for (int i = -(X-1)/2; i <= (X-1)/2; i++)  /* Set the middle horizontal row of the diamond as zero row, and gives i the initial                                                                           value of -(X-1)/2. The -(X-1)/2 was induced by using mathymatical inductive method. */
                        {
            			for (int j = -(X-1)/2; j <= (X-1)/2; j++) 
				{
                			if (Math.abs(i) + Math.abs(j) <= (X-1)/2) System.out.print("*");   /* Plus the absolute value i and absolute value j,                                                                                                            if the total value no more than (X-1)/2, print"*",                                                                                                            or print" ", the (X-1)/2 was induced by using                                                                                                                 mathymatical inductive method */ 
                                        else                                System.out.print(" "); 

            			}
            			System.out.println();
        		}
    		}
	
		if(args.length == 2)
		{
			int a = (int)Double.parseDouble(args[0]);
			int b = (int)Double.parseDouble(args[1]);
			new Rectangle().print(a,b);  //use the Rectangle class
	
		}
	}
}
class Rectangle /* Prints a rectangle which the number of horizortal row is the first input value, and the number of vertical row is the second input vaule*/
{	
	public void print(int a,int b)
{
			for(int i=1; i<=b;i++)
			{
				for(int j =1;j<=a;j++)
				{
					System.out.print('*');
				}
			System.out.println();
			}
}
}
                                                                                                             
                                       

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -