会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132420个问题
Python 全系列/第二十三阶段:人工智能基础_机器学习理论和实战(旧)/决策树算法 33228楼

老师您好! 我都是按照视频来敲的 但是跟老师的不一样我这些地方都会报错,能帮忙看看吗?,谢谢


package com.bjsxt.servlet;


import com.bjsxt.server.Request;

import com.bjsxt.server.Response;


public class LoginServlet extends Servlet {


@Override

public void doGet(Request req, Response rep) throws Exception {

//获取请求参数

String nameString=req.getParamter("username");

String pwdString=req.getParamter("pwd");

if (this.login(name, pwd)) {

//调用响应中的构建内容的方法

rep.println(name+"登陆成功");

}else {

rep.println(name+"登陆失败,对不起,账号或密码不正确");

}

}

private boolean login(String name,String pwd){

if ("bjsxt".equals(name)&&"123".equals(pwd)) {

return true;

}

return false;

}


@Override

public void doPost(Request req, Response rep) throws Exception {

// TODO Auto-generated method stub

}


}

截图:image.png

代码:

package com.bjsxt.server;

import java.io.IOException;
import java.net.Socket;

import com.bjsxt.servlet.Servlet;
import com.bjsxt.util.IOCloseutil;

public class Dispatcher implements Runnable{
	private Request req;
	private Response rep;
	private	Socket	client;
	private	int code=200;//状态码
	//构造方法初始化属性
	public Dispatcher(Socket client){
		//将局部变量的值赋给成员变量
		this.client=client;
		try {
			req=new Request(this.client.getInputStream());
			rep=new Response(this.client.getOutputStream());
		} catch (IOException e) {
			code=500;
			return;
		}
	}
	@Override
	public void run() {
		//根据不同的url创建指定的Servlet对象
		//System.out.println(req.getUrl());
		Servlet servlet=WebApp.getServlet(req.getUrl());
		if (servlet==null) {
			this.code=404;
		}else{
			//调用相应的Servlet中的service
			try {
				servlet.service(req, rep);
			} catch (Exception e) {
				this.code=500;
			}
		}
		//将响应结果推送到客户机的浏览器
		rep.pushToClient(code);
		IOCloseutil.closeAll(client);
	}

}

截图:image.png这个老师那里没有错 但是我这边就报错了


代码:

package com.bjsxt.server;


import java.io.BufferedWriter;

import java.io.IOException;

import java.io.OutputStream;

import java.io.OutputStreamWriter;

import java.io.UnsupportedEncodingException;


import org.omg.IOP.Codec;


import com.bjsxt.util.IOCloseutil;


public class Response {/*响应*/

private StringBuilder headInfo;//响应头

private StringBuilder content;//响应内容

private int length;//响应内容的长度

//流

private BufferedWriter bw;

//两个常量,换行和空格

private static final String CRLF="\r\n";//换行

private static final String BLANK=" ";//空格

//构造方法

public Response(){

headInfo=new StringBuilder();

content=new StringBuilder();

}

//带参构造方法

public Response(OutputStream os){

this();//调用本类的无参构造方法

try {

bw=new BufferedWriter(new OutputStreamWriter(os, "utf-8"));

} catch (UnsupportedEncodingException e) {

headInfo=null;

}

}

//构造正文部分

public Response print(String info){

content.append(info);

try {

length+=info.getBytes("utg=f-8").length;

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return this;

}

public Response println(String info){


content.append(info).append(CRLF);

try {

length+=(info+CRLF).getBytes("utf-8").length;

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return this;

}

//构造响应头

private void createHeadInfo(){

Object code;

headInfo.append("HTTP/1.1").append(BLANK).append(code).append(BLANK);

switch (code) {

case 200:

headInfo.append("OK");

break;

case 500:

headInfo.append("SERVER ERROR");

break;

default:

headInfo.append("NOT FOUND");

break;

}

headInfo.append(CRLF);

headInfo.append("Content-Type:text/html;charset=ytf-8").append(CRLF);

headInfo.append("Content-Length:"+length).append(CRLF);

headInfo.append(CRLF);

}

/**

* 推动到客户机的浏览器

* @param code

*/

public void pushToClient(int code){

if (headInfo==null) {

code=500;

}

try {

//调用本类中的构造响应头

this.createHeadInfo(code);

bw.write(headInfo.toString());

bw.write(content.toString());

bw.flush();

this.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

private void createHeadInfo(int code) {

// TODO Auto-generated method stub

}

public void close(){

IOCloseutil.closeAll(bw);

}

}

截图:image.png这个提示的是:不能打开类型 Object 的值。只允许使用可转换的 int 值或枚举常量


代码:

package com.bjsxt.server;


import java.io.BufferedWriter;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStreamWriter;

import java.net.ServerSocket;

import java.net.Socket;


import com.bjsxt.servlet.Servlet;

import com.bjsxt.util.IOCloseutil;


public class Server {//服务器,用于启动和停止服务

private ServerSocket server;

private boolean isShutDown=false;//默认没有出错

public static void main(String[] args) {

Server server=new Server();//创建服务器对象

server.start();

}

public void start(){

this.start(8888);

}

public void start(int port){

try {

server=new ServerSocket(port);

this.receive();//调用接受请求信息的方法

} catch (IOException e) {

isShutDown=true;

}

}

private void receive() {

try {

while(!isShutDown){

//(1)监听

Socket client=server.accept();

//创建线程类的对象

Dispatcher dis=new Dispatcher(client);

//创建代理类并启动线程

new Thread(dis).start();

}

} catch (IOException e) {

this.stop();//关闭服务器

}

}

public void stop(){

isShutDown=true;

IOCloseutil.closeAll(server);

}

}

截图:image.png提示:类型 IOCloseutil 中的方法 closeAll(Closeable...)对于参数(ServerSocket)不适用

 问题出现的比较多  辛苦老师帮忙解答一下,谢谢!

JAVA 全系列/第二阶段:JAVA 基础深化和提高/手写服务器项目(旧) 33229楼
JAVA 全系列/第三阶段:数据库编程/Oracle 数据库的使用 33231楼

老师,按键控制飞机,飞机不动

package com.bjsxt.plane05;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/** 
 * <p>Title: 游戏主窗口(飞机大战1.0版)</p>  
 * <p>Description: 尚学堂</p>  
 * @author xiaoding
 * @date Apr 16, 2020  
 * @version 1.0 
 */

public class MyGameFrame extends Frame{
    //调用GameUtil类的静态方法,把图片的地址传过去
    Image feiJi = GameUtil.getImage("images/plane.png");
    Image bj = GameUtil.getImage("images/bj.jpg");
    
    Plane plane = new Plane(feiJi,100,100,7);
    
    boolean left,right,up,down;
    //画出窗口
    @Override
    public void paint(Graphics g) {    //g当做是一支画笔
        g.drawImage(bj,0,0,500,500,null);
        plane.drawMyself(g);
        if(left) {
            plane.x -= plane.speed;
        }
        if(right) {
            plane.x += plane.speed;
        }
        if(up) {
            plane.y -= plane.speed;
        }
        if(down) {
            plane.y += plane.speed;
        }
    }
    
    //初始化窗口
        public void launchFrame() {
            //游戏名称
            this.setTitle("飞机大战-尚学堂");        
            setVisible(true);    //窗口是否可见
            
            setSize(Constant.GAME_WIHTH,Constant.GAME_HEIGHT);    //窗口大小
            setLocation(400,400);    //窗口打开位置
            
            //增加关闭窗口的动作
            this.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);    //正常退出程序
                }
            });
            //启动重画窗口内部类方法
            new PaintThread().run();
            //启动键盘监听
            this.addKeyListener(new KeyMonitor());
        }
    
    /*
     *     定义一个重画窗口的线程类
     *     定义内部类是类为方便直接使用窗口类的相关方法
     */
    class PaintThread extends Thread {
        public void run() {
            while(true) {
                repaint();        //内部类可以直接使用外部类的成员!
                
                try {
                    Thread.sleep(50);        //1s=1000ms  (1000 / 50) = 20
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    
    /*
     *     创建一个键盘控制内部类
     *     实现键盘的监听处理
     */
    class KeyMonitor extends KeyAdapter {
        @Override
        public void keyPressed(KeyEvent e) {
            System.out.println("按下:" + e.getKeyCode());
            if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                left = true;
            }
            if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                right = true;
            }
            if (e.getKeyCode() == KeyEvent.VK_UP) {
                up = true;
            }
            if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                down = true;
            }
        }
        @Override
        public void keyReleased(KeyEvent e) {
            System.out.println("抬起:" + e.getKeyCode());
            if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                left = false;
            }
            if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                right = false;
            }
            if (e.getKeyCode() == KeyEvent.VK_UP) {
                up = false;
            }
            if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                down = false;
            }
        }
    }
    
    //解决屏幕闪烁缓冲技术
    private Image offScreenImage = null; 
    public void update(Graphics g) { 
        if(offScreenImage == null) 
            //这是游戏窗口的宽度和高度
            offScreenImage = this.createImage(Constant.GAME_WIHTH,Constant.GAME_HEIGHT);
        
            Graphics gOff = offScreenImage.getGraphics(); 
            paint(gOff); 
            g.drawImage(offScreenImage, 0, 0, null); 
    }
    

    //main主方法,程序执行入口
    public static void main(String[] args) {
        MyGameFrame gameFrame = new MyGameFrame();
        gameFrame.launchFrame();
    }
}


JAVA 全系列/第一阶段:JAVA 快速入门/飞机大战小项目训练 33232楼
JAVA 全系列/第一阶段:JAVA 快速入门/飞机大战小项目训练 33233楼
JAVA 全系列/(旧的隐藏)第七阶段:JAVA 高级技术/Maven 33234楼
JAVA 全系列/(旧的隐藏)第七阶段:JAVA 高级技术/Zookeeper 33237楼

问题:

W~4J`0$8J]I{FT}XR7FIN`7.png

老师,最后一问如何解决呢。

代码:

public class Store {
    private int id;
    private String name;
    private String number;
    private double price;
    private double discount;
    private double sale_price;


    public Store(int id, String name, String number, double price, double discount) {
        this.id = id;
        this.name = name;
        this.number = number;
        this.price = price;
        this.discount = discount;
    }

 public String toString(){
        return "["+id+","+name+","+number+","+price+"discount"+"]";
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public double getDiscount() {
        return discount;
    }

    public void setDiscount(double discount) {
        this.discount = discount;
    }

    public double getSale_price(double m) {
        if (m<(this.price*this.discount)){
            System.out.println(this.name);
        }else {
            System.out.println("这个商品价格太低了");

        }
        return m;
    }

    public void setSale_price(double sale_price) {
        this.sale_price = sale_price;
    }

    public static void main(String[] args) {
        Store[]stores={new Store(1,"baizhan_mouse","BZ_001",99,0.9) ,
                new Store(2,"doll","WO_102",403.00,0.7),
                new Store(3,"java","BK_001",89.00,0.8),
                new Store(4,"suit","GQ_XF_12",700.00,0.5),
                new Store(5,"Mi_phone","DM_PH_13",900.00,0.3)};

        for (int t=0;t<stores.length;t++){
            System.out.println(stores[t]);
        }
        //System.out.println(Arrays.toString(stores));
        }
        }


JAVA 全系列/第一阶段:JAVA 快速入门/数组和数据存储 33238楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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