При запуске созданного мною 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);
}
}
}