Java经典小案例(不定时更新)

人类除了擅长颓废,做什么都不对


1. 实现金字塔效果

这里写图片描述 这里写图片描述

import java.util.Scanner;

/**
 * @author caojiantao-ext 根据输入的数目输出金字塔
 */
public class _01 {

    public static void main(String[] args) {
        System.out.print("请输入金字塔层数:");
        Scanner scanner = new Scanner(System.in);
        int count = scanner.nextInt();
        System.out.print("1:金字塔;2:倒金字塔:");
        int mode = scanner.nextInt();
        switch (mode) {
        case 1:
            function_1(count);
            break;
        case 2:
            function_2(count);
            break;
        default:
            System.out.println("输入有误!");
            break;
        }
        scanner.close();
    }

    // 金字塔
    public static void function_1(int count) {
        for (int i = 0; i < count; i++) {
            for (int space = count - i - 1; space > 0; space--) {
                System.out.print("   ");
            }
            for (int star = 0; star < (2 * i + 1); star++) {
                System.out.print("☆");
            }
            System.out.println();
        }
    }

    // 倒金字塔
    public static void function_2(int count) {
        for (int i = 0; i < count; i++) {
            for (int space = 0; space < i; space++) {
                System.out.print("    ");
            }
            for (int star = 0; star < (2 * (count - i) - 1); star++) {
                System.out.print("☆");
            }
            System.out.println();
        }
    }
}

2. 显示九九乘法表

这里写图片描述

/**
 * @author caojiantao-ext 九九乘法表
 */
public class _02 {

    public static void main(String[]args){
        for (int row = 1; row < 10; row++) {
            for (int column = 1; column <= row; column++) {
                System.out.print(row+"*"+column+"="+row*column);
                System.out.print("    ");
            }
            System.out.println();
        }
    }
}

3.绘余弦正弦函数

这里写图片描述

import java.applet.*;
import java.awt.Graphics;

/**
 * @author caojiantao-ext 画余弦函数
 */
public class _03 extends Applet {

    /**
     * 重写resize函数,改变applet窗体大小
     */
    @Override
    public void resize(int arg0, int arg1) {
        // TODO Auto-generated method stub
        super.resize(800, 600);
    }

    /**
     * 重写paint,画点成线,特别注意比例的转换(自己计算)
     */
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        // 画x轴
        g.drawLine(50, 300, 750, 300);
        // 画y轴
        g.drawLine(400, 50, 400, 550);
        // 画坐标轴箭头
        g.drawLine(735, 290, 750, 300);
        g.drawLine(735, 310, 750, 300);
        g.drawLine(390, 65, 400, 50);
        g.drawLine(410, 65, 400, 50);
        // 计算缩放比例
        double d = 150.0 / (Math.PI);
        // 画cos曲线
        int x, y;
        for (x = 100; x <= 700; x++) {
            // 特别注意这里的sin(PI)不等于0,浮点通病
            double a = Math.sin((x - 400) / d);
            // 绘余弦函数
            // double a = Math.cos((x - 400) / d);
            y = (int) (a * d);
            // 注意坐标转换
            g.drawString("·", x, 300 - y);
        }
    }
}

4.绘奥运五环

这里写图片描述

import java.applet.Applet;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

/**
 * @author caojiantao-ext 画奥运五环
 */
public class _04 extends Applet {

    @Override
    public void resize(int width, int height) {
        // TODO Auto-generated method stub
        super.resize(400, 300);
    }

    @Override
    public void paint(Graphics g) {
        // TODO Auto-generated method stub
        super.paint(g);
        // clr[]存储颜色
        Color clr[] = { Color.blue, Color.black, Color.red, Color.yellow, Color.green };
        // x[]存储起始x坐标
        int[] x = { 85, 165, 245, 125, 205 };
        // y[]存储起始y坐标
        int[] y = { 70, 70, 70, 135, 135 };
        // 设置画笔的粗细
        ((Graphics2D) g).setStroke(new BasicStroke(5.0f));
        for (int loop = 0; loop < clr.length; loop++) {
            g.setColor(clr[loop]);
            // 注意x,y是外切矩形的起始坐标,假设半径为90
            g.drawOval(x[loop], y[loop], 90, 90);
        }
    }
}

4.输出杨辉三角

这里写图片描述

import java.util.Scanner;

/**
 * @author caojiantao-ext 杨辉三角
 */
public class _05 {

    // 写内层循环时注意控制范围
    public static void main(String[] args) {
        System.out.print("请输入杨辉三角的行数:");
        Scanner scanner = new Scanner(System.in);
        int count = scanner.nextInt();
        int num[][] = new int[count][count];
        // 预防数组越界,初始化首列的值
        for (int loop = 0; loop < count; loop++) {
            num[loop][0] = 1;
        }
        // 根据杨辉三角的规则,给二维数组赋值处理
        for (int row = 1; row < count; row++) {
            for (int column = 1; column <= row; column++) {
                num[row][column] = num[row - 1][column] + num[row - 1][column - 1];
            }
        }
        // 循环打印
        for (int row = 0; row < count; row++) {
            for (int i = 0; i < 2 * (count - row) - 1; i++) {
                System.out.print(" ");
            }
            for (int column = 0; column <= row; column++) {
                // 输出控制,完美等腰
                System.out.printf("%-4d", num[row][column]);
            }
            System.out.println();
        }
        scanner.close();
    }
}

6.绘画国际棋盘

这里写图片描述

import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JLabel;

/**
 * @author caojiantao-ext 用窗体展现国际象棋的棋盘
 */
public class _06 {

    public static void main(String[] args) {
        JFrame frame = new JFrame("国际象棋");
        frame.setSize(415, 435);
        // 假设棋盘为20*20
        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 20; j++) {
                JLabel label = new JLabel();
                label.setSize(20, 20);
                label.setBackground(((i + j) % 2 == 0) ? Color.BLACK
                        : Color.white);
                label.setLocation(20 * i, 20 * j);
                // 设置不透明,要不然不会显示
                label.setOpaque(true);
                frame.add(label);
            }
        }
        // 窗体居中显示
        frame.setLocationRelativeTo(null);
        // 当窗口关闭程序结束
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 展示棋盘
        frame.setVisible(true);
    }
}

7.输出字符串排序(去掉重复项)

这里写图片描述

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class PermutationTest{

    private static Set<String> set = new HashSet<String>();

    public static void main(String[] args) throws Exception {
        Scanner scanner = new Scanner(System.in);
        char[] chars = scanner.next().toCharArray();
        permutation(chars, 0);
        System.out.print(set);
    }

    /**
     * 数组元素交换,地址引用传递
     */
    static void swap(char[] arr, int idx1, int idx2) {
        char temp = arr[idx1];
        arr[idx1] = arr[idx2];
        arr[idx2] = temp;
    }

    /**
     * 递归排序
     */
    static void permutation(char[] chars, int index) {
        // 递归出口
        if (index == chars.length) {
            // 添加到set中,避免重复排序
            set.add(new String(chars));
        } else {
            for (int i = index; i < chars.length; i++) {
                // 交换数组元素
                swap(chars, i, index);
                // 索引向后移一位,递归开始
                permutation(chars, index + 1);
                // 在每一次递归完成的时候记得还原数组元素,保证最外层循环的数据源不变
                swap(chars, i, index);
            }
        }
    }
}

  • 34
    点赞
  • 198
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值