繁体   English   中英

如何使applet将用户的输入转换为整数并将其与计算机的随机数进行比较?

[英]How do I make my applet turn the user's input into an integer and compare it to the computer's random number?

我正在开始编程,但是我还不完全了解applet。 但是,(在Internet教程的帮助下)我能够创建一个applet,与用户进行猜谜游戏。 小程序可以正常编译,但是运行时会出现以下错误消息:

"Exception in thread "main" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:470)
    at java.lang.Integer.parseInt(Integer.java:499)
    at Guess.createUserInterface(Guess.java:101)
    at Guess.<init>(Guess.java:31)
    at Guess.main(Guess.java:129)"

我尝试将101行上的“ userguess = Integer.parseInt( t1.getText() ); ”移动到多个位置,但是仍然遇到相同的错误。 谁能告诉我我在做什么错? 编码:

// Creates the game GUI.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

    public class Guess extends JFrame{

        private JLabel userinputJLabel;
        private JLabel lowerboundsJLabel;
        private JLabel upperboundsJLabel;
        private JLabel computertalkJLabel;
        private JButton guessJButton;
        private JPanel guessJPanel;
        static int computernum;
        int userguess;
        static void declare() {
        computernum = (int) (100 * Math.random()) + 1;       //random number picked (1-100)
    }
// no-argument constructor
        public Guess()
   {
    createUserInterface();

   }
 // create and position GUI components
   private void createUserInterface()
   {

 // get content pane and set its layout
        Container contentPane = getContentPane();
        contentPane.setLayout( null );
        contentPane.setBackground( Color.white );

// set up userinputJLabel
        userinputJLabel = new JLabel();
        userinputJLabel.setText( "Enter Guess Here -->" );
        userinputJLabel.setBounds( 0, 65, 120, 50 );
        userinputJLabel.setHorizontalAlignment( JLabel.CENTER );
        userinputJLabel.setBackground( Color.white );
        userinputJLabel.setOpaque( true );
        contentPane.add( userinputJLabel );
// set up lowerboundsJLabel
        lowerboundsJLabel = new JLabel();
        lowerboundsJLabel.setText( "Lower Bounds Of Guess = 1" );
        lowerboundsJLabel.setBounds( 0, 0, 170, 50 );
        lowerboundsJLabel.setHorizontalAlignment( JLabel.CENTER );
        lowerboundsJLabel.setBackground( Color.white );
        lowerboundsJLabel.setOpaque( true );
        contentPane.add( lowerboundsJLabel );
// set up upperboundsJLabel
        upperboundsJLabel = new JLabel();
        upperboundsJLabel.setText( "Upper Bounds Of Guess = 100" );
        upperboundsJLabel.setBounds( 250, 0, 170, 50 );
        upperboundsJLabel.setHorizontalAlignment( JLabel.CENTER );
        upperboundsJLabel.setBackground( Color.white );
        upperboundsJLabel.setOpaque( true );
        contentPane.add( upperboundsJLabel );
// set up computertalkJLabel
        computertalkJLabel = new JLabel();
        computertalkJLabel.setText( "Computer Says:" );
        computertalkJLabel.setBounds( 0, 130, 100, 50 );  //format (x, y, width, height)
        computertalkJLabel.setHorizontalAlignment( JLabel.CENTER );
        computertalkJLabel.setBackground( Color.white );
        computertalkJLabel.setOpaque( true );
        contentPane.add( computertalkJLabel );
//Set up guess jbutton
        guessJButton = new JButton();
        guessJButton.setText( "Enter" );
        guessJButton.setBounds( 250, 78, 100, 30 );
        contentPane.add( guessJButton );
        guessJButton.addActionListener(

         new ActionListener() // anonymous inner class
         {
            // event handler called when Guess button is pressed
            public void actionPerformed( ActionEvent event )
            {
               guessActionPerformed( event );
            }

         } // end anonymous inner class

      ); // end call to addActionListener
// set properties of application's window
        setTitle( "Guess Game" ); // set title bar text
        setSize( 500, 500 ); // set window size
        setVisible( true );  // display window
//create text field
        TextField t1 = new TextField();                // Blank text field for user input
        t1.setBounds( 135, 78, 100, 30 );
        contentPane.add( t1 );
        userguess = Integer.parseInt( t1.getText() );
//create section for computertalk
        Label computertalkLabel = new Label("");
        computertalkLabel.setBounds( 115, 130, 300, 50);
        contentPane.add( computertalkLabel );
 }

 // Display computer reactions to user guess
   private void guessActionPerformed( ActionEvent event )
   {


      if (userguess > computernum)          //if statements (computer's reactions to user guess)
          computertalkJLabel.setText( "Computer Says: Too High" );
      else if (userguess < computernum)
          computertalkJLabel.setText( "Computer Says: Too Low" );
      else if (userguess == computernum)
          computertalkJLabel.setText( "Computer Says:You Win!" );
      else
          computertalkJLabel.setText( "Computer Says: Error" );


   } // end method oneJButtonActionPerformed
 // end method createUserInterface

 // main method
   public static void main( String args[] )
   {
      Guess application = new Guess();
      application.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);


   } // end method main

} // end class Phone

请不要使用null布局。

从错误消息中可以看到,您正在按按钮输入数字,而实际上没有在输入中写任何东西。 您需要处理该情况。
您需要更换:

userguess = Integer.parseInt( t1.getText() );

有:

String userEntered = t1.getText().trim();
if(userEntered.equals("")){
    System.out.println("You did not enter anything");
}else{
    try{
        userguess = Integer.parseInt( userEntered );
    }catch(NumberFormatException e){
        System.out.println("Invalid format");
    }
}
java.lang.NumberFormatException: For input string: ""

这意味着将在空字符串“”上调用它。 这不是数字格式,因此会引发异常。

您需要确保在用户获得输入信息后调用它,并且对于用户做一些奇怪的事情,您可能想验证输入或处理此异常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM