【问题标题】:Java applets seem to double-promptJava 小程序似乎双重提示
【发布时间】:2011-05-09 00:24:40
【问题描述】:

在运行我制作的 Java 小程序(一个基本的猜数小游戏)时,每当它提出问题时,无论是/否或其他问题,它都会询问前几个问题,并执行标准程序以获得结果,然后打开下一个问题并重新打开上一组问题。这会持续很长一段时间,直到您关闭 html 文件才停止。我希望能够看看我的游戏是否真的有效,并可能玩它,虽然它很简单。谁能帮忙解决这个问题?

完整的代码,但前几个问题是问题的真正根源。

import java.applet.Applet;
import java.awt.Graphics;
import javax.swing.JOptionPane;
import java.util.Random;

public class HelloWorld extends Applet {

    public void paint(Graphics g) {

        g.drawString ("James mylastname", 50, 25);
        String ans1 = JOptionPane.showInputDialog("Please input a value");
        String ans2 = JOptionPane.showInputDialog("Please input another value");
        String ans3 = JOptionPane.showInputDialog("Please input a final value");
        double ans1double = Double.parseDouble(ans1);
        double ans2double = Double.parseDouble(ans2);
        double ans3double = Double.parseDouble(ans3);
        double total = ans1double+ans2double+ans3double;
        double average = total/3;
        String answer = Double.toString (average);
        g.drawString ("The average of these three numbers is " + answer, 50, 50);

        Random generator = new Random();
        int x = generator.nextInt(100);
        x++;
        int i;
        // attribute names should be firstWordLowerCase
        int Prime = 5;
        for (i=2; i < x ;i++ ) {
            int n = x%i;
            if (n==0) {
                Prime = 1;
            } else {
                Prime = 0;
            }
        }

        g.drawString ("A random number has been generated, from 0 to 100. " +
            "Follow the dialogue boxes to guess the number. You have three " +
            "chances, and three hints.", 50, 75);
        int even;
        even = JOptionPane.showConfirmDialog(this, "Do you think the number is even?");
        if (even == 0) {
            if (x%2 == 0) {
                g.drawString ("Yes, this number is even.", 50,75);
            }
            if (x%2 != 0) {
                g.drawString ("No, this number is not even.", 50,75);
            }
        }

        if (even == 1) {
            if (x%2 == 0) {
                g.drawString ("Incorrect. This number is even.", 50,75);
            }
            if (x%2 != 0) {
                g.drawString ("Correct. This number is not even.", 50,75);
            }
        }

        // very bad idea to name one attribute 'Prime' and another 'prime'
        int prime;
        prime = JOptionPane.showConfirmDialog(this, "Do you think the number is prime?");
        if (prime == 0) {
            if (Prime == 1) {
                g.drawString ("Sorry, the number is not prime.", 50, 100);
            }
            if (Prime == 0) {
                g.drawString ("Correct, the number is prime.", 50, 100);
            }
        }

        if (prime == 1) {
            if (Prime == 1) {
                g.drawString ("Correct, the number is prime.", 50, 100);
            }
            if (Prime == 0) {
                g.drawString ("Sorry, the number is not prime.", 50, 100);
            }
        }

        int moreless;
        moreless = JOptionPane.showConfirmDialog(this, "Do you think the number is 50 or lower?");
        if (moreless == 0) {
            if (x <= 50) {
                g.drawString ("Correct. The number is 50 or less.", 50, 125);
            }
            if (x > 50) {
                g.drawString ("Incorrect. The number is higher than 50.", 50, 125);
            }
        }

        if (moreless == 1) {
            if (x<= 50) {
                g.drawString ("Incorrect. The number is lower than 50.", 50, 125);
            }
            if (x > 50) {
                g.drawString ("Correct. The number is higher than 50.", 50, 125);
            }
        }

        String guess1 = JOptionPane.showInputDialog("Please guess what you think the number is.");
        double guess1double = Double.parseDouble(guess1);
        if (guess1double == x) {
            g.drawString ("Correct! You guessed the number!", 50, 150);
            return;
        }
        if (guess1double != x) {
            g.drawString ("Incorrect! Please guess again, you have two more tries!", 50, 150);
        }

        String guess2 = JOptionPane.showInputDialog("Please guess again.");
        double guess2double = Double.parseDouble(guess2);
        if (guess2double == x) {
            g.drawString ("Correct! You guessed the number!", 50, 175);
            return;
        }

        if (guess2double != x) {
            g.drawString ("Incorrect! Please guess again, you have one more try!", 50, 150);
        }

        String guess3 = JOptionPane.showInputDialog("Please guess again.");
        double guess3double = Double.parseDouble(guess3);
        if (guess3double == x) {
            g.drawString ("Correct! You guessed the number!", 50, 200);
            return;
        }

        if (guess3double != x) {
            g.drawString ("Incorrect! Sorry, that was your last guess!", 50, 200);
        }
    }
}

