Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4613452
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:33:07+00:00 2026-05-22T01:33:07+00:00

When running a java applet that I made (a basic little number-guessing game), whenever

  • 0

When running a java applet that I made (a basic little number-guessing game), whenever it asks a question, be it yes/no or otherwise, it asks the first few questions, and does the standard procedure for the results, then opens the next question and reopens the previous set of questions. This goes on for quite a while, and does not stop at all until you close out of the html file. I’d like to be able to see if my game actually works, and possibly play it, though it’s simple. Can anyone help with this problem?

Whole code for completion, but the first few questions are the real source of the problem.

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);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:33:08+00:00Added an answer on May 22, 2026 at 1:33 am

    The correct place to put those parts of the code that only need to be done once, is in the init() method.

    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");
        }
    }
    

    Example 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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm accessing a java applet that is running on a website (which i can't
I have a Java Applet that I'm making some edits to and am running
I have a java applet that parses a string of xml. However, running the
If I'm running a signed Java applet. Can I load additional classes from remote
I am running a java program that sets up a database connection to an
There is an actual running Java ServerPages (JSP) application within a *NIX box which
What are my options for running Java 6 on OS X? I have an
I'd like to generate sequence diagrams of a running java ee application using jtracert
We're running a java/hibernate app going against ORACLE 10g in TESTING. Once in a
I have an Eclipse RCP app running on Java 6. When I try to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.