当前位置: 代码迷 >> Java相关 >> java多线程找零问题,我想知道我的程序运行后为什么得不到正确的结果?
  详细解决方案

java多线程找零问题,我想知道我的程序运行后为什么得不到正确的结果?

热度:308   发布时间:2013-05-19 19:34:35.0
java多线程找零问题,我想知道我的程序运行后为什么得不到正确的结果?
在线求高手解决:

package Exercise2;

import java.awt.*;
import javax.swing.*;

public class ThreadTest_02 {
    static SalesLady saleslady = new SalesLady(14, 0, 0, 0);

    public static void main(String[] args) {
        new ThreadTest_02().start();
    }

    public void start() {
        int moneies[] = { 10, 20, 5, 10, 5, 10, 5, 50, 100, 10, 5, 10, 20, 5,
                10, 5 };
        Thread[] aThreadArray = new Thread[moneies.length];
        System.out.println("现在开始售票:");
        for (int i = 0; i < moneies.length; i++) {
            aThreadArray[i] = new Thread(new CustomerClass(i + 1, moneies[i]));
            aThreadArray[i].start();
        }
        for (int i = 0; i < moneies.length; i++) {
            try {
                aThreadArray[i].join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("票已售完");
    }
}

// 顾客 销售人员
class SalesLady {
    int memontoes, five, ten, twenty, fifty, hundred;
    String s = null;

    SalesLady(int m, int f, int t, int ty) {
        memontoes = m;
        five = f;
        ten = t;
        twenty = ty;
    }

    public synchronized String ruleForSale(int num, int money) {        
        if (memontoes <= 0)
            return s = "对不起,已经售完";
        if (money == 5) {            
            memontoes--;
            five++;        
            s = "给你票,你的钱正好。";
            notify();
        } else if (money == 10) {
            if (five >= 1) {
                memontoes--;
                five -= 1;
                ten++;
                s = "给你票,找你5元。";
            } else {
                s = num + "号顾客用10元购票,请等待";
//                notify();
                try {                    
                    wait();
                } catch (InterruptedException e) {
                }
            }
            notify();

        } else
            if (money == 20) {
        
            
            twentyGive();
            notify();
            
        } else if (money == 50) {
//            notify();
            fiftyGive();
//            notify();
        } else if(money == 100) {
            hundredGive();
            notify();
        }
        notifyAll();
        return s;            
    }

    public void twentyGive() {
        if (ten >= 1 && five >= 1) {
            s = "找您1个10元1个5元!";
            ten--;
            five--;
            twenty++;
            memontoes--;
        } else if (ten < 1 && five >= 3) {
            s = "给您球票,找您3个5元,请在球赛开始前进影院观看精彩比赛!";
            five -= 3;
            memontoes--;
            twenty++;
        } else {
            s = ": 不好意思,暂时没有零钱找,请稍等,!";
            try {
                wait();
            } catch (InterruptedException e) {
                s = " 不好意思,暂时没有零钱找,请稍等,!";
            }
        }
//        return ;

    }

    public void fiftyGive() {
        if (twenty >= 2 && five >= 1) {
            s = ": 给您球票,找您2个20元和1个5元,请在球赛开始前进影院观看精彩比赛!\n";
            five--;
            twenty -= 2;
            fifty++;
            memontoes--;
        } else if (twenty >= 1 && ten >= 2 && five >= 1) {
            s = ": 给您球票,找您1个20元2个10元和1个5元,请在球赛开始前进影院观看精彩比赛!\n";
            five--;
            twenty--;
            ten -= 2;
            fifty++;
            memontoes--;
        } else if (twenty < 1 && ten >= 4 && five >= 1) {
            s = ": 给您球票,找您45元钱,请在球赛开始前进影院观看精彩比赛!\n";
            five--;
            ten -= 4;
            fifty++;
            memontoes--;
        } else if (twenty < 1 && ten >= 3 && five >= 3) {
            s = ": 给您球票,找您45元钱,请在球赛开始前进影院观看精彩比赛!\n";
            five -= 3;
            ten -= 3;
            fifty++;
            memontoes--;
        } else if (twenty < 1 && ten >= 2 && five >= 5) {
            s = ": 给您球票,找您45元钱,请在球赛开始前进影院观看精彩比赛!\n";
            five -= 5;
            ten -= 2;
            fifty++;
            memontoes--;
        } else if (twenty < 1 && ten >= 1 && five >= 7) {
            s = ": 给您球票,找您45元钱,请在球赛开始前进影院观看精彩比赛!\n";
            five -= 7;
            ten--;
            fifty++;
            memontoes--;
        } else if (twenty < 1 && ten < 1 && five >= 9) {
            s = ": 给您球票,找您45元钱,请在球赛开始前进影院观看精彩比赛!\n";
            five -= 9;
            fifty++;
            memontoes--;
        } else {
            s = ": 不好意思,暂时没有零钱找,请稍等,!";
            try {
                wait();
            } catch (InterruptedException e) {
                System.out.println("等待状态!");
            }

        }
        notify();

    }

    /*
     * 100元找钱方法,使用最有方法找零钱
     */
    public void hundredGive() {
        if (fifty >= 1 && twenty >= 2 && five >= 1) {
            
            fifty--;
            twenty -= 2;
            five--;
            hundred++;
            memontoes--;
            s = ": 给您球票,找您95元钱,请在球赛开始前进影院观看精彩比赛!\n";
        } else if (fifty < 1 && twenty >= 4 && five >= 1) {
            
            five--;
            twenty -= 4;
            ten -= 4;
            hundred++;
            memontoes--;
            s = ": 给您球票,找您95元钱,请在球赛开始前进影院观看精彩比赛!\n";
        } else if (fifty < 1 && twenty >= 3 && ten >= 3 && five >= 1) {
            
            five--;
            ten -= 3;
            twenty -= 3;
            hundred++;
            memontoes--;
            s = ": 给您球票,找您95元钱,请在球赛开始前进影院观看精彩比赛!\n";
        } else if (fifty < 1 && twenty >= 2 && ten >= 5 && five >= 1) {
            
            five -= 1;
            ten -= 5;
            fifty++;
            memontoes--;
            s = ": 给您球票,找您95元钱,请在球赛开始前进影院观看精彩比赛!\n";
        } else if (fifty < 1 && twenty >= 1 && ten >= 7 && five >= 5) {
            
            five -= 5;
            ten -= 7;
            fifty++;
            memontoes--;
            s = ": 给您球票,找您95元钱,请在球赛开始前进影院观看精彩比赛!\n";
        } else {
            s = ": 不好意思,暂时没有零钱找,请稍等,!\n";
            try {
                wait();
            } catch (InterruptedException e) {
                System.out.println("等待状态!");
            }
        }
        notify();

    }
}

// 顾客
class CustomerClass implements Runnable {
    int num, money;

    public void run() {
        System.out.println("我是" + num + "号顾客,用" + money + "元购票\n售票员说:"
                + ThreadTest_02.saleslady.ruleForSale(num, money));
    }

    /*
     * 顾客编号及给的钱的数量
     */
    CustomerClass(int n, int m) {
        num = n;
        money = m;
    }
}
搜索更多相关的解决方案: static  package  public  import  

----------------解决方案--------------------------------------------------------
建议不要用线程组了,是不是看的教材太老了。
----------------解决方案--------------------------------------------------------
请楼上的高手给出解决办法,感激不尽…………
----------------解决方案--------------------------------------------------------
程序很乱,看不明白,先提出你的程序功能需求,
----------------解决方案--------------------------------------------------------
使用图形化界面模拟多人排队买车票,假设车票5元一张,购票者有持5元、10元、20元、50元、100元的。已知排队者持钱情况序列,使用多线程模拟购票。
----------------解决方案--------------------------------------------------------
一楼真实高手,一眼就能看出楼主的教材过老,佩服佩服
----------------解决方案--------------------------------------------------------
  相关解决方案