2

我在尝试存储JTextfield位于方法 GuessGame() 中的值并将它们放入位于 findMiddleNumber() 的数组中时遇到问题。程序结束时我得到的只是用户的最后一个输入,即随机数。

有人可以看看我的代码,看看我哪里出错了

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class GuessGame extends JFrame
{
Container container;
int currentDistance;

int inputGuess;
int lastDistance;
int medianValue = 40;
int middleNumber;
int positionIndex = 5;

private JButton newGame;
private JLabel message;
private JLabel prompt; 
private JLabel numberInput;
private JTextField guessInput;
private int countGuess;
private int randomNumber;



public GuessGame()
{
    super( "Play Guess A Number Game Version 2.1" );
    setLayout( new FlowLayout() );

    countGuess = 0;

    prompt = new JLabel( "A random number between N and 100 has been"
            + " selected." );
    numberInput = new JLabel( "Write your number and press enter key: ");

    guessInput = new JTextField( 5 );
    guessInput.addActionListener( new GuessHandler() );
    message = new JLabel( "The result will be shown here." );


    newGame = new JButton( "Start New Game" );
    newGame.addActionListener( new ActionListener()
    {
        @Override
        public void actionPerformed( ActionEvent event )
        {

            guessInput.setText("");
            guessInput.setEditable(true);

            generateNumber();

        }
    });

    container = getContentPane();
    container.add( prompt);
    container.add( numberInput );
    container.add( guessInput );
    container.add( message );
    container.add( newGame );

    setSize( 380, 150 );
    setVisible( true );

    generateNumber();

}

private void generateNumber()
{
    randomNumber = ( int ) ( Math.random() * 93 + 7 );
    System.out.println( randomNumber );

}        

public void processGuessedNumber( int usersGuess )
{
    countGuess++;

    if ( countGuess == 1 )
    {
        lastDistance = Math.abs( usersGuess - randomNumber );

        if ( usersGuess > randomNumber )
            message.setText( "Higher than correst guess." );
        else
            message.setText( "Lower than correct guess." );

    }

    else 
    {
        currentDistance = Math.abs( usersGuess - randomNumber );

        if ( usersGuess > randomNumber )
        {
            message.setText( "Higher than correct guess." );

            lastDistance = currentDistance;

        }

    else if ( usersGuess < randomNumber )
    {
        message.setText( "Lower than correct guess." );

        lastDistance = currentDistance;

    }

        else
        {
            message.setText( "Congratulations, your guess is correct!" );

            guessInput.setEditable( false );
            countGuess = 0;

            findMiddleNumber();
        }
    }
}
public void findMiddleNumber()
{
    int num_of_guesses = 0; 

    Scanner inputs = new Scanner( System.in );

    int [] testArray = new int[ 20 ]; 

    do 
    { 
        if ( inputGuess != randomNumber ) 
        { 
            inputGuess  = inputs.nextInt();      

        } 
        else  
            testArray[ num_of_guesses ] = inputGuess; 
            num_of_guesses++; 

            if ( inputGuess >= randomNumber) 
            { 
                break;      
            }  

    }  
    while ( inputGuess != randomNumber); 

    middleNumber = testArray[( num_of_guesses)/ 2]; 

    System.out.printf( "\nMiddle is: " + middleNumber);

    finalOutput();
}


public void finalOutput()
{
    JOptionPane.showMessageDialog( null,"Congratulations, your guess "
        + "is correct." + "\n\n1. Middle number from all guessed "
        + "numbers by the user is: " + middleNumber + "\n\r2. Median "
        + " value of all guessed numbers by the user is: " 
        + medianValue + "\n\r3. A position (array index) of correctly "
        + "guessed number in sorted array is: " + positionIndex, 
        "Final Output", JOptionPane.PLAIN_MESSAGE );  

}

public static void main( String [] args )
{
    GuessGame application = new GuessGame();
    application.setDefaultCloseOperation( EXIT_ON_CLOSE );

}        

class GuessHandler implements ActionListener
{
    @Override
    public void actionPerformed ( ActionEvent event )
    {
        inputGuess = Integer.parseInt( guessInput.getText() );
        processGuessedNumber( inputGuess );

    }
}   
}
4

1 回答 1

2

您需要在用户到来时获取用户的输入并存储它们。在你actionPerformed()正在谈论的值中JTextField,将这些值存储在一个数组test[]中,然后你调用你的ProcessGuessedNumber,然后如果它匹配,你的findmidlenumber被调用,我不知道为什么你过去常常从其中获取输入system.in并将它们存储在一个数组中inputGuess匹配randomNumber所以很明显你的inputGuess最新值将是最后一个,即randomNumber.

