猜猜O'matic猜猜#问题 [英] Guess O'matic the guess # issue

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

问题描述

我遇到猜测的问题,当它是由计算机选择的正确#时,显示gameWon()void。我首先将它设置为if(theNumber == 20)但是当我输入任何数字时它仍然显示我放在那里的gameWon void text。它让计算机选择一个数字,但如果正确则不会取数字然后什么都不起作用



我尝试了什么:



I'm having an issue with having the guess, when its the correct # that is selected by the computer, displaying the gameWon() void. I first have it set to if(theNumber == 20) but when I input any number it still displays the gameWon void text that I put in there. After it lets the computer select a number but won't take the number if its correct and then nothing works

What I have tried:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
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();
    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.00;
    
    
    
    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++;
         theNumber = randomizer.nextInt(100);
         numTries = 0;
         displayInstructions();
         bankRoll.setText(amtRemaining-- + " Zipoids");
         updateScore();
         theOutput.setText("");
         
     }
    
    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");
           thePlayer.requestFocus();
       }else
       {
           amtRemaining = 100.00;
           numGames = 0;
           thePlayer.setEnabled(false);
           thePlayer.setBackground(Color.WHITE);
           newPlayer.setEnabled(true);
           newNumber.setEnabled(true);
           theGuess.setEnabled(true);
       }
    }
    
    private void newGuess() 
    {
        String curStr = "123";
        
        int converted = Integer.parseInt(curStr);
        numTries++;
        int curGuess = converted;
        if(theNumber == 20)
        {
            gameWon();
            bankRoll.setText(curGuess + " Zipoids");
        }else
        {
            theOutput.append("Try a higher or lower number \n");
            theGuess.requestFocus();
            theGuess.setBackground(Color.yellow);
            theGuess.selectAll();
            bankRoll.setText(amtRemaining-- + " Zipoids");
        }
        //Prevents the 'Zipoids' from going into the negatives
        if(amtRemaining == 0.0)
        {
            theOutput.append("Game Over\n");
            theOutput.setEnabled(false);
            theGuess.setEnabled(false);
            newPlayer.setEnabled(false);
            thePlayer.setEnabled(false);
        }
    }
    
    public void gameWon()
    {
        double curWinnings = 0.0;
        
        theOutput.setText("******* WINNER ********");
        theOutput.append("\nAmount of tries: " + numTries + "\n" + "Amount Wagered: " + amtRemaining * curWinnings);
        theGuess.setText("");
        theGuess.setBackground(Color.white);
        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.0;
                }
            default:
                if(numTries < 9)
                {
                    curWinnings = 0.0;
                }
        }
    }
    
    public void updateScore()
    {
        thePlayer.setText(playerName + numGames);
        bankRoll.setText(amtRemaining + " Zipoids");
    }
    
     private void displayInstructions() 
    {
        theOutput.append("Enter your name" + "\nPress ENTER to play\n");
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        GOM gui = new GOM();
    }
    
}

推荐答案

你应该读这个: switch语句(Java™教程>学习Java语言>语言基础知识) [ ^ ]

You should read this: The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)[^]
引用:

我遇到了猜测的问题,



学习如何使用的时间调试器!



有一个工具可以让你看到你的代码在做什么,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

HTTP://docs.or acle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你到。当代码没有达到预期的效果时,你就会接近一个错误。


Time to learn how to use a debugger!

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


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

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