Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java小程序似乎有双重提示_Java_Applet_Double_Prompt - Fatal编程技术网

Java小程序似乎有双重提示

Java小程序似乎有双重提示,java,applet,double,prompt,Java,Applet,Double,Prompt,当运行我制作的java小程序(一个基本的猜数字游戏)时,每当它问问题时,不管是“是/否”,它都会问前几个问题,并对结果执行标准过程,然后打开下一个问题并重新打开前一组问题。这会持续相当长的一段时间,直到您关闭html文件时才会停止。我希望能够看到我的游戏是否真的有效,并可能玩它,尽管它很简单。有人能帮忙解决这个问题吗 完整的代码需要完成,但前几个问题才是问题的真正根源 import java.applet.Applet; import java.awt.Graphics; import java

当运行我制作的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);
        }
    }
}
import java.applet.applet;
导入java.awt.Graphics;
导入javax.swing.JOptionPane;
导入java.util.Random;
公共类HelloWorld扩展小程序{
公共空间涂料(图g){
g、 抽绳(“James mylastname”,50,25);
字符串ans1=JOptionPane.showInputDialog(“请输入一个值”);
字符串ans2=JOptionPane.showInputDialog(“请输入另一个值”);
字符串ans3=JOptionPane.showInputDialog(“请输入最终值”);
double-ans1double=double.parseDouble(ans1);
double-ans2double=double.parseDouble(ans2);
双ans3double=double.parseDouble(ans3);
双倍合计=ANS1双倍+ANS2双倍+ANS3双倍;
双倍平均值=总数/3;
字符串应答=Double.toString(平均值);
g、 抽绳(“这三个数字的平均值是”+答案,50,50);
随机生成器=新随机();
int x=发电机下一个(100);
x++;
int i;
//属性名称应为firstWordLowerCase
int素数=5;
对于(i=2;i
paint
方法中有UI创建代码,该方法每秒调用多次以呈现小程序视图。这就是原因。您需要将该代码移动到
init
方法,该方法只调用一次


问题在于,您有代码在
paint()
方法中提示用户。AWT调用该方法的次数与绘制小程序内容所需的次数相同。相反,将此代码移动到一个单独的方法中,您知道该方法只会被调用一次。一个很好的候选者是
Applet.start()
将只需执行一次的代码部分放在
init()
方法中

DemoApplet.java
您应该真正考虑更有效地重写代码。你有一个很高的重复率,可以从b中去掉
// <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");
    }
}
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