如何在JAVA中修复这个简单的程序?猜谜游戏

时间:2015-03-14 20:24:08

标签: java swing joptionpane

这是我上大学的第一年,我需要为我的作业修好这个课程。在用户输入他/她的第一个猜测后,如何让程序循环返回另一个输入?

import javax.swing.JOptionPane;
import java.util.*;

public class Guessing {
  public static void main(String[] args) {
    final int MAX = 20;
    int answer, guess, lowcount = 0, highcount = 0;
    String sguess;
    Random generator = new Random();
    answer = generator.nextInt(MAX) + 1;
    do {
      sguess = JOptionPane.showInputDialog("I'm thinking of a number between 1 and " + MAX + ". Guess what it is: ");
      guess = Integer.parseInt(sguess);

      if (guess > answer) {
        JOptionPane.showMessageDialog(null, "That is TOO HIGH!");
        highcount++;
        break;
      } else if (guess < answer) {
        JOptionPane.showMessageDialog(null, "That is TOO LOW!");
        lowcount++;
        break;
      }
    }
    while (guess != answer);
  }
}

1 个答案:

答案 0 :(得分:1)

你需要删除'do-while'循环中的break语句。