【问题讨论】:

  • 你真的应该考虑更有效地重写你的代码。您的重复率非常高,for 循环可能会消除这种情况。它还可以使您的代码更有效地运行(如果正确完成),并使我们更容易阅读。
  • 这个小程序没有很好的理由覆盖paint()。相反,您应该将输出放在TextField 中。说到这里,为什么要在这个千年里使用 AWT?当询问基于 Swing 的 JApplet 时,您可能会得到更好的帮助,即使只是因为大多数人不记得如何使用 AWT 组件进行编码。
  • 另外,这次你很幸运,但是在将基于 Swing 的组件(例如 JOptionPane)与 AWT(例如 Applet)混合时会出现意外(和不受欢迎)。
  • 一开始我没有仔细看代码。当我这样做的时候,它的格式很差,让我眼花缭乱。请在以后的代码帖子中使用一致的缩进和格式。请注意,我重新格式化了代码并向其中添加了两个 cmets。

标签: java applet double prompt


【解决方案1】:

将那些只需要执行一次的代码部分放在正确的位置是在init() 方法中。

DemoApplet.java

// <applet code='DemoApplet' width='400' height='400'></applet>
import java.applet.Applet;

public class DemoApplet extends Applet {

    @Override
    public void init() {
        System.out.println("init() once only at start-up");
    }

    @Override
    public void start() {
        System.out.println("start() potentially many times " +
            "(e.g. each time restored from minimized)");
    }

    @Override
    public void stop() {
        System.out.println("stop() potentially many times " +
            "(e.g. each time minimized)");
    }

    @Override
    public void destroy() {
        System.out.println("destroy() once only at shut down");
    }
}

示例 I/O

prompt>appletviewer DemoApplet.java
init() once only at start-up
start() potentially many times (e.g. each time restored from minimized)
stop() potentially many times (e.g. each time minimized)
start() potentially many times (e.g. each time restored from minimized)
stop() potentially many times (e.g. each time minimized)
start() potentially many times (e.g. each time restored from minimized)
stop() potentially many times (e.g. each time minimized)
destroy() once only at shut down

【讨论】:

    【解决方案2】:

    paint 方法中有 UI 创建代码,该方法每秒调用多次以呈现小程序视图。这就是原因。您需要将该代码移至调用一次的init 方法。

    http://www.java2s.com/Code/Java/Swing-JFC/DemonstrationofAppletMethods.htm

    http://www.cafeaulait.org/course/week5/28.html

    【讨论】:

    • 好吧,这是有道理的。但是,它如何应用于此代码?我写了一个方法start,它不带参数并调用paint(g),但这不起作用。我这样做完全不正确吗?
    • 我对 start 和 init 感到困惑,抱歉。这是两者中的任何一个。感谢您的建议。
    • 感谢您编辑您的答案。反对票被推翻为赞成票。
    【解决方案3】:

    问题是您有代码在您的paint() 方法中提示用户。 AWT 会根据需要多次调用该方法来绘制小程序的内容。相反,将此代码移动到您知道只会被调用一次的单独方法中。一个好的候选人是Applet.start()

    【讨论】:

    • 不清楚他是否希望代码再次执行,例如浏览器窗口被恢复。在实践中,start 很少被调用多次,尤其是。使用插件 2。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-13
    • 1970-01-01
    • 1970-01-01
    • 2014-10-22
    相关资源
    最近更新 更多