会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132885个问题
Python 全系列/第十四阶段:Python 爬虫开发/移动端爬虫开发- 4036楼
Python 全系列/第一阶段:Python入门/函数和内存分析 4037楼
JAVA 全系列/第一阶段:JAVA 快速入门/面向对象详解和JVM底层内存分析 4038楼
Python 全系列/第一阶段:Python入门/面向对象 4040楼
Python 全系列/第一阶段:Python入门/面向对象 4042楼
JAVA 全系列/第四阶段:网页编程和设计/CSS3(旧) 4044楼
WEB前端全系列/第二阶段:JavaScript编程模块/运算符_数据类型和流程循环语句 4045楼

import java.util.Scanner;

public class TestSalary {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("***********我的薪水计算器***********");
        System.out.println("1.输入 88,退出程序\n2.输入 66,计算下一个年薪");
        while(true){
            System.out.println("请输入月薪:");
            int monthSalary = s.nextInt();
            System.out.println("请输入一年几个月薪资:");
            int months = s.nextInt();
            int yearSalary = monthSalary*months; //年薪
            System.out.println("年薪是:"+yearSalary);
            if(yearSalary>=200000){
                System.out.println("恭喜你超越 98%的国人");
            }else if(yearSalary>=100000){
                System.out.println("恭喜你超越 90%的国人");
            }
            System.out.println("输入 88,退出系统;输入 66,继续计算。");
            int comm = s.nextInt();
            if(comm==88){
                System.out.println("系统退出!");
                break;
            }
            if(comm==66) {
                System.out.println("继续计算下一个薪资");
               continue;
            }
        }
    }
}

老师,因为这个循环本身是死循环,跳出这个循环和不跳出这个循环,之后执行结果都一样,是不是continue就可加可不加

JAVA 全系列/第一阶段:JAVA 快速入门/控制语句、方法、递归算法 4047楼

/**
 * 创建发送消息类
 */

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

    }
    /**
     * 发送消息
     */
    private void sendMsg(){
        Scanner scanner = null;
        PrintWriter pw = null;
        try {
            //键盘输入
            scanner = new Scanner(System.in);
            //创建向对方输出消息的流对象
            pw = new PrintWriter(this.socket.getOutputStream());
            while (true){
                String msg = scanner.nextLine();
                if ("exit".equals(msg)){
                    break;
                }
                pw.println(msg);
                pw.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (pw != null){
                pw.close();
            }
            if (scanner != null){
                scanner.close();
            }
            if(this.socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

/**
 * 创建接收消息线程类
 */
class ClientReceive extends Thread {
    private Socket socket;
    public ClientReceive(Socket socket){
        this.socket = socket;
    }
    @Override
    public void run() {

    }
    /**
     * 接收消息的方法
     */
    private void receiveMsg(){
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
            while (true){
                String msg = br.readLine();
                System.out.println("他说: " +msg );

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

public class PointSocketClient {
    public static void main(String[] args)  {

        try {
            Socket socket = new Socket("127.0.0.1",8888);
            System.out.println("连接成功");
            new ClientSend(socket).start();
            new ClientReceive(socket).start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }
}
/**
 * 创建发送消息类
 */

class Send extends Thread{
    private  Socket socket;
    public Send(Socket socket){

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

    }

    /**
     * 发送消息
     */
    private void sendMsg(){
        Scanner scanner = null;
        PrintWriter pw = null;
        try {
            //键盘输入
            scanner = new Scanner(System.in);
            //创建向对方输出消息的流对象
            pw = new PrintWriter(this.socket.getOutputStream());
            while (true){
                String msg = scanner.nextLine();
                if ("exit".equals(msg)){
                    break;
                }
                pw.println(msg);
                pw.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (pw != null){
                pw.close();
            }
            if (scanner != null){
                scanner.close();
            }
            if(this.socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
/**
 * 创建接收消息线程类
 */
class Receive extends Thread{
    private  Socket socket;

    public Receive(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();
                System.out.println("他说: " +msg );

            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
public class PointSocketServer {
    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        try {
            serverSocket =new ServerSocket(8888);
            System.out.println("服务器启动等待连接.....");
            Socket socket = serverSocket.accept();
            System.out.println("连接成功");
            new Send(socket).start();
            new Receive(socket).start();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (serverSocket != null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

老师我这个代码只有客户端给服务端发消息的收回会输出

服务端给客户端发消息时没有反应,麻烦老师看下

JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 4048楼

变量定义.删除.png

Python 全系列/第一阶段:Python入门/编程基本概念 4049楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/手写服务器项目(旧) 4050楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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