利用java打印小图形
1、打印等腰三角形
import java.util.Scanner;
//打印等腰三角形
public class Test01 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入等腰三角形的高:");
int sum = input.nextInt();
for(int i = 1; i <= sum; i++){
for (int j = 0; j <= sum-i; j++){
System.out.print(" ");
}
for (int k = 1; k <= 2*i-1; k++){
System.out.print("*");
}
System.out.println();
}
}
}
2、打印空心等腰三角形
import java.util.Scanner;
//打印空心等腰三角形
public class Test02 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入等腰三角形的高:");
int sum = input.nextInt();
for(int i = 1; i <= sum; i++){
for (int j = 0; j <= sum-i; j++){
System.out.print(" ");
}
for (int j = 1; j <= 2*i-1; j++){
if (i == sum){ //打印最后一行
if (j % 2 != 0){ //为了美观,最后一行进行间隔性打印
System.out.print("*");
}else{
System.out.print(" ");
}
}else{ //进行等腰三角形空心操作
if (j == 1 || j == 2*i-1){
System.out.print("*");
}else{
System.out.print(" ");
}
}
}
System.out.println();
}
}
}
3、打印菱形
//由于菱形的打印条件限制,该代码只能实现奇数行的菱形
import java.util.Scanner;
//打印菱形
public class Test03 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入菱形的高:");
int sum = input.nextInt();
if (sum % 2 == 0) {
System.out.println("实际的菱形的高为: "+(sum+1));
}else{
System.out.println("实际的菱形的高为: "+ sum);
}
for(int i = 1; i <= sum/2+1; i++){
for (int j = 0; j <= sum/2-i; j++){
System.out.print(" ");
}
for (int j = 1; j <= 2*i-1; j++){
System.out.print("*");
}
System.out.println();
}
for (int i = 1; i <= sum/2; i++) {
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
for (int j = 0; j < 2*((sum/2+1)-i)-1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
4、打印空心菱形
//由于菱形的打印条件限制,该代码也只能实现奇数行的菱形
import java.util.Scanner;
//打印空心菱形
public class Test04 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入菱形的高:");
int sum = input.nextInt();
if (sum % 2 == 0) {
System.out.println("实际的菱形的高为: "+(sum+1));
}else{
System.out.println("实际的菱形的高为: "+ sum);
}
for(int i = 1; i <= sum/2+1; i++){
for (int j = 0; j <= sum/2-i; j++){
System.out.print(" ");
}
for (int j = 1; j <= 2*i-1; j++){
if (j == 1 || j == 2*i-1) {
System.out.print("*");
}else{
System.out.print(" ");
}
}
System.out.println();
}
for (int i = 1; i <= sum/2; i++) {
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2*((sum/2+1)-i)-1; j++) {
if (j == 1 || j == 2*((sum/2+1)-i)-1) {
System.out.print("*");
}else{
System.out.print(" ");
}
}
System.out.println();
}
}
}
5、斐波那契数列
//斐波那契数列 1 1 2 3 5 8 13 21 34 55 89 144
public static void main(String[] args) {
int a = 1;
int b = 1;
System.out.print(a + " " + b + " ");
int i = 1;
while (i <= 10) {
int c = a + b;
System.out.print(c + " ");
a = b;
b = c;
i++;
}
}
6、打印杨辉三角形
public class TestYangHui {
//杨辉三角形
public static void main(String[] args) {
int[][] nums = new int[10][];
//给杨辉三角形数组赋值
for (int i = 0; i < nums.length; i++) {
nums[i] = new int[i+1];
for (int j = 0; j < nums[i].length; j++) {
if (j == 0 || j == nums[i].length - 1){
nums[i][j] = 1;
}else{
nums[i][j] = nums[i-1][j] + nums[i-1][j-1];
}
}
System.out.println();
}
//遍历杨辉三角形数组
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j <nums[i].length ; j++) {
System.out.print(nums[i][j]+"\t");
}
System.out.println();
}
}
}