当前位置:首页 > java > 正文

java有趣的程序代码

  • java
  • 2024-04-25 18:04:11
  • 9888

java
public class BaseConverter {
private static final char[] DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F'};
public static String toBase(int number, int base) {
if (base < 2 || base > 16) {
throw new IllegalArgumentException("Base must be between 2 and 16");
}
StringBuilder result = new StringBuilder();
while (number > 0) {
result.insert(0, DIGITS[number % base]);
number /= base;
}
return result.toString();
}
public static void main(String[] args) {
System.out.println(toBase(100, 2)); // 1100100
System.out.println(toBase(100, 8)); // 144
System.out.println(toBase(100, 16)); // 64
}
}
质数生成器
java
public class PrimeGenerator {
public static List generatePrimes(int limit) {
List primes = new ArrayList<>();
boolean[] isPrime = new boolean[limit + 1];
for (int i = 2; i <= limit; i++) {
isPrime[i] = true;
}
for (int i = 2; i <= Math.sqrt(limit); i++) {
if (isPrime[i]) {
for (int j = i i; j <= limit; j += i) {
isPrime[j] = false;
}
}
}
for (int i = 2; i <= limit; i++) {
if (isPrime[i]) {
primes.add(i);
}
}
return primes;
}
public static void main(String[] args) {
List primes = generatePrimes(100);
System.out.println(primes); // [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
}
}
迷宫生成器
java
public class MazeGenerator {
private static final int NORTH = 0;
private static final int EAST = 1;
private static final int SOUTH = 2;
private static final int WEST = 3;
private int[][] maze;
private int width;
private int height;
public MazeGenerator(int width, int height) {
this.width = width;
this.height = height;
maze = new int[height][width];
}
public void generateMaze() {
Random random = new Random();
// Set all cells to visited
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
maze[i][j] = -1;
}
}
// Create the first cell as the starting point
maze[0][0] = NORTH;
// Loop until all cells are visited
while (hasUnvisitedCells()) {
int currentRow = random.nextInt(height);
int currentCol = random.nextInt(width);
if (maze[currentRow][currentCol] == -1) {
// Get a list of unvisited neighbors
List neighbors = getUnvisitedNeighbors(currentRow, currentCol);
if (!neighbors.isEmpty()) {
// Choose a random neighbor
int neighbor = neighbors.get(random.nextInt(neighbors.size()));
// Remove the wall between the current cell and the neighbor
switch (neighbor) {
case NORTH:
maze[currentRow - 1][currentCol] = SOUTH;
break;
case EAST:
maze[currentRow][currentCol + 1] = WEST;
break;
case SOUTH:
maze[currentRow + 1][currentCol] = NORTH;
break;
case WEST:
maze[currentRow][currentCol - 1] = EAST;
break;
}
// Mark the neighbor as visited
maze[currentRow][currentCol] = neighbor;
}
}
}
}
public boolean hasUnvisitedCells() {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (maze[i][j] == -1) {
return true;
}
}
}
return false;
}
public List getUnvisitedNeighbors(int row, int col) {
List neighbors = new ArrayList<>();
if (row > 0 && maze[row - 1][col] == -1) {
neighbors.add(NORTH);
}
if (col < width - 1 && maze[row][col + 1] == -1) {
neighbors.add(EAST);
}
if (row < height - 1 && maze[row + 1][col] == -1) {
neighbors.add(SOUTH);
}
if (col > 0 && maze[row][col - 1] == -1) {
neighbors.add(WEST);
}
return neighbors;
}
public void printMaze() {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
switch (maze[i][j]) {
case NORTH:
System.out.print("+---+");
break;
case EAST:
System.out.print("+ +");
break;
case SOUTH:
System.out.print("+---+");
break;
case WEST:
System.out.print("+ +");
break;
default:
System.out.print(" ");
}
}
System.out.println();
}
}
public static void main(String[] args) {
MazeGenerator mazeGenerator = new MazeGenerator(10, 10);
mazeGenerator.generateMaze();
mazeGenerator.printMaze();
}
}
递归汉诺塔
java
public class TowersOfHanoi {
public static void solve(int n, char from, char to, char aux) {
if (n == 1) {
System.out.println("Move disk 1 from " + from + " to " + to);
return;
}
solve(n - 1, from, aux, to);
System.out.println("Move disk " + n + " from " + from + " to " + to);
solve(n - 1, aux, to, from);
}
public static void main(String[] args) {
solve(3, 'A', 'B', 'C');
}
}
分形树
java
public class FractalTree {
private static final double ANGLE = Math.PI / 6;
public static void draw(Turtle turtle, int depth) {
if (depth <= 0) {
return;
}
turtle.forward(5);
turtle.turnLeft(ANGLE);
draw(turtle, depth - 1);
turtle.turnRight(2 ANGLE);
draw(turtle, depth - 1);
turtle.turnLeft(ANGLE);
turtle.backward(5);
}
public static void main(String[] args) {
Turtle turtle = new Turtle();
turtle.setup(800, 600);
turtle.hide();
turtle.penup();
turtle.goto(400, 100);
turtle.pendown();
draw(turtle, 10);
}
}