JAVA练习(九九乘法表、使用循环输出等腰三角形)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_38358499/article/details/99490024

需求一:打印出九九乘法表

public class MultiplicationTable 
{
	public static void main(String[] args)
	{
		int a;
		int b;
		int c;
		for(int i = 1; i < 10; i++)
		{
			for(int j = 1; j <= i; j++)
			{
				a = i;
				b = j;
				c = a * b;
				System.out.print(a + " * " + b + " = " + c + "    ");
			}
			System.out.println("\n");
		}
	}
}

需求二:使用循环输出等腰三角形      

 

import java.util.Scanner;
public class IsoscelesTriangle 
{
	public static void main(String[] args)
	{
		System.out.println("请输入一个整数:");
		//在输出端输入一个数
		Scanner input = new Scanner(System.in);
		int num = input.nextInt();
		for(int i = 1; i <= num; i++)
		{
			int a = num - i;
			while(a != 0)
			{
				System.out.print(" ");
				a--;
			}
			int b = i * 2 - 1;
			while(b != 0) 
			{
				System.out.print("*");
				b--;
			}
			System.out.println("\n");
		}		
	}
}

猜你喜欢

转载自blog.csdn.net/qq_38358499/article/details/99490024