简单的java小程序
时间: 2024-02-08 20:04:41 浏览: 29
好的,以下是一个简单的Java小程序,可以生成随机数并判断是否为奇数:
```java
import java.util.Random;
public class RandomNumber {
public static void main(String[] args) {
Random r = new Random();
int num = r.nextInt(100);
System.out.println("生成的随机数为:" + num);
if (num % 2 == 0) {
System.out.println("这是一个偶数");
} else {
System.out.println("这是一个奇数");
}
}
}
```
该程序使用了Java的`Random`类生成一个0~99之间的随机整数,并使用条件语句判断是否为奇数。最终输出结果到控制台。可以通过修改代码来实现其他功能,例如生成多个随机数并求和、生成随机字符串等。
相关问题
java简单表白小程序
Java简单表白小程序可以按照以下步骤进行实现:
1. 创建一个Java类作为程序的入口点。
2. 在该类中引入JavaFX库来实现图形化界面。
3. 创建一个窗口并设置其大小、标题等属性。
4. 在窗口中添加文本框、按钮等组件,用于输入表白的内容和触发发送表白的操作。
5. 设置按钮的监听事件,在按钮被点击时执行相应的操作。
6. 在事件处理方法中获取文本框中输入的表白内容。
7. 利用Java的Socket编程功能,将表白内容发送给指定的接收方。
8. 将发送成功的消息显示在界面上。
具体实现的过程如下:
```java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
public class LoveProgram extends Application {
private TextField textField;
private Button sendButton;
@Override
public void start(Stage primaryStage) {
textField = new TextField();
sendButton = new Button("发送");
sendButton.setOnAction(event -> sendLove());
VBox vbox = new VBox(10, textField, sendButton);
Scene scene = new Scene(vbox, 300, 200);
primaryStage.setTitle("表白小程序");
primaryStage.setScene(scene);
primaryStage.show();
}
private void sendLove() {
String loveContent = textField.getText();
try {
Socket socket = new Socket("接收方IP", 8888);
OutputStream outputStream = socket.getOutputStream();
outputStream.write(loveContent.getBytes(StandardCharsets.UTF_8));
outputStream.flush();
outputStream.close();
textField.setText("发送成功!");
} catch (IOException e) {
e.printStackTrace();
textField.setText("发送失败!");
}
}
public static void main(String[] args) {
launch(args);
}
}
```
这样,我们就实现了一个简单的Java表白小程序。当点击发送按钮时,程序会将文本框中的内容发送给指定的接收方IP(需根据实际情况更改),并在界面上显示发送结果。
java小程序五子棋
以下是一个简单的Java小程序五子棋的设计思路和代码实现:
1. 设计思路:
- 启动五子棋游戏,调用一个方法进行初始化棋盘。
- 打印出五子棋棋盘。
- 黑白双方交替下棋,输入坐标。
- 判断坐标是否合法,判断是否重复。
- 判断输赢。
2. 代码实现:
```java
public class GobangGame {
private int[][] board; // 二维数组表示棋盘
private int currentPlayer; // 当前玩家,1代表黑棋,2代表白棋
public GobangGame() {
board = new int[15][15]; // 初始化棋盘为15x15的二维数组
currentPlayer = 1; // 黑棋先下
}
public void printBoard() {
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
if (board[i][j] == 0) {
System.out.print("- "); // 未落子的位置用"-"表示
} else if (board[i][j] == 1) {
System.out.print("● "); // 黑棋用"●"表示
} else if (board[i][j] == 2) {
System.out.print("○ "); // 白棋用"○"表示
}
}
System.out.println();
}
}
public boolean isValidMove(int row, int col) {
if (row >= 0 && row < 15 && col >= 0 && col < 15 && board[row][col] == 0) {
return true; // 坐标合法且未落子
} else {
return false;
}
}
public boolean isWin(int row, int col) {
int count = 1; // 连续相同棋子的数量
int player = board[row][col]; // 当前玩家的棋子
// 判断水平方向
for (int i = col - 1; i >= 0; i--) {
if (board[row][i] == player) {
count++;
} else {
break;
}
}
for (int i = col + 1; i < 15; i++) {
if (board[row][i] == player) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
// 判断垂直方向
count = 1;
for (int i = row - 1; i >= 0; i--) {
if (board[i][col] == player) {
count++;
} else {
break;
}
}
for (int i = row + 1; i < 15; i++) {
if (board[i][col] == player) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
// 判断左上到右下方向
count = 1;
for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
if (board[i][j] == player) {
count++;
} else {
break;
}
}
for (int i = row + 1, j = col + 1; i < 15 && j < 15; i++, j++) {
if (board[i][j] == player) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
// 判断右上到左下方向
count = 1;
for (int i = row - 1, j = col + 1; i >= 0 && j < 15; i--, j++) {
if (board[i][j] == player) {
count++;
} else {
break;
}
}
for (int i = row + 1, j = col - 1; i < 15 && j >= 0; i++, j--) {
if (board[i][j] == player) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
return false;
}
public void play(int row, int col) {
if (isValidMove(row, col)) {
board[row][col] = currentPlayer;
if (isWin(row, col)) {
System.out.println("玩家 " + currentPlayer + " 获胜!");
} else {
currentPlayer = 3 - currentPlayer; // 切换玩家
}
} else {
System.out.println("无效的落子位置!");
}
}
public static void main(String[] args) {
GobangGame game = new GobangGame();
game.printBoard();
// 示例下棋过程
game.play(7, 7); // 黑棋下在中间位置
game.printBoard();
game.play(7, 6); // 白棋下在中间左边位置
game.printBoard();
game.play(8, 7); // 黑棋下在中间下边位置
game.printBoard();
game.play(6, 6); // 白棋下在中间左上位置
game.printBoard();
game.play(8, 8); // 黑棋下在中间右下位置
game.printBoard();
}
}
```