编译一个Java中random类的简单程序
时间: 2024-05-09 11:16:58 浏览: 10
好的,我可以帮您编写一个Java中使用random类的简单程序,以下是代码:
```java
import java.util.Random;
public class RandomExample {
public static void main(String[] args) {
Random random = new Random();
int num = random.nextInt(100);
System.out.println("生成的随机数为:" + num);
}
}
```
这段代码会生成一个0到99之间的随机数,并输出结果。请问还有其他问题吗?
相关问题
java编译一个扫雷游戏程序
以下是一个简单的Java扫雷游戏程序的代码示例:
```java
import java.util.Random;
import java.util.Scanner;
public class Minesweeper {
private char[][] board;
private boolean[][] revealed;
private int rows, cols;
private int remaining;
public Minesweeper(int rows, int cols, int mines) {
this.rows = rows;
this.cols = cols;
this.board = new char[rows][cols];
this.revealed = new boolean[rows][cols];
this.remaining = rows * cols - mines;
// initialize board with empty cells
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
board[i][j] = '.';
revealed[i][j] = false;
}
}
// randomly place mines on the board
Random random = new Random();
int count = 0;
while (count < mines) {
int i = random.nextInt(rows);
int j = random.nextInt(cols);
if (board[i][j] != '*') {
board[i][j] = '*';
count++;
}
}
}
public void play() {
Scanner scanner = new Scanner(System.in);
while (true) {
// print board
System.out.println(getBoardString());
// get user input
System.out.print("Enter row and column (e.g. 3 4): ");
int row = scanner.nextInt() - 1;
int col = scanner.nextInt() - 1;
// check if cell is valid and not already revealed
if (row < 0 || row >= rows || col < 0 || col >= cols) {
System.out.println("Invalid cell, try again.");
continue;
}
if (revealed[row][col]) {
System.out.println("Cell already revealed, try again.");
continue;
}
// reveal cell and update remaining count
revealed[row][col] = true;
if (board[row][col] == '*') {
System.out.println("Boom! Game over.");
return;
} else {
remaining--;
if (remaining == 0) {
System.out.println("Congratulations! You win!");
return;
}
}
// update adjacent cells
if (board[row][col] == '.') {
updateAdjacentCells(row, col);
}
}
}
private void updateAdjacentCells(int row, int col) {
int[][] directions = {
{-1, -1}, {-1, 0}, {-1, 1},
{0, -1}, {0, 1},
{1, -1}, {1, 0}, {1, 1}
};
for (int[] dir : directions) {
int r = row + dir[0];
int c = col + dir[1];
if (r >= 0 && r < rows && c >= 0 && c < cols && board[r][c] == '*') {
board[row][col]++;
}
}
}
private String getBoardString() {
StringBuilder sb = new StringBuilder();
sb.append(" ");
for (int j = 0; j < cols; j++) {
sb.append(j + 1);
}
sb.append("\n");
for (int i = 0; i < rows; i++) {
sb.append((char) ('A' + i)).append(" ");
for (int j = 0; j < cols; j++) {
if (revealed[i][j]) {
sb.append(board[i][j]);
} else {
sb.append(".");
}
}
sb.append("\n");
}
return sb.toString();
}
public static void main(String[] args) {
Minesweeper game = new Minesweeper(8, 8, 10);
game.play();
}
}
```
这个程序使用了一个二维字符数组来表示扫雷的棋盘,其中`.`表示未翻开的空格,`*`表示地雷,数字表示该格周围地雷的数量。使用一个二维布尔数组来表示哪些格子已经被翻开了。程序会随机放置指定数量的地雷,然后让用户逐步翻开格子,直到所有非地雷格子都被翻开为止。如果翻开了地雷格子,游戏结束。如果所有非地雷格子都被翻开了,用户获胜。
用Java做一个星空程序
当使用Java来实现一个星空程序时,你可以借助Java的图形库来绘制星空效果。下面是一个简单的示例代码,展示了如何使用Java Swing库来创建一个绘制星空的程序:
```java
import javax.swing.*;
import java.awt.*;
public class StarrySky extends JFrame {
private static final int WIDTH = 800; // 窗口宽度
private static final int HEIGHT = 600; // 窗口高度
private Star[] stars; // 星星数组
public StarrySky() {
setTitle("Starry Sky");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
// 初始化星星数组
stars = new Star[100];
for (int i = 0; i < stars.length; i++) {
int x = (int) (Math.random() * WIDTH);
int y = (int) (Math.random() * HEIGHT);
int size = (int) (Math.random() * 5) + 1;
int speed = (int) (Math.random() * 3) + 1;
stars[i] = new Star(x, y, size, speed);
}
}
@Override
public void paint(Graphics g) {
super.paint(g);
// 绘制背景
g.setColor(Color.BLACK);
g.fillRect(0, 0, WIDTH, HEIGHT);
// 绘制星星
g.setColor(Color.WHITE);
for (Star star : stars) {
g.fillOval(star.getX(), star.getY(), star.getSize(), star.getSize());
}
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
StarrySky starrySky = new StarrySky();
starrySky.setVisible(true);
// 更新星星位置
while (true) {
for (Star star : starrySky.stars) {
star.updatePosition();
}
starrySky.repaint();
try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
}
class Star {
private int x; // x坐标
private int y; // y坐标
private int size; // 大小
private int speed; // 移动速度
public Star(int x, int y, int size, int speed) {
this.x = x;
this.y = y;
this.size = size;
this.speed = speed;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getSize() {
return size;
}
public void updatePosition() {
y += speed;
if (y > StarrySky.HEIGHT) { // 如果超出窗口范围,则重新从顶部开始
y = 0;
x = (int) (Math.random() * StarrySky.WIDTH);
}
}
}
```
这个程序使用Java的Swing库创建了一个窗口,并通过继承JFrame类来实现。在窗口中绘制星空效果,首先初始化了一些随机位置和大小的星星对象,并在`paint`方法中使用`Graphics`对象绘制这些星星。然后,通过不断更新星星的位置和重绘窗口来实现星星的移动效果。
你可以将上述代码保存为`StarrySky.java`文件,并使用Java编译器进行编译,然后运行该程序,即可看到一个简单的星空效果。