JAVA lab2

此代码包含两个Java类Pattern1和Pattern2,分别用于打印两种不同的数字模式。Pattern1打印从1到指定数字的对称三角形,Pattern2则打印倒置的对称三角形。另外,Pattern类中封装了这两个方法,并在main方法中调用以展示两种模式。MyCount类提供了一个countUp方法,用于从0开始递增计数到指定数字,并在控制台打印过程。

在这里插入图片描述

public class Pattern1{
	public static void main(String[] args){
		int n = Integer.parseInt(args[0]);
		for(int i=1;i<=n;i++){
			for(int j=1;j<=i;j++){
				System.out.printf("%d ",j);
			}
			System.out.println();
		}
	}
}

在这里插入图片描述

public class Pattern2{
	public static void main(String[] args){
		int n = Integer.parseInt(args[0]);
		int n2=n;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n2;j++){
				System.out.printf("%d ",j);	
			}
			n2--;
			System.out.println();
		}
	}
}

在这里插入图片描述

public class Pattern{
	public void printPattern1(int n){
		for(int i=1;i<=n;i++){
			for(int j=1;j<=i;j++){
				System.out.printf("%d ",j);
			}
			System.out.println();
		}
	}
	public void printPattern2(int n){
		int n2=n;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n2;j++){
				System.out.printf("%d ",j);	
			}
			n2--;
			System.out.println();
		}
	}
	public static void main(String[] args){
		int n= Integer.parseInt(args[0]);
		Pattern p=new Pattern();
		p.printPattern1(n);
		p.printPattern2(n);
	}
}

在这里插入图片描述

/*
*This is my first properly documented piece java code.
*When the javadoc tool is run over this code, it will
*create an HTML maintenance manual.
*@author Fangzheng Li
*@version 1.0
*/
public class MyCount{
	/*
	*This method counts up from zero to a specified number
	* It will print its progress to the command line 
	* @param count The number to count from. 
	*/
	public void countUp(int count){
	/*Note: If there are no brackets after a for loop,
	* it is only the sentence that immediatly follows which is part of the loop;
	*/
		for(int i=1;i<=count;i++){
			System.out.println(i);
		}
		System.out.println("\nAll done!");
	}

	/*
	*Main now only creates a new instance of my program
	* and calls the program's method .
	* @param args The program doesnot use this parameter .
	*/
	public static void main(String[] args){
		MyCount test=new MyCount();
		test.countUp(5);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值