当他输入时将用户输入数组中,middleNumer()如果它等于randomNumber,控件将自动移动到您的位置。因此,在您的 GuessHandler 中actionPerformed()

 public void actionPerformed ( ActionEvent event )
{
    inputGuess = Integer.parseInt( guessInput.getText() );
    test[i]= inputGuess;
    processGuessedNumber( inputGuess );
    i++;

}

然后使用这个数组来计算你想要的任何东西,比如:

middleNumber = test[( i)/ 2]; 
System.out.printf( "\nMiddle is: " + middleNumber);
finalOutput();

Complete Code

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class GuessGame extends JFrame {
Container container;
int currentDistance;

int inputGuess;
int lastDistance;
int medianValue = 40;
int middleNumber;
int positionIndex = 5;
int test[] = new int[20];
int num_of_guess = 0;

private JButton newGame;
private JLabel message;
private JLabel prompt;
private JLabel numberInput;
private JTextField guessInput;
private int countGuess;
private int randomNumber;

public GuessGame() {
    super("Play Guess A Number Game Version 2.1");
    setLayout(new FlowLayout());

    countGuess = 0;

    prompt = new JLabel("A random number between N and 100 has been"
            + " selected.");
    numberInput = new JLabel("Write your number and press enter key: ");

    guessInput = new JTextField(5);
    guessInput.addActionListener(new GuessHandler());
    message = new JLabel("The result will be shown here.");

    newGame = new JButton("Start New Game");
    newGame.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {

            guessInput.setText("");
            guessInput.setEditable(true);

            generateNumber();

        }
    });

    container = getContentPane();
    container.add(prompt);
    container.add(numberInput);
    container.add(guessInput);
    container.add(message);
    container.add(newGame);

    setSize(380, 150);
    setVisible(true);

    generateNumber();

}

private void generateNumber() {
    randomNumber = (int) (Math.random() * 93 + 7);
    // randomNumber = 45;
    System.out.println(randomNumber);

}

public void processGuessedNumber(int usersGuess) {
    countGuess++;

    if (countGuess == 1) {
        lastDistance = Math.abs(usersGuess - randomNumber);

        if (usersGuess > randomNumber)
            message.setText("Higher than correst guess.");
        else
            message.setText("Lower than correct guess.");

    }

    else {
        currentDistance = Math.abs(usersGuess - randomNumber);

        if (usersGuess > randomNumber) {
            message.setText("Higher than correct guess.");

            lastDistance = currentDistance;

        }

        else if (usersGuess < randomNumber) {
            message.setText("Lower than correct guess.");

            lastDistance = currentDistance;

        }

        else {
            message.setText("Congratulations, your guess is correct!");

            guessInput.setEditable(false);
            countGuess = 0;

            findMiddleNumber();
        }
    }
}

public void findMiddleNumber() {
    /*
     * int num_of_guesses = 0;
     * 
     * Scanner inputs = new Scanner( System.in );
     * 
     * int [] testArray = new int[ 20 ];
     * 
     * do { if ( inputGuess != randomNumber ) {
     * 
     * 
     * {inputGuess = inputs.nextInt();
     * System.out.println("inputGuess:"+inputGuess); }
     * 
     * 
     * } else testArray[ num_of_guesses ] = inputGuess;
     * System.out.println("testarr "
     * +num_of_guesses+" :"+testArray[num_of_guesses]); num_of_guesses++;
     * 
     * if ( inputGuess >= randomNumber) { break; }
     * 
     * } while ( inputGuess != randomNumber);
     */

    middleNumber = test[(num_of_guess) / 2];

    System.out.printf("\nMiddle is: " + middleNumber);

    finalOutput();
}

public void finalOutput() {
    JOptionPane.showMessageDialog(null, "Congratulations, your guess "
            + "is correct." + "\n\n1. Middle number from all guessed "
            + "numbers by the user is: " + middleNumber + "\n\r2. Median "
            + " value of all guessed numbers by the user is: "
            + medianValue + "\n\r3. A position (array index) of correctly "
            + "guessed number in sorted array is: " + positionIndex,
            "Final Output", JOptionPane.PLAIN_MESSAGE);

}

public static void main(String[] args) {
    GuessGame application = new GuessGame();
    application.setDefaultCloseOperation(EXIT_ON_CLOSE);

}

class GuessHandler implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent event) {
        inputGuess = Integer.parseInt(guessInput.getText());
        // System.out.println("inputGuess"+inputGuess);
        test[num_of_guess] = inputGuess;
        //System.out.println(num_of_guess + ":" + test[num_of_guess]);
        processGuessedNumber(inputGuess);
        num_of_guess++;

    }
}
}
于 2012-04-30T07:28:07.890 回答