用java做一个简单的售票系统(你好,请问一下java模拟5个窗口售票系统你是如何编写的,能教我一下吗)

本文目录
- 你好,请问一下java模拟5个窗口售票系统你是如何编写的,能教我一下吗
- 用java模拟铁路售票系统~实现四个售票点发售某日某次列车的100张车票(多线程问题)
- java火车票售票用到web吗
- java 多线程模拟火车售票系统问题
- Java编程 假设有火车票1000张,创建10个线程模拟10个售票点,每个售票点100毫秒售出一张票
- java简单的火车售票系统怎么输入行和列让指定坐标改变
你好,请问一下java模拟5个窗口售票系统你是如何编写的,能教我一下吗
package yaoshun.Thread;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
/*
* 多线程同步输出
*/
public class MultiThread extends JFrame {
private int ticket; // 计数变量
private JButton jButton;
private JLabel jLabel;
private JLabel jLabel1;
private JLabel jLabel2;
private JLabel jLabel3;
private JLabel jLabel4;
private JLabel jLabel5;
private JTextArea jTextArea1;
private JTextArea jTextArea2;
private JTextArea jTextArea3;
private JTextArea jTextArea4;
private JTextArea jTextArea5;
private Thread thread1;
private Thread thread2;
private Thread thread3;
private Thread thread4;
private Thread thread5;
// 构造器
public MultiThread() {
setName("火车票销售情况");
setLayout(null); // 使用绝对布局
setSize(680, 720);
setResizable(false); // 设置窗体大小不变
setLocationRelativeTo(null);// 设置窗体居中
jLabel = new JLabel("火车站售票大厅");
jLabel.setBounds(280, 20, 140, 30);
jLabel.setFont(new Font(getName(), Font.BOLD, 16)); // 设置字体大小
jLabel.setForeground(Color.BLUE); // 设置字体颜色
jLabel1 = new JLabel("售票窗1");
jLabel1.setBounds(72, 50, 80, 50);
jLabel1.setForeground(Color.red);
jLabel2 = new JLabel("售票窗2");
jLabel2.setBounds(192, 50, 80, 50);
jLabel2.setForeground(Color.red);
jLabel3 = new JLabel("售票窗3");
jLabel3.setBounds(312, 50, 80, 50);
jLabel3.setForeground(Color.red);
;
jLabel4 = new JLabel("售票窗4");
jLabel4.setBounds(432, 50, 80, 50);
jLabel4.setForeground(Color.red);
jLabel5 = new JLabel("售票窗5");
jLabel5.setBounds(552, 50, 80, 50);
jLabel5.setForeground(Color.red);
jTextArea1 = new JTextArea();
jTextArea1.setBounds(45, 100, 100, 500);
jTextArea1.setEditable(false); // 文本框不可
jTextArea2 = new JTextArea();
jTextArea2.setBounds(165, 100, 100, 500);
jTextArea2.setEditable(false); // 文本框不可
jTextArea3 = new JTextArea();
jTextArea3.setBounds(285, 100, 100, 500);
jTextArea3.setEditable(false); // 文本框不可
jTextArea4 = new JTextArea();
jTextArea4.setBounds(405, 100, 100, 500);
jTextArea4.setEditable(false); // 文本框不可
jTextArea5 = new JTextArea();
jTextArea5.setBounds(525, 100, 100, 500);
jTextArea5.setEditable(false); // 文本框不可
jButton = new JButton("开始售票");
jButton.setBounds(270, 625, 140, 40);
jButton.setFont(new Font(getName(), Font.BOLD, 16));// 设置字体
jButton.setForeground(Color.blue);
thread1 = new Thread(new MyThread1());
thread2 = new Thread(new MyThread2());
thread3 = new Thread(new MyThread3());
thread4 = new Thread(new MyThread4());
thread5 = new Thread(new MyThread5());
// 为按钮添加监听器
jButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
thread1.start();
thread2.start();
thread3.start();
thread4.start();
thread5.start();
}
});
add(jLabel);
add(jLabel1);
add(jLabel2);
add(jLabel3);
add(jLabel4);
add(jLabel5);
add(jTextArea1);
add(jTextArea2);
add(jTextArea3);
add(jTextArea4);
add(jTextArea5);
add(jButton);
setVisible(true); // 设置窗体显示
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置窗体关闭
}
class MyThread1 extends Thread {
// 重写run方法
public synchronized void run() {
while (ticket 《 100) {
++ticket;
jTextArea1.append("第" + ticket + "张票已售出" + "\n");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
}
class MyThread2 extends Thread {
// 重写run方法
public synchronized void run() {
while (ticket 《 100) {
++ticket;
jTextArea2.append("第" + ticket + "张票已售出" + "\n");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
}
class MyThread3 extends Thread {
// 重写run方法
public synchronized void run() {
while (ticket 《 100) {
++ticket;
jTextArea3.append("第" + ticket + "张票已售出" + "\n");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
}
class MyThread4 extends Thread {
// 重写run方法
public synchronized void run() {
while (ticket 《 100) {
++ticket;
jTextArea4.append("第" + ticket + "张票已售出" + "\n");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
}
class MyThread5 extends Thread {
// 重写run方法
public synchronized void run() {
while (ticket 《 100) {
++ticket;
jTextArea5.append("第" + ticket + "张票已售出" + "\n");
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
}
public static void main(String args) {
MultiThread multiThread = new MultiThread();
}
}
自己改一下 包名吧,这个题目是很多学校在多线程都用来的基础练习题。
用java模拟铁路售票系统~实现四个售票点发售某日某次列车的100张车票(多线程问题)
public class TicketsSystem
{
public static void main(String args)
{
SellThread st=new SellThread();
// new Thread(st).start();
try
{
Thread.sleep(1);
}
catch(Exception e)
{
e.printStackTrace();
}
st.b=true;
new Thread(st).start();
new Thread(st).start();
new Thread(st).start();
}
}
class SellThread implements Runnable
{
int tickets=100;
Object obj=new Object();
boolean b=false;
public void run()
{
if(b==false)
{
while(true)
sell();
}
else
{
while(true)
{
synchronized(obj)
{
try
{
Thread.sleep(10);
}
catch(Exception e)
{
e.printStackTrace();
}
synchronized(this)
{
if(tickets》0)
{
System.out.println("obj:"+Thread.currentThread().getName()+
" sell tickets:"+tickets);
tickets--;
}
}
}
}
}
}
public synchronized void sell()
{
synchronized(obj)
{
if(tickets》0)
{
try
{
Thread.sleep(10);
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.println("sell():"+Thread.currentThread().getName()+
" sell tickets:"+tickets);
tickets--;
}
}
}
}qq号:1369038795
java火车票售票用到web吗
用。用Java Web实现的火车票售票系统,其中实现了火车票售票系统该有的基础功能,是面向演示开发的课程设计。火车票,是乘客乘坐火车需出示的票据,主要由客票和附加票两部分构成。
java 多线程模拟火车售票系统问题
在synchronized(str) 里加一句 if(ticket 》 0) //判断是否有余票,没有余票还卖什么?
也就是这样:
package socket;
public class Test
{
public static void main(String args)
{
TestThread tt=new TestThread();
new Thread(tt).start();
new Thread(tt).start();
new Thread(tt).start();
new Thread(tt).start();
}
}
class TestThread implements Runnable
{
int ticket=10;
String str=" ";
public void run()
{
while(ticket》0)
{
synchronized(str)
{
if(ticket 》 0 ) {
System.out.println(Thread.currentThread().getName()+":ticket "+ticket+" is saling");
ticket--;
}
}
}
}
}
你的while循环只是超出票数的循环,在同步里面还要有判断是否有票的条件.因为里面同步的时候除了一个线程在执行外,其它几个都在等待,而此时并没有跳出循环,当然会出现负数了.
当然把while判断放到同步代码里面去也可以.不过这样就没有超出票数的判断了,实际应用中应该有个超出票数的判断.. 例如 :for(int i=0;i《100;i++) //同步检测 只有5张票,显示100张,实际上卖出的还是5张..用来检测同步是否正确...
Java编程 假设有火车票1000张,创建10个线程模拟10个售票点,每个售票点100毫秒售出一张票
package tmp;
public class Test {
private static int k=0;//售出票数
private static int count=1000;//总票数
private static synchronized boolean sell(int n){
if(k《count){
k++;
System.out.println("第"+n+"售票点售出第"+k+"张票");
return true;
}
return false;
}
static class SellPot extends Thread{//售票点
int n;//售票点编号
public SellPot(int n){
this.n=n;
}
public void run(){
while(sell(n)){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String args) {
for(int i=1;i《=10;i++){
new SellPot(i).start();
}
}
}
//银行汇款
public class Test2 {
private static int k=0;//总汇款
private static synchronized void send(int m,int i,int n){
k+=m;
System.out.println("第"+n+"个顾客进行第"+(i+1)+"次汇款,银行接收总汇款"+k);
}
static class Costum extends Thread{//售票点
int n;//顾客编号
public Costum(int n){
this.n=n;
}
public void run(){
for(int i=0;i《3;i++){
send(100,i,n);//汇款100
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String args) {
for(int i=1;i《=3;i++){
new Costum(i).start();
}
}
}
java简单的火车售票系统怎么输入行和列让指定坐标改变
首先数组piao1必须定义好值但含义,比如初始化后默认都是0,那么你可以认为0表示邮票,改变值为1后认为是无票即可。
另外,可以将其中一些方法提取出来使得代码更清晰
import java.util.Scanner;
public class Shoupiao {
public static void main(String args) {
System.out.println("\t简单的售票系统");
int piao1;
printPiao(piao1);// 打印票数据
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("输入坐标售票后回车,如: 1,1,退出轻按q,重置轻按r");
String zuobiaoStr = sc.nextLine();
if (zuobiaoStr.equals("q")) {
break;
}
if (zuobiaoStr.equals("r")) {
resetPiao(piao1);
printPiao(piao1);
continue;
}
String zuobiaoArray = zuobiaoStr.split(",");
if (zuobiaoArray.length == 2) {
try {
int x = Integer.parseInt(zuobiaoArray.trim());
int y = Integer.parseInt(zuobiaoArray.trim());
piao1 = 1;
printPiao(piao1);
} catch (Exception e) {
System.out.println("输入的坐标有误,请注意格式");
}
} else {
System.out.println("未能识别坐标或命令");
}
}
}
private static void resetPiao(int piao1) {
for (int i = 0; i 《 piao1.length; i++) {
for (int j = 0; j 《 piao1.length; j++) {
piao1 = 0;
}
}
}
private static void printPiao(int piao1) {
for (int i = 0; i 《 piao1.length; i++) {
for (int j = 0; j 《 piao1.length; j++) {
int piao = piao1;
if (piao == 0) {
System.out.print("有票\t");
} else {
System.out.print("已售\t");
}
}
System.out.println();
}
}
}

本文相关文章:
日本哪个浏览器可以在中国用(怎么在中文XP系统装日文浏览器(日本IE))
2026年9月24日 14:20
网站系统发生错误(我在百度搜我的网页,显示系统发生错误,是什么原因导致的)
2026年9月13日 14:10
web前端视频教程全套(求一份比较系统的web前端学习视频)
2026年8月13日 03:00
店铺首页设计模板(淘宝c店可以自己设计首页模板,不用官方的模板吗就是不用系统自带的首页模板,自己设计好,但是要怎)
2026年8月8日 14:30
javaweb动态网站开发(想要系统学习java到底要学习哪些知识)
2026年7月29日 00:50
更多文章:
跷二郎腿太低好吗?想要通过贴墙站改正二郎腿影响的话,有哪些要点需要注意
2026年10月11日 05:10
易语言点击js按钮(易语言网页填表怎样点击链接为“javascript:void(0)“的按钮)
2026年10月11日 03:00
compare with造句(用compared with和compared to造句)
2026年10月10日 23:30
tensorflow与keras对应版本(为什么tensorflow2.8没有keras)
2026年10月10日 22:10
maven仓库jar网站(如何在maven仓库中添加jar包)
2026年10月10日 19:50


