会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132834个问题
JAVA 全系列/第一阶段:JAVA 快速入门/数组和数据存储 1576楼
JAVA 全系列/第一阶段:JAVA 快速入门/数组和数据存储 1579楼
JAVA 全系列/第一阶段:JAVA 快速入门/JAVA入门和背景知识 1581楼
JAVA 全系列/第一阶段:JAVA 快速入门/JAVA入门和背景知识 1582楼

老师好,关于递归调用,有如下算法代码:




class Solution {
    public void recoverTree(TreeNode root) {
        TreeNode pre = null;
        TreeNode first = null;
        TreeNode second = null;
        helper(root, pre, first, second);
        int temp = first.val;
        first.val = second.val;
        second.val = temp;
    }
    private void helper(TreeNode root, TreeNode pre, TreeNode first, TreeNode second){
        if(root ==  null) return;
        helper(root.left, pre, first, second);
        if(pre != null && pre.val > root.val){
            if(first == null){
                first = pre;
                second = root;
            }else{
                second = root;
            }
        }
        pre = root;
        helper(root.right, pre, first, second);
    }
}


我的问题是3-5行的三个变量,这里要通过一个私有方法helper去修改pre, first和second的变量值,但是运行后发现是空指针。


我的问题是,如果在helper方法中,没有返回三个变量的值,是否就无法改变recoverTree方法中的三个变量值?这里的修改方法,要么是在helper方法中,返回一个first和scond的数组,要么就把三个变量放到全局变量中?


JAVA 全系列/第一阶段:JAVA 快速入门/控制语句、方法、递归算法 1585楼
JAVA 全系列/第一阶段:JAVA 快速入门/JAVA入门和背景知识 1588楼

源代码:

package ballGame;
/**
 * 台球小游戏
 */

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.math.BigDecimal;
import java.math.RoundingMode;
import javax.swing.*;

public class BallGame extends JFrame{
    static int drawCnt = 1;

    Image ball = Toolkit.getDefaultToolkit().getImage("images/ball.png");
    Image desk = Toolkit.getDefaultToolkit().getImage("images/desk.png");

    double x=200;
    double y=200;

  //  boolean right = true;
  //  boolean down = true;

    double degree = Math.PI/4;//3; // 设置球的角度

    public String format(double value){ // 取2位小数
        BigDecimal bd = new BigDecimal(value);
        bd = bd.setScale(2, RoundingMode.HALF_UP);
        return bd.toString();
    }

    public void paint(Graphics g){
     //   System.out.println("窗口画了\t"+(drawCnt++)+"" +
      //          "\t次"+",\tx="+format(x)+",\ty="+format(y)); // 打印轨迹信息

        g.drawImage(desk, 0,0,null);
        g.drawImage(ball, (int)x, (int)y, null);

        x   =  x + 10*Math.cos(degree);
        y   =  y + 10*Math.sin(degree);

        if((x>=790) || (x<30))   {degree = Math.PI - degree;} // 左右边界反弹

        if((y>=430) || (y<70))   {degree = -degree;} // 上下界面反弹


        /*
        if(right)
            x+=1.5;
        else
            x-=1.5;

        if(x>=780){right=false;}
        else if(x<70){right = true;}

        if(down)
            y+=1;
        else
            y-=1;

        if(y>=430){down=false;}
        else if(y<70){down = true;}*/
    }

    // 创建窗口
    void launchFrame(){
        setSize(856, 501);
        setLocation(100, 100);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                //   super.windowClosing(e);
                System.exit(0);
            }
        });
        setVisible(true);


/*
        while(true){
            repaint();

            try {
                Thread.sleep(40);
            }  catch(InterruptedException e)
            {
                e.printStackTrace();
            }
        }*/

        new PaintTread().start();
    }



    /**
     * 重画窗口的线程
     */
    class PaintTread extends Thread{
        @Override
        public void run() {
            while(true) {
                repaint();
                try {
                    Thread.sleep(40);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static void main(String[] args){
        BallGame game = new BallGame();
        game.launchFrame();

    }

    private Image offScreenInage = null;

    // 
    //
    // 参照飞机大战那个小项目,加了如下方法,运行时台球移动还是有鬼影(一闪一闪的),飞机大战里面的
    // 飞机和炮弹不会有残影
    public void update (Graphics g){
        if(offScreenInage == null){
         //   offScreenInage = this.createImage(this.getWidth(), this.getHeight());
            offScreenInage = this.createImage(856, 501);
        }

        Graphics gOff= offScreenInage.getGraphics();
        paint(gOff);
        g.drawImage(offScreenInage, 0, 0, null);
    }
}


运行没问题。

请教一个问题,在飞机大战那个小游戏的实操中,通过这段代码重画窗口以消除鬼影,但是为什么在这个台球这个案例中,我也添加了,但是好像没起到作用


JAVA 全系列/第一阶段:JAVA 快速入门/IDEA的使用和第一个java项目 1589楼
JAVA 全系列/第一阶段:JAVA 快速入门/面向对象详解和JVM底层内存分析 1590楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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