Java如何判断输入时间与当前系统时间的大小,
时间: 2024-06-04 15:10:25 浏览: 8
Java中可以通过使用Java.util.Date类和Java.util.Calendar类来判断输入时间与当前系统时间的大小。首先获取当前系统时间,然后获取输入时间,将它们转换为Date对象,最后使用Date类中的compareTo()方法来比较两个日期的大小。如果输入时间大于当前系统时间,则返回1,如果输入时间小于当前系统时间,则返回-1,如果两个时间相同,则返回0。
相关问题
用java写一个时间片轮转算法
好的,以下是用 Java 实现时间片轮转算法的代码示例:
```java
import java.util.*;
public class RoundRobin {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 输入进程数
System.out.println("请输入进程数:");
int n = sc.nextInt();
// 输入进程的到达时间和需要的运行时间
int[] arriveTime = new int[n];
int[] runTime = new int[n];
for (int i = 0; i < n; i++) {
System.out.println("请输入第" + (i + 1) + "个进程的到达时间和需要的运行时间:");
arriveTime[i] = sc.nextInt();
runTime[i] = sc.nextInt();
}
// 输入时间片大小
System.out.println("请输入时间片大小:");
int timeSlice = sc.nextInt();
// 初始化等待队列和完成队列
Queue<Integer> waitQueue = new LinkedList<>();
List<Integer> finishList = new ArrayList<>();
// 初始化当前时间和当前进程
int curTime = 0;
int curProcess = -1;
// 时间片轮转算法
while (!waitQueue.isEmpty() || curProcess != -1) {
// 将到达时间小于等于当前时间的进程加入等待队列
for (int i = 0; i < n; i++) {
if (arriveTime[i] <= curTime && !finishList.contains(i)) {
waitQueue.offer(i);
}
}
// 如果当前没有进程在运行,则取出等待队列中的第一个进程
if (curProcess == -1) {
curProcess = waitQueue.poll();
}
// 运行当前进程
runTime[curProcess] -= timeSlice;
curTime += timeSlice;
// 如果当前进程已经运行完毕,则加入完成队列
if (runTime[curProcess] <= 0) {
finishList.add(curProcess);
curProcess = -1;
}
// 如果当前进程还未运行完毕,则放回等待队列末尾
if (curProcess != -1) {
waitQueue.offer(curProcess);
curProcess = -1;
}
}
// 输出每个进程的完成时间
System.out.println("每个进程的完成时间分别为:");
for (int i = 0; i < n; i++) {
int finishTime = arriveTime[i];
for (int j = 0; j < finishList.size(); j++) {
if (finishList.get(j) == i) {
finishTime += (j + 1) * timeSlice;
break;
}
}
System.out.println("进程" + (i + 1) + ":" + finishTime);
}
}
}
```
注意:以上代码仅为示例,实际应用中可能需要进行更多的输入检查和错误处理。
用JAVA编写 GUI 界面,模拟抢红包游戏,在界面输入红包大小和抢红包人姓名(用逗号隔开),输出系统当前时间以及每个人抢的红包大小,要求每个人抢的红包最少不能少于0.01元。
好的,下面是用 Java 编写的实现:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
public class RedPacketGame extends JFrame implements ActionListener {
private JLabel labelAmount, labelName;
private JTextField textAmount, textName;
private JButton buttonStart;
private JTextArea textOutput;
public RedPacketGame() {
setTitle("抢红包游戏");
setSize(400, 400);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建输入红包大小的Label和TextField
labelAmount = new JLabel("红包大小:");
textAmount = new JTextField(10);
// 创建输入抢红包人姓名的Label和TextField
labelName = new JLabel("抢红包人姓名:");
textName = new JTextField(20);
// 创建"开始"按钮
buttonStart = new JButton("开始");
buttonStart.addActionListener(this);
// 创建输出信息的TextArea
textOutput = new JTextArea(10, 30);
textOutput.setEditable(false);
// 将组件添加到窗口中
JPanel panel = new JPanel(new GridLayout(4, 1));
panel.add(labelAmount);
panel.add(textAmount);
panel.add(labelName);
panel.add(textName);
panel.add(buttonStart);
panel.add(new JScrollPane(textOutput));
add(panel);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == buttonStart) {
// 获取输入的红包大小和抢红包人姓名
double amount = Double.parseDouble(textAmount.getText());
String[] names = textName.getText().split(",");
// 判断红包大小是否足够
if (amount < names.length * 0.01) {
textOutput.append("红包大小不足!\n");
return;
}
// 计算每个人抢到的红包大小
double[] redPackets = new double[names.length];
for (int i = 0; i < names.length; i++) {
if (i == names.length - 1) {
redPackets[i] = amount - sum(redPackets);
} else {
redPackets[i] = Math.round((amount / names.length) * 100) / 100.0;
}
}
// 输出系统当前时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String current_time = sdf.format(new Date());
textOutput.append("系统当前时间:" + current_time + "\n");
// 输出每个人抢到的红包大小
for (int i = 0; i < names.length; i++) {
textOutput.append(names[i] + " 抢到了 " + String.format("%.2f", redPackets[i]) + " 元。\n");
}
}
}
private double sum(double[] arr) {
double sum = 0.0;
for (double d : arr) {
sum += d;
}
return sum;
}
public static void main(String[] args) {
RedPacketGame game = new RedPacketGame();
game.setVisible(true);
}
}
```
这个界面有两个输入框,一个是红包大小,一个是抢红包人姓名,用逗号隔开。当你点击"开始"按钮之后,程序会输出系统当前时间以及每个人抢到的红包大小。注意,每个人抢到的红包大小最少不能少于0.01元。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)