小编T. *_*mas的帖子

动态编程 - 改变

我无法弄清楚动态硬币更改问题的最后一段代码.我已经包含了以下代码.

我无法弄清楚最后一个else.我应该在那时使用贪婪算法,还是可以从表中已有的值计算答案?我努力尝试理解这个问题,我觉得我非常接近.该方法通过创建表并使用存储在表中的结果来解决较大的问题而不使用递归,从而找到进行特定更改所需的最小硬币数量.

public static int minCoins(int[] denom, int targetAmount){
    int denomPosition; // Position in denom[] where the first spot
                       // is the largest coin and includes every coin
                       // smaller.
    int currentAmount; // The Amount of money that needs to be made
                       // remainingAmount <= initalAmount
    int[][] table = new int[denom.length][targetAmount+1];
    for(denomPosition = denom.length-1 ; denomPosition >= 0 ; denomPosition--) {
        for(currentAmount = 0 ; currentAmount <= targetAmount ; currentAmount++){
            if (denomPosition == denom.length-1){
                table[denomPosition][currentAmount] = 
                     currentAmount/denom[denomPosition];
            } …
Run Code Online (Sandbox Code Playgroud)

java dynamic-programming greedy

6
推荐指数
1
解决办法
1万
查看次数

使用javax.sound.sampled.Clip在游戏中播放,循环和停止多个声音.意外错误

我正试图在游戏中同时播放两个wav声音(背景音乐和效果).我首先使用java中的另一个音频处理程序构建了这段代码,它将处理声音的播放,停止和循环.此构造将播放背景音乐或效果,但一次只能播放一个.我环顾互联网并被告知使用javax.sound.sampled.Clip处理声音,因此重复使用相同的构造(播放,停止,循环),但将其切换为使用javax.sound.sampled.Clip.现在我完全迷失了.从我到目前为止所读到的,我已经完成了所有正确的操作并且在eclipse编辑器中没有出现任何错误,但是当我运行它时,我得到了两个错误之一.在eclipse(在Linux上运行)中抛出LineUnavailableException.在eclipse(在Windows 7上运行)中,我在此代码的loop()部分中获得了java.lang.NullPointerException.如果你能告诉我我做错了什么或者给我一些相关文档,我会很感激.我假设我的代码处理异常,但我不确定.如果你看到任何其他可怕的代码失误,请让我知道我正在努力成为最好的程序员,我真的很感激建设性的批评.感谢您的时间.

    import java.io.File;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.Clip;
    import javax.sound.sampled.LineUnavailableException;
    import javax.sound.sampled.UnsupportedAudioFileException;

    /**
     * Handles play, pause, and looping of sounds for the game.
     * @author Tyler Thomas
     *
     */
    public class Sound {
        private Clip myClip;
        public Sound(String fileName) {
                try {
                    File file = new File(fileName);
                    if (file.exists()) {
                        Clip myClip = AudioSystem.getClip();
                        AudioInputStream ais = AudioSystem.getAudioInputStream(file.toURI().toURL());
                        myClip.open(ais);
                    }
                    else {
                        throw new RuntimeException("Sound: file not found: " + fileName);
                    }
                } …
Run Code Online (Sandbox Code Playgroud)

java audio exception game-development

6
推荐指数
1
解决办法
3万
查看次数

使用有界框的碰撞检测

我正在为课程做一个编程项目.我们正在编制游戏突破,包括一个球,一个球拍,四个边缘和砖块.当检测到碰撞时,球从不同的物体上反弹并且每个物体在其自身上预先形成不同的操作.下面是我的代码,目前无法正常工作.我正在尝试使用对象的位置(它的中心点)来构造一个有界框和每个边(顶部,底部,左边,右边)的值来计算框是否命中.我在脑海中想到,可能有两种类型的碰撞,一种是角落撞击,另一种是一种物体碰到另一种物体中间的某处.如果可以,请查看我的代码并提供帮助.我不知道是否有更好的方法来做我正在尝试做的事情,但我目前无法正常工作并且几乎一直都会返回真值.

这是代码的一部分,它检查每个能够与可能发生碰撞的其他对象发生冲突的对象.它还会移动每个对象并勾选游戏的时钟.

/**
     * Tell the GameWorld that the "game clock" has ticked. A clock tick in the GameWorld has the 
     * following effects: (1) all movable objects are told to update their positions according to there 
     * current heading and speed, (2) the "elapsed game time" is incremented by one and (3) all Items are 
     * checked for a collision.
     */
    public void tickClock() {
        gameClock++;
        Iterator theElements0 = listOfGameObjects.getIterator();
        while (theElements0.hasNext()){
            GameObject gObj = (GameObject) theElements0.getNext();
            if ( …
Run Code Online (Sandbox Code Playgroud)

java collision-detection bounding-box

4
推荐指数
1
解决办法
6369
查看次数

加载共享库时出错,无法打开共享对象文件:没有这样的文件或目录 (hiredis)

我正处于编写后端使用 redis 的 C 程序的 alpha 阶段。

我试过构建/安装hiredis(make && sudo make install)并运行测试(大部分通过)但是在尝试构建example.c程序时我收到一个错误,无法找到hiredis.h。

在命令行上构建: gcc -v example.c -lhiredis -I /usr/local/include/hiredis/

我尝试过的事情:

  1. pkg-infohiredis 返回 0
  2. 通过 (make && ./hiredis-test && sudo make) 和 (make clean) 卸载/重新安装
  3. 在与hiredis.c和hiredis.h相同的目录中构建example.c(仍然没有这样的文件或目录)
  4. gcc -v example.c -lhiredis -I /usr/local/include/hiredis/ (以及其他变体)

一切都没有运气。

我猜我没有为 gcc 正确链接程序,但文档没有任何构建示例。

我做错了什么,不允许我使用hiredis 构建此代码(或任何代码)?

确切的输出是...

     Using built-in specs.
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl …
Run Code Online (Sandbox Code Playgroud)

c gcc makefile redis hiredis

2
推荐指数
1
解决办法
2万
查看次数

在Java中向对象添加对象

我正在尝试将一个对象添加到向量中,当我使用下面的代码粘贴时,我得到的错误是"令牌上的语法错误,错位的构造(s)".和"令牌上的语法错误"gamePaddle",在此令牌之后预期的VariableDecloratorID." 我到处寻找,找不到我做错了什么,他们都告诉我像这样构建Vector.错误发生在启动ListOfGameObjects.add的行上(...

class GameWorld {
/**
 * Code that instantiate, hold, and manipulate GameOobjects and related game state data.
 * @author Tyler Thomas
 *
 */
        Paddle gamePaddle = new Paddle();
        Ball gameBall = new Ball();
        Edge topEdge = new Edge(50, 150);
        Edge bottomEdge = new Edge(50, 0);
        Edge leftEdge = new Edge(0, 75);
        Edge rightEdge = new Edge(100, 75);
        Vector<GameObject> ListOfGameObjects = new Vector<GameObject>();
        ListOfGameObjects.add(gamePaddle);
}
Run Code Online (Sandbox Code Playgroud)

java arrays vector

0
推荐指数
2
解决办法
6927
查看次数