猜猜O'的问题 [英] Guess O' matic issues

查看:46
本文介绍了猜猜O'的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个猜测textarea / field的问题。我可以输入任何我想要的东西但是当我按下回车时我在控制台中出错。



是的。我的坏,错误是

I'm having an issue with my make a guess textarea/field. I can input whatever I want but when I press enter I get an error in the console.

yeah. my bad, the error is

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
 at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
 at java.lang.Integer.parseInt(Integer.java:592)
 at java.lang.Integer.parseInt(Integer.java:615)
 at gom.GOM.newGuess(GOM.java:175)
 at gom.GOM.keyReleased(GOM.java:147)
 at java.awt.Component.processKeyEvent(Component.java:6494)
 at javax.swing.JComponent.processKeyEvent(JComponent.java:2832)
 at java.awt.Component.processEvent(Component.java:6310)
 at java.awt.Container.processEvent(Container.java:2236)
 at java.awt.Component.dispatchEventImpl(Component.java:4889)
 at java.awt.Container.dispatchEventImpl(Container.java:2294)
 at java.awt.Component.dispatchEvent(Component.java:4711)
 at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1954)
 at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:806)
 at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1074)
 at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:945)
 at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:771)
 at java.awt.Component.dispatchEventImpl(Component.java:4760)
 at java.awt.Container.dispatchEventImpl(Container.java:2294)
 at java.awt.Window.dispatchEventImpl(Window.java:2746)
 at java.awt.Component.dispatchEvent(Component.java:4711)
 at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
 at java.awt.EventQueue.access$500(EventQueue.java:97)
 at java.awt.EventQueue$3.run(EventQueue.java:709)
 at java.awt.EventQueue$3.run(EventQueue.java:703)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
 at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
 at java.awt.EventQueue$4.run(EventQueue.java:731)
 at java.awt.EventQueue$4.run(EventQueue.java:729)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
 at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
 at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
 at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
 at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
 at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
 at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
 at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)





这就是我得到的整个错误



我尝试了什么:





thats the entire error that I get

What I have tried:

package gom;

/**
 *
 * @author stephenwessels
 */
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import java.util.Scanner;
import javax.swing.*;
public class GOM extends JFrame implements ActionListener, KeyListener
{

    Scanner in = new Scanner(System.in);
    
    Container content = this.getContentPane();
    JTextField theGuess = new JTextField();
    JLabel bankRoll = new JLabel("100.00 Zipoids");
    JLabel d1 = new JLabel("  ");
    JLabel d2 = new JLabel("  ");
    JButton newPlayer = new JButton("New Player");
    JButton newNumber = new JButton("New Number");
    JTextField thePlayer = new JTextField();
    JTextArea theOutput = new JTextArea("Enter your name" + "\n" + "Press ENTER to play");
    JPanel p1 = new JPanel();
    JPanel p2 = new JPanel();
    JScrollPane scrollArea = new JScrollPane(theOutput);
    
    Random randomizer = new Random();
    String playerName = "";
    int theNumber = 20;
    int numTries = 0;
    int numGames = 0;
    double amtRemaining = 100.0;
    
    
    
    public GOM()
    {
        JLabel guess = new JLabel("Make your guess:");
        
        this.setVisible(true);
        this.setSize(500,400);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("Guess O'Matic");

        content.add(p1, BorderLayout.NORTH);
        p1.setLayout(new BorderLayout());
        p1.add(bankRoll, BorderLayout.EAST);
        p1.add(guess, BorderLayout.WEST);
        p1.add(theGuess);
        
        content.add(p2, BorderLayout.SOUTH);
        p2.setLayout(new BorderLayout());
        p2.add(thePlayer);
        p2.add(newPlayer, BorderLayout.WEST);
        p2.add(newNumber, BorderLayout.EAST);
        
        content.add(scrollArea, BorderLayout.CENTER);
        content.add(d1, BorderLayout.WEST);
        content.add(d2, BorderLayout.EAST);
        
        newPlayer.addActionListener(this);
        newNumber.addActionListener(this);
        
        thePlayer.addKeyListener(this);
        theGuess.addKeyListener(this);
        
        theOutput.setForeground(Color.LIGHT_GRAY);
        
        
        
    }
    
    public void actionPerformed(ActionEvent e) 
    {
        JButton btn = (JButton) e.getSource();
        if (btn == newPlayer) 
        {
            this.newPlayer();
        }else 
        {
            newGame();
            thePlayer.setText(playerName + " Game # " + numGames);
            theGuess.requestFocus();
            theGuess.setEnabled(true);
            theGuess.setBackground(Color.yellow);
        }
        
           
    }
    
     private void newGame() 
     {
         numGames++;
         randomizer.nextInt(100);
         numTries = 0;
         displayInstructions();
         
     }
    
    public void newPlayer()
    {
        theOutput.setText("");
        
        theOutput.setEnabled(false);
        theGuess.setEnabled(false);
        newPlayer.setEnabled(false);
        newNumber.setEnabled(false);
        
        thePlayer.setEnabled(true);
        thePlayer.setText(playerName);
    }
    
    
    
     @Override
    public void keyTyped(KeyEvent ke) 
    {
        
    }

    @Override
    public void keyPressed(KeyEvent ke) 
    {
        
    }

    @Override
    public void keyReleased(KeyEvent ke) 
    {
        JTextField tf = (JTextField) ke.getSource();
        int key = ke.getKeyCode();
        if(key == KeyEvent.VK_ENTER)
        {
            if(tf == thePlayer)
            {
                addPlayer();
            }else
            {
                newGuess();
            }
        }
    }
    
    public void addPlayer()
    {
        playerName = thePlayer.getText();
       if(playerName.equals(""))
       {
           theOutput.append("\nEnter a name to play");
       }else
       {
           amtRemaining = 100;
           numGames = 0;
           thePlayer.setEnabled(false);
           thePlayer.setBackground(Color.WHITE);
           newPlayer.setEnabled(true);
           newNumber.setEnabled(true);
           theGuess.setEnabled(true);
       }
    }
    
    private void newGuess() 
    {
        String curStr = "";
        int curGuess = 0;
        
        Integer.parseInt(curStr);
        numTries++;
        theOutput.append(curStr);
        if(theNumber == curGuess)
        {
            gameWon();
        }else
        {
            theOutput.append("Try a higher or lower number");
            theGuess.requestFocus();
            theGuess.setBackground(Color.yellow);
            theGuess.selectAll();
        }
    }
    
    public void gameWon()
    {
        double curWinnings = 0;
        
        theOutput.setText("******* WINNER ********" + "\n" + "Amount Wagered ");
        theOutput.append("Amount of tries " + numTries + "\n" + "Amount Wagered: " + curWinnings);
        switch(numTries)
        {
            case 1:
                if(numTries == 1)
                {
                    curWinnings = 2.00;
                }
            case 2:
                if(numTries == 2)
                {
                    curWinnings = 1.75;
                }
                case 3:
                if(numTries == 3)
                {
                    curWinnings = 1.50;
                }
                case 4:
                if(numTries == 4)
                {
                    curWinnings = 1.25;
                }
                case 5:
                if(numTries == 5)
                {
                    curWinnings = 1.00;
                }
                case 6:
                if(numTries == 6)
                {
                    curWinnings = 0.75;
                }
                case 7:
                if(numTries == 7)
                {
                    curWinnings = 0.50;
                }
                case 8:
                if(numTries == 8)
                {
                    curWinnings = 0.25;
                }
                case 9:
                if(numTries == 9)
                {
                    curWinnings = 0;
                }
            default:
                
        }
    }
    
     private void displayInstructions() 
    {
        theOutput.append("Instructions test text");
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        GOM gui = new GOM();
    }
    
}

推荐答案

500(EventQueue.java:97)
at java.awt.EventQueue
500(EventQueue.java:97) at java.awt.EventQueue


3.run(EventQueue.java:709)
at java.awt.EventQueue
3.run(EventQueue.java:709) at java.awt.EventQueue


3。 run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain
3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain


这篇关于猜猜O'的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