会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132358个问题
Python 全系列/第一阶段:Python入门/Python入门(动画版) 38568楼
Python 全系列/第一阶段:Python入门/Python入门(动画版) 38569楼
JAVA 全系列/第一阶段:JAVA 快速入门/变量、数据类型、运算符 38570楼
Python 全系列/第一阶段:Python入门/Python入门(动画版) 38571楼

import threading
import time
from queue import Queue
class Pro(threading.Thread):
    def run(self):
        global qu
        count=0
        while True:
            if qu.qsize()<1000:
                for i in range(100):
                    count+=1
                    msg="生成产品"+str(count)
                    qu.put(msg)
                    print(msg)
            time.sleep(1)
class Con(threading.Thread):
    def run(self):
        global qu
        while True:
            if qu.qsize()>100:
                for i in range(3):
                    msg=self.name+"消费了"+qu.get()
                    print(msg)
            time.sleep(1)
if __name__=="__main__":
    qu=Queue
    for i in range(500):
        qu.put("初始产品"+str(i),)
    for i in range(2):
        p=Pro()
        p.start()
    for i in range(5):
        c=Con()
        c.start()

代码跟老师的一样,为什么会报这样的错??????????

Traceback (most recent call last):

  File "C:/Users/齐泉/PycharmProjects/1/test2.py", line 28, in <module>

    qu.put("初始产品"+str(i))

TypeError: put() missing 1 required positional argument: 'item'


Process finished with exit code 1


Python 全系列/第三阶段:Python 网络与并发编程/并发编程 38573楼

package cn.sxt.game;

import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Date;

/**
 * 飞机游戏的主窗口
 * @author 高淇
 *
 */
public class MyGameFrame  extends  Frame {
	
	Image   planeImg  = GameUtil.getImage("images/plane.png");
	Image   bg  = GameUtil.getImage("images/bg.jpg");
	
	Plane   plane = new Plane(planeImg,250,250);
	Shell[]   shells = new Shell[50];
	
	Explode   bao ;
	Date  startTime = new Date();
	Date  endTime;
	int period;   //游戏持续的时间
	
	@Override
	public void paint(Graphics g) {		//自动被调用。  g相当于一只画笔
		Color   c =  g.getColor();
		g.drawImage(bg, 0, 0, null);
		
		plane.drawSelf(g);  //画飞机
		
		//画出所有的炮弹
		for(int i=0;i<shells.length;i++){
			shells[i].draw(g);
			
			//飞机和炮弹的碰撞检测!!!
			boolean  peng = shells[i].getRect().intersects(plane.getRect());
			if(peng){
				plane.live = false;
				if(bao ==null){
					bao  = new Explode(plane.x, plane.y);
					
					endTime = new Date();
					period = (int)((endTime.getTime()-startTime.getTime())/1000);
				}
				bao.draw(g);
			}
			
			//计时功能,给出提示
			if(!plane.live){
				g.setColor(Color.red);
				Font   f  =  new Font("宋体", Font.BOLD, 50);
				g.setFont(f);
				g.drawString("时间:"+period+"秒", (int)plane.x, (int)plane.y);
			}
			
		}
		
		g.setColor(c);
	}
	
	
	//帮助我们反复的重画窗口!
	class  PaintThread  extends  Thread  {
		@Override
		public void run() {
			while(true){
				repaint();		//重画
				
				try {
					Thread.sleep(40);   	//1s=1000ms
				} catch (InterruptedException e) {
					e.printStackTrace();
				}		
			}
		}
		
	}
	
	//定义键盘监听的内部类
	class   KeyMonitor extends  KeyAdapter  {

		@Override
		public void keyPressed(KeyEvent e) {
			plane.addDirection(e);
		}

		@Override
		public void keyReleased(KeyEvent e) {
			plane.minusDirection(e);
		}
		
		
	}
	
	
	/**
	 * 初始化窗口
	 */
	public  void  launchFrame(){
		this.setTitle("尚学堂学员_程序猿作品");
		this.setVisible(true);
		this.setSize(Constant.GAME_WIDTH	, Constant.GAME_HEIGHT);
		this.setLocation(300, 300);
		
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		
		new PaintThread().start();	//启动重画窗口的线程
		addKeyListener(new KeyMonitor());   //给窗口增加键盘的监听
		
		
		//初始化50个炮弹
		for(int i=0;i<shells.length;i++){
			shells[i] = new Shell();
		}
		
	}
	
	public static void main(String[] args) {
		MyGameFrame  f = new MyGameFrame();
		f.launchFrame();
	}
	
	private Image offScreenImage = null;
	 
	public void update(Graphics g) {
	    if(offScreenImage == null)
	        offScreenImage = this.createImage(Constant.GAME_WIDTH,Constant.GAME_HEIGHT);//这是游戏窗口的宽度和高度
	     
	    Graphics gOff = offScreenImage.getGraphics();
	    paint(gOff);
	    g.drawImage(offScreenImage, 0, 0, null);
	}
	
}

老师,我这个照着敲得代码,为什么运行有时候会出现如下异常?

异常.png

JAVA 全系列/第一阶段:JAVA 快速入门/飞机大战小项目训练 38574楼
JAVA 全系列/第一阶段:JAVA 快速入门/JAVA入门和背景知识 38576楼
JAVA 全系列/第三阶段:数据库编程/Oracle 数据库的使用 38579楼
JAVA 全系列/第一阶段:JAVA 快速入门/控制语句、方法、递归算法 38580楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637