Guess O'Matic the guess # issue
У меня возникла проблема с тем, чтобы угадать, когда его правильный#, выбранный компьютером, отображает пустоту gameWon (). Сначала я установил его в if(theNumber == 20), но когда я ввожу любое число, оно все равно отображает текст gameWon void, который я туда вставил. После этого он позволяет компьютеру выбрать номер, но не будет принимать номер, если он правильный, а затем ничего не работает
Что я уже пробовал:
/* * 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(); } }