会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132422个问题
Python 全系列/第五阶段:数据库编程/mysql的使用 32791楼
Python 全系列/第一阶段:Python入门/控制语句 32792楼
JAVA 全系列/第一阶段:JAVA 快速入门/IDEA的使用和第一个java项目 32794楼

关于return在方法中的作用,我不太明白,比如下代码:


public static int calculateScore(boolean gameOver,int score, int levelCompleted, int bonus) {
        if(gameOver) {
            int finalScore = score + (levelCompleted * bonus);
            finalScore += 2000;
            System.out.println("Your final score was " + finalScore);
            return finalScore;
        }
        return -1;
    }
boolean gameOver = true;
int score = 800;
int levelCompleted = 5;
int bonus = 100;
calculateScore(gameOver, score, levelCompleted, bonus);
calculateScore(false, score, levelCompleted, bonus);



有三个问题:

  1. 在写的时候,如果不return,会报错。这里在执行最后一句和倒数第二句时,实际的作用是计算并print最终分数,所以我不懂为什么一定要return,否则报错。

2.return的数据最终在哪里?之前的课程讲过,方法执行完成后,会出栈并消失,值存与堆之中。那么,这个return的值,在方法出栈并消失后,存在哪里?在堆中吗?是以什么形式存在堆中呢?比如这里是int,就存于堆中吗?还是在方法区?

3. return的值,在这个方法中,是看不出来的。特别是,最后一行代码实际没有打印出任何东西。

如果我想把return的值打印出来(比如这里返回finalScore和-1),我要怎么写代码才能看到return的值,谢谢




JAVA 全系列/第一阶段:JAVA 快速入门/面向对象详解和JVM底层内存分析 32795楼
JAVA 全系列/第九阶段:权限控制与安全认证/Shiro 32798楼

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 接收客户端发送消息的线程类
 */

class ChatReceive extends Thread{
    private Socket socket;
    public ChatReceive(Socket socket){
        this.socket=socket;
    }
    @Override
    public void run() {
        this.receiveMsg();
    }

    /**
     * 实现接收客户端发送的消息
     */
    private void receiveMsg(){
        BufferedReader br=null;
        try {
            br=new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
            while (true){
                String msg=br.readLine();
                synchronized ("abc"){
                    //把读取到的数据写入公共数据区
                    ChatRoomServer.buf="["+this.socket.getInetAddress()+"] "+msg;
                    //唤醒发送消息的线程对象
                    "abc".notifyAll();
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (this.socket!=null){
                    try {
                        this.socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
/**
 *向客户端发送消息的线程类
 */

class ChatSend extends Thread {
    private  Socket socket;

    public ChatSend(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        this.sendMsg();
    }

    /**
     * 将公共数据区的消息发送给客户端
     */
    private void sendMsg() {
        PrintWriter pw = null;
        try {
            pw = new PrintWriter(this.socket.getOutputStream());
            while (true) {
                synchronized ("abc") {
                    //让发送消息的线程处于等待状态
                    "abc".wait();
                    //将公共数据区中的消息发送给客户端
                    pw.println(ChatRoomServer.buf);
                    pw.flush();


                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (pw != null) {
                pw.close();
            }
            if (this.socket != null) {
                try {
                    this.socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

public class ChatRoomServer {
    //定义公共数据区
    public static String buf;
    public static void main(String[] args) {
        System.out.println("Chat Server Version 1.0");
        System.out.println("Listen at 8888.....");
        ServerSocket serverSocket=null;
        try {
            serverSocket=new ServerSocket(8888);
            while (true){
                Socket socket=serverSocket.accept();
                System.out.println("连接到"+socket.getInetAddress());
                new ClientReceive(socket).start();
                new ClientSend(socket).start();


            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (serverSocket!=null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

我按老师的代码一模一样敲的,为什么运行的时候和老师的不一样;我感觉向客户端发送消息的线程没有执行。这是怎么回事呢


JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 32799楼
人工智能/第十四阶段:深度学习-目标检测YOLO(V8正在更新中)实战/YOLOv5项目实战 32801楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